arduino:program_structure
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
arduino:program_structure [2012/09/13 05:16] – mithat | arduino:program_structure [2012/09/14 01:20] (current) – mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Program structure ====== | + | Moved [[arduino:arduino_crash_course: |
- | + | ||
- | ===== Main loop ===== | + | |
- | + | ||
- | <file c program_structure.ino> | + | |
- | void setup() { | + | |
- | // Stuff in here gets run once (when the program starts). | + | |
- | } | + | |
- | + | ||
- | void loop() { | + | |
- | // Stuff in here gets run over and over and over again (until you turn the power off). | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ===== Blink an LED ===== | + | |
- | + | ||
- | Blinking an LED is the microcontroller equivalent to a "[[https:// | + | |
- | + | ||
- | <file c BlinkMe.ino> | + | |
- | /* | + | |
- | BlinkMe | + | |
- | Turn an LED on and off once per second. | + | |
- | */ | + | |
- | + | ||
- | void setup() { | + | |
- | // make pin 13 an output | + | |
- | pinMode(13, OUTPUT); | + | |
- | } | + | |
- | + | ||
- | void loop() { | + | |
- | digitalWrite(13, | + | |
- | delay(1000); | + | |
- | digitalWrite(13, | + | |
- | delay(1000); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Here is the same example but using a **variable** to store the pin number. | + | |
- | + | ||
- | <file c BlinkMe2.ino> | + | |
- | /* | + | |
- | BlinkMe2 | + | |
- | Turn an LED on and off once per second (improved version). | + | |
- | */ | + | |
- | + | ||
- | // use variable ledPin to store the pin number that drives the LED | + | |
- | int ledPin = 13; | + | |
- | + | ||
- | void setup() { | + | |
- | // make ledPin an output | + | |
- | pinMode(ledPin, | + | |
- | } | + | |
- | + | ||
- | void loop() { | + | |
- | digitalWrite(ledPin, | + | |
- | delay(1000); | + | |
- | digitalWrite(ledPin, | + | |
- | delay(1000); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Now if we need to change the pin number, we only need to change it in one place. | + |
arduino/program_structure.1347513406.txt.gz · Last modified: 2012/09/13 05:16 by mithat