Structuring Arduino code
One of the most important things in programming is writing understandable and readable code. Despite the fact that usually when you deal with electronics you have to write the code in very low level language, Arduino syntax is very close to such high level programming languages as C and PHP. If you know any of these languages, I bet there will be no problems for you.
The basic way of structuring your Arduino code is writing a normal procedural code. If we recall the example arduino code from the previous post, it was something like this:
if (condition){
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(300);
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(300);
}else{
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(300);
}There is no problem with the code unless you want to give it to other people or be satisfied with the job you have done. The trick is that you can easily replace the identical parts with functions. Normally you can replace the 4 lines with just one function call:
if (condition){
blinkLed(300);
blinkLed(300);
}else{
blinkLed(300);
}or even better
if (condition){
blinkLed(2, 300);
}else{
blinkLed(1, 300);
}And the definition of the function:
void function blinkLed(times, lengthms){
for (int i = 0; i < times, i++){
digitalWrite(ledPin, HIGH);
delay(lengthms);
digitalWrite(ledPin, LOW);
delay(lengthms);
}
}The idea is simple - you choose the time in milliseconds the led will be on and the number of times the blink operation should be done. That’s it. Now if we include this code in the previous example, the code looks cleaner as well as is readable easier:
int ledPin = 13;
int usbnumber = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
usbnumber = Serial.read();
}
if (usbnumber > 0) {
if (usbnumber % 2 == 0){
blinkLed(1,300);
}else{
blinkLed(2,300);
}
usbnumber = 0;
}
}
void function blinkLed(times, lengthms){
for (int i = 0; i < times, i++){
digitalWrite(ledPin, HIGH);
delay(lengthms);
digitalWrite(ledPin, LOW);
delay(lengthms);
}
}I don’t think it’s worth to rewrite the arduino website here, so go on and check out the function declaration manual.

Great tutorial! Really
Great tutorial! Really helpful to a Arduino newbie like me.
If you use linux. load your
If you use linux. load your Diecimila with Simple Message System from the archives at arduino.cc, then get my package of shell scripts from http://user.cavenet.com/rolandl. These scripts provide full IO and PWM control. AD is scaled to milliVolts and CSV formatted for import to most spreadsheets. GUI provided by using the xdialog command. Maximum power/flexibility.
Was wondering if this site
Was wondering if this site has been left for dead. Have not seen any updates for a while. I am looking for a place to share my Arduino code with other users and get code to download from them.
Just started coding Arduino last month. Lots of fun but surprisingly have not been able to find a good site for trading source code.
Nice blog you have here. I
Nice blog you have here. I too started out on electronics some time back. My projects mainly use the popular Atmel AT89S52. I do have had some experience with the Arduino as well. check out my blog for more information.
Would you mind if I were to link back to your blog from mine ?
regards,
Seemanta