carlynorama

the blog

Intel Galileo and Bee Mail Part 6: c string to int on an Arduino

by Carlyn Maw on March 31, 2014, no comments

NEW! There will be a Part 7. I’m splitting the wrap-up off of this post. The last few posts have been very unphotogenic and I have one last unphotogenic step to get through before the step with more to see. Instead of burying the pretty, I’ll give the project glamor shots their own day.

This installment will only cover changes made to the getEmailCount() function needed to finally read the file I could now make on the SD card.

Strings to Ints

Sadly, even once a file with a number in lived it on my SD card, the code from the sparkfun Intel Galileo tutorial still wasn’t working for me. I changed a whole bunch of things before rechecking it, so I still don’t know what fixed it for sure. Bad troubleshooting hygiene, I’ll admit.  A better me would go back and recreate the error, but I don’t have time.

When I ran the code I was seeing a null value in the int that was supposed to be holding the quantity of emails found in the inbox. That int derives its value from each of the ASCII characters in the text file on the SD card being passed into an array. Some processing then needs to happen on those characters and/or the array to finish the transformation of a String into an int.

If that paragraph made no sense, the following links are a place to start. I picked them because they all discuss serial communication. During serial communication information is passed between computers and the protocol has to be really clear about what format the data moves around in.  This makes the topic a good one introduction to thinking about swapping data between two programs running on the same machine as well.

To be clear, Jim Lindblom has a clever approach where he converts each ASCII digit into its int value by subtracting the ASCII value for 0 (48) from each byte read from the txt file (line 118).  He then implements a for loop to step through the array those int values had been placed in, multiplying each value by the appropriate power of ten so their sum would equal the correct number of emails (line 125, 126).  This demonstrates a valid and very Arduino community friendly approach because it requires no functions outside of the Arduino reference guide.

Seriously cranky at this point, I chose the less friendly but simpler to me path of loading the text digits into a char array, adding a NULL termination to it and then using the c function atoi() in lieu of the for loops.  Keep in mind, the word “string” in C documentation usually refers to a null-terminated char array not an analog of the Arduino String object.

Celebrate Good Times

This function worked!!! Happy Dance! Ready to move the shield over from the Uno to the Intel Galileo! Yay!

In this series

  • Part 1
    • Overview
  • Part 2
    • From Zero to Blink.
    • Creating an SD card image and getting a sketch to persist on the Galileo
    • UPDATE: Blink on an external LED
  • Part 3
    • Creating the bees and getting them to work on an Arduino UNO
  • Part 4
    • Setting up Wifi (very easy, library examples worked immediately)
    • Getting the SD card read & write to work (a missing libraries/symlink problem)
  • Part 5
    • Serial Communication to the Linux Core
    • Troubleshooting Python on the Galileo
  • Part 6 (this post)
    • Revising the String to Int code
  • Part 7
    • Motors moved to Intel Galileo
    • Conclusions
    • Next Steps

Leave a Reply

carlynorama is Stephen Fry proof thanks to caching by WP Super Cache