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.

Buy Phentermine
SWOMtkM Buy Phentermine Cheap Viagra Buy Xanax Ultram Ambien Buy Valium
Cheap Phentermine
dajAOAdV Cheap Phentermine Ativan Cialis Buy Ambien Valium Buy Xanax
Cialis
QBVnPSiT Cialis Cheap Tramadol Xanax Cheap Phentermine Buy Ambien Cheap Valium
Buy Xanax
cfJwFU Buy Xanax Cheap Ativan Cheap Phentermine Valium Cheap viagra Cheap Ambien
Xanax
CUOJLwH Xanax Valium Cheap Phentermine Ambien Cialis Tramadol
Cheap Valium
lWUmCiqb Cheap Valium Phentermine Buy Xanax Ambien Buy Viagra Ativan
Buy Phentermine
ojoVPy Buy Phentermine Xanax Viagra Cheap Cigarettes Valium Buy Ambien
Cheap watches
ZEpOqKTt Cheap watches Cheap Phentermine Tramadol Tag Heuer Watches Ativan Xanax
Cheap Tramadol
VrBWpyQq Cheap Tramadol Cheap viagra Phentermine Buy Cialis Ambien
Buy Xanax
NWKKZa Buy Xanax Cheap Ambien Cheap watches Cheapest Cialis Cheap Tramadol
Cialis
gZLwTy Cialis Ativan Cheap Phentermine Ambien Buy Viagra online Xanax
Phentermine
GviqsT Phentermine Valium Klonopin Xanax Buy Viagra online
Cheap Xanax
EqhhHNJ Cheap Xanax Viagra Cheap Phentermine Klonopin Cheap Ativan
Cialis
OscLSch Cialis Buy Tramadol Buy Klonopin Ativan Ambien
Viagra
QJAcrLY Viagra Buy Cialis Cialis Cheap Phentermine Viagra Buy Viagra
Buy Xanax
ibUqPFsl Buy Xanax Cheap Valium Cheap Ativan Phentermine Buy Viagra online Ambien
Cheapest Cialis
TQxqwLX Cheapest Cialis Buy Valium Phentermine Buy Ambien Ativan Buy Xanax
Buy Xanax
ggiOOUHs Buy Xanax Buy Phentermine Cialis Valium Ativan Buy Ambien
Cheap Phentermine
GrpDogsu Cheap Phentermine Cheap Valium Buy Viagra Cheap Ativan Ambien Buy Xanax
Buy Viagra
DZkSbSb Buy Viagra Cialis Cialis Buy Viagra Cialis
Buy Winston
KHsVMwL Buy Winston Buy Cigarettes Buy Camel Buy Lucky Strike Pall Mall cigarettes buy Marlboro cigarettes
Buy Phentermine
OaKofEw Buy Phentermine Cheap Valium Ativan Cheap Ambien Cheap Xanax Ultram
Cialis
ZPtalwL Cialis Cheap viagra Viagra Buy Cialis Buy Cialis Online
Buy Viagra online
fcwVtX Buy Viagra online Buy Viagra online Cheap Xanax Buy Ativan Tramadol
Cialis
ANZeRnIO Cialis Lorazepam Buy Valium Diazepam Buy Valium
griThog
PInHcVt griThog
xkNwVaO
SscUYV xkNwVaO
Buy Generic Viagra | Generic
Buy Generic Viagra |
Generic Cialis |
Cheap Generic Viagra |
Cheap Generic Cialis |
Generic Levitra |
Cheap propecia
Generic Viagra and Kamagra
Generic Viagra and Kamagra is widely used by men to treat their ED. It contains sildenafil citrate a clinically proven drug that helps men with ED in achieving erection.
Bellspharmacy.com is one of the highly acclaim distributor of generic drugs. This company is known worldwide of its quality drugs and it offer generic drugs at a very low prices. Visit them online
Kamagra | Generic Viagra | Caverta | Generic Viagra | Cheap Generic Viagra | Kamagra | Cheap Generic Viagra | Finpecia | Penegra | Cheap Generic Viagra
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
thanks...
casino online online casino roulette play roulette online online casino deposit bonus online craps casino online video slot slot machines online blackjack online casino play blackjack online casinos slot machines deposit casino bonuses online casinos for usa players online casinos for money bonus usa casino online online video poker video poker machines online poker videos online baccarat best online baccarat online casino keno online keno casino