25
int i;
void setup()
{
pinMode(2,OUTPUT);
Serial.begin(9600);
Serial.println("Welcome to my program");
delay(1000);
flasher(); // call flasher function
Serial.println("I hope you like flashing");
delay(1000);
flasher(); // call flasher again
Serial.println("Here it is one more time");
delay(1000);
flasher();
}
void loop() {}
void flasher()
{
for(i=0;i<3;i++) {
digitalWrite(2,HIGH);
delay(250);
digitalWrite(2,LOW);
delay(250);
}
}
Several things should be noted here. The function flasher() is defined outside the setup() and
loop() functions. When the main program encounters a flasher(); command, the program
immediately jumps to the function and starts executing the code there. When it reaches the end
of the function, the program returns to execute the command that immediately follows the
flasher(); command. It is this feature that allows you to call the subroutine from several different
places in the code. Parameters can be passed to and returned from functions, but that feature is
for the advanced programmer.
This concludes the section on basic program commands. You can write some awesome programs
using just what was described here. There is much more that the Arduino can do and you are
urged to read through the complete Arduino Language Reference page on-line
9 Coding Style
Style refers to your own particular style for creating code and includes layout, conventions for
using case, headers, and use of comments. All code must follow correct syntax, but there are
many different styles you can use. Here are some suggestions:
Start every program with a comment header that has the program name and perhaps a brief
description of what the program does.
Use indentation to line things up. Function name and braces are in column one, then use
indents in multiples of 2 or 4 to mark code chunks, things inside loops and so on.