arduino:program_structure
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
arduino:program_structure [2012/09/13 04:35] – mithat | arduino:program_structure [2012/09/13 06:01] – [Main loop] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
===== Main loop ===== | ===== Main loop ===== | ||
+ | |||
+ | All Arduino programs (called " | ||
<file c program_structure.ino> | <file c program_structure.ino> | ||
void setup() { | void setup() { | ||
- | // Stuff in here gets run once (when the program starts). | + | // Stuff in here gets run once when the program starts. |
} | } | ||
Line 24: | Line 26: | ||
void setup() { | void setup() { | ||
- | // Make pin 13 an output. | + | // make pin 13 an output |
pinMode(13, OUTPUT); | pinMode(13, OUTPUT); | ||
} | } | ||
Line 36: | Line 38: | ||
</ | </ | ||
- | Here is the same example but using a //variable// to store the pin number. | + | Here is the same example but using a **variable** to store the pin number. |
<file c BlinkMe2.ino> | <file c BlinkMe2.ino> | ||
/* | /* | ||
- | | + | |
Turn an LED on and off once per second (improved version). | Turn an LED on and off once per second (improved version). | ||
*/ | */ | ||
- | // Use pin 13 to drive the LED. | + | // use variable ledPin |
- | int led_pin | + | int ledPin |
void setup() { | void setup() { | ||
- | // make the pin an output. | + | // make ledPin |
- | pinMode(led_pin, OUTPUT); | + | pinMode(ledPin, OUTPUT); |
} | } | ||
void loop() { | void loop() { | ||
- | digitalWrite(led_pin, HIGH); | + | digitalWrite(ledPin, HIGH); |
- | delay(1000); | + | delay(1000); |
- | digitalWrite(led_pin, LOW); // turn the LED off by making the voltage LOW | + | digitalWrite(ledPin, LOW); // turn the LED off |
- | delay(1000); | + | delay(1000); |
} | } | ||
</ | </ | ||
Now if we need to change the pin number, we only need to change it in one place. | Now if we need to change the pin number, we only need to change it in one place. | ||
+ | |||
+ | Here we use variables also to define delay times: | ||
+ | |||
+ | <file c BlinkMe3.ino> | ||
+ | /* | ||
+ | BlinkMe3 | ||
+ | Turn an LED on and off once per second (another improved version). | ||
+ | */ | ||
+ | |||
+ | // use variable ledPin to store the pin number that drives the LED | ||
+ | int ledPin = 13; | ||
+ | // define the LED's on and off times in milliseconds | ||
+ | int onTime = 1000; | ||
+ | int offTime = 1000; | ||
+ | |||
+ | void setup() { | ||
+ | // make ledPin an output | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | digitalWrite(ledPin, | ||
+ | delay(onTime); | ||
+ | digitalWrite(ledPin, | ||
+ | delay(offTime); | ||
+ | } | ||
+ | </ |
arduino/program_structure.txt · Last modified: 2012/09/14 01:20 by mithat