[ Pobierz całość w formacie PDF ]
low (pressed), the if condition is true and the goto statement executed, breaking out of the while
loop.
functions
Functions are a powerful programming feature that are used when you want to set up an action
that can be called from several places in the program. For example, let's say you wanted an LED
connected to pin 2 to flash 3 times as an alert, but that you needed to execute the alert at three
different places in the program. One solution would be to type in the flashing code at the three
separate program locations. This uses up precious code space and also means that if you change
the flash function, for example changing from 3 flashes to 4, you have to change the code in
three places. A better solution is to write the flash function as a subroutine and to call it from the
main body of the code. Here is an example
24
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
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.
25
·ð Mark major sections or functions with a comment header line or two
·ð Have just the right number of comments, not too few and not too many. Assume the reader
knows the programming language so have the comment be instructive. Here is an example of
an instructive comment
digitalWrite(4,HIGH) // turn on motor
and here is a useless comment
digitalWrite(4,HIGH) // set pin 4 HIGH
You need not comment every line. In fact, commenting every line is generally bad practice.
·ð Add the comments when you create the code. If you tell yourself, "Oh, I'll add the comments
when the code is finished", you will never do it.
10 Common Coding Errors
·ð Forgetting the semi-colon at the end of a statement
·ð Misspelling a command
·ð Omitting opening or closing braces
Please send comments, suggestions and typo alerts about this guide to wkdurfee@umn.edu
11 Appendix
On the following page is a summary of basic Arduino commands, with examples.
26
Arduino Programming Basics
Command Description
pinMode(n,INPUT)
Set pin n to act as an input. One-time command at top of program.
pinMode(n,OUTPUT)
Set pin n to act as an output
digitalWrite(n,HIGH)
Set pin n to 5V
digitalWrite(n,LOW)
Set pin n to 0V
delay(x)
Pause program for x millisec, x = 0 to 65,535
tone(n,f,d)
Play tone of frequency f Hz for d millisec on speaker attached to pin n
for()
Loop. Example: for (i=0;i
if (expr) {}
Conditional branch. If expr true, do instructions enclosed by {}
while (expr) {}
While expr is true, repeat instructions in {} indefinitely
For more commands see the ME2011 Arduino Microcontroller Guide and the Language Reference
section of the arduino web site.
Instructions in the setup() function are executed once. Those in the loop() function are executed
indefinitely.
Examples
1. Turn on LED connected to Pin 2 for 1 s. 4. Play 440 hz tone for one second on speaker
connected to pin 5. Delay is needed because
void setup() {
the program does not wait for the tone()
pinMode(2,OUTPUT);
command to finish but rather immediately goes
digitalWrite(2,HIGH);
to the command following tone().
delay(1000);
digitalWrite(2,LOW);
void setup() {
}
pinMode(5,OUTPUT);
void loop()
tone(5,440,1000);
{}
delay(1100);
}
2. Flash LED connected to Pin 2 at 1 Hz forever. void loop()
{}
void setup() {
pinMode(2,OUTPUT);
5. LED is on Pin 2 and switch is on Pin 6. Turns
}
on the LED for one sec when switch is pressed.
void loop() {
digitalWrite(2,HIGH);
void setup() {
delay(500);
pinMode(2,OUTPUT);
digitalWrite(2,LOW);
pinMode(6,INPUT);
delay(500);
while (digitalRead(6) == HIGH)
}
;
digitalWrite(2,HIGH);
3. Turn on motor connected to Pin 4 for 1 s. delay(1000);
digitalWrite(2,LOW);
void setup() {
pinMode(4,OUTPUT);
}
digitalWrite(4,HIGH);
void loop()
delay(1000);
{}
digitalWrite(4,LOW);
}
void loop()
{}
27
[ Pobierz całość w formacie PDF ]