April 06, 2013

Going Back to C Programming with Arduino

A while back I created a cool project using Raspberry PI, Arduino, led screens, legos, and a bunch of caffeine. What I fashioned was a pretty awesome mini bill board, complete with my ability to change it at will. I been meaning to create an article on how I programmed the Ardunio. Now I have finally sat down to write up what I did.

So in order to do something cool that introduced me to a bunch of technologies, I had to know what the Arduino was capable of. Luckily, there is a serial port to communicate. If I go back to my previous article about serial communication titled: Programming Serial with C Sharp, I knew if I wanted to create a cool billboard system, I would need to do some serial stuff with the Arduino.

The great thing about the Arduino is that it does not leave you with just crappy c libraries. It allows you to use some libraries it has to help the pain of c. One such library is their serial library.

 void setup() { Serial.begin(9600); delay(500); } 

Unlike programming in Visual c++ or gcc, you do not have a main function that kicks off everything. The entry point to the program is a setup function. There you create all the logic you need to start. Next, there is another weird function called loop. This is for the embedded system. This is how it processes things over and over and over and …. you get the idea. Here is an example of some code in the loop:

 void loop() { String content1 = ""; String one, two; char character; while(Serial.available()) { character = Serial.read(); content1.concat(character); } console.log(content1); } 

That is a bit of code, but let\`s dissect it really quick. The “String” object is built in a library in Arudino\`s IDE, and it works kinda like the STL string library. Then we have a while loop. This reads all available characters from the serial buffer. Once the buffer is depleted, it will kick out of the loop and print what was sent to the console. Super easy.

You can get the Arudino IDE here: http://www.arduino.cc/ and if you are looking to buy one, check out Microcenter, or Adafruit.com. If you are a little timid about how to get started, visit adafruit, as they have many tutorials on how to do this stuff. But believe me, it is super easy. If you are looking to get an example of how to put these things together, I will have a project article out soon to describe it all, so stay tuned. 🙂