Recording and playing back 20 sensor readings.
jonerik
Posts: 2
I finished my CC robotics class using the activitybot, but had trouble with the SD card section in C. I did not have any problem writing a string value to the SD card. However, I have not been able to do the section on my own where it asks you to write a program to record and log 20 sensor readings. I used the PING sensor, but my program only seems to hold one data value, which makes me think that my program is writing over all the prior values. I would appreciate some sample code.
Comments
There are much better ways to do this, but here is an example that tries to stay close to what you might know at that point. With this, I'm hoping you'll be able to modify to add Ping))) readings without too much trouble.
Notes:
In fwrite("i = ", 5, 1, fp) the string has 4 characters with a hidden 0, that's why we use 5 as the number of bytes to write. Likewise with char istr[5].
The fields are fixed width, and the reading has to stay in sync with the writing (in terms of number of bytes).
This will not display well if you try to open it as a file on your computer because the values are binary. Next post will have another way to save and retrieve the data, but also have it be readable in Notepad.
I appreciate the code. We now use your activity-bot and C tutorial in our robotics course at North Seattle Community College, which I took this summer. Previously, NSCC used the Boe-bot kit in its robotics course, which I also took. Is there a book or other resource that you would recommend to learn more about the propeller chip and C? I felt a bit frustrated this past quarter with the Parallax C tutorial and the activitybot because it just did not seem as well documented as the Boe-bot kit. The above exercise was one in which I and other students spent a lot of time on but could never figure it out. I went to the OBEX files for inspiration, but virtually everything there is in Spin. I ended up checking out a copy of The C Programming Language, which is good for learning more about C, but doesn't help with microprocessors and memory usage. I thought about purchasing Programming and Customizing the Multicore Propeller Microcontroller: The Official Guide, but it seems to be based on Spin. Similarly, there is a C for Arduino book, but of course it is Arduino, not the Propeller. What would you recommend? [h=1][/h]
The program uses memset to set most of each line to spaces, and then sets the last two characters to Windows Notepad's carriage return. Converting variable values to formatted strings with sprint is a lot like sending it to the terminal with print. The difference is that sprint puts the result in a character array, not on the terminal. So, like print("i = %d , val = %d ", i, val) would display two values on the terminal, sprint(s, "i = %d , val = %d ", i, val) stores them in a string with the same format.
The sscan function also does the same thing as scan, taking the characters from an array, and storing string represented values in variables if the format string tells it to do so. One gotcha, sscan does not treat a comma as a valid terminating character for a string represented value. So, the characters "1234," would not work, but "1234 ," would work. That's why I add spaces then commas after the %d flags instead of just commas. The commas should still work for spreadsheet imports where comma delimited values are used.