User Tools

Site Tools


arduino:program_structure

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
arduino:program_structure [2012/09/13 04:37] mithatarduino:program_structure [2012/09/13 06:01] – [Main loop] mithat
Line 2: Line 2:
  
 ===== Main loop ===== ===== Main loop =====
 +
 +All Arduino programs (called "sketches") have the same basic structure: a ''setup'' function and a ''loop'' function. Whatever you write in the body of the ''setup'' function will happen only once--when the program starts (e.g. after you power up the Arduino, press the reset button, or load a new program). Whatever you write in the body of the ''loop'' function will happen over and over as fast as possible until you halt the program (e.g., by removing the power, pushing the reset button, or loading a new program).
  
 <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 40: Line 42:
 <file c BlinkMe2.ino> <file c BlinkMe2.ino>
 /* /*
-  BlinkMe+  BlinkMe2
   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 to store the pin number that drives the LED 
-int led_pin = 13;+int ledPin = 13;
  
 void setup() {                 void setup() {                
-  // make the pin an output +  // make ledPin an output 
-  pinMode(led_pin, OUTPUT);     +  pinMode(ledPin, OUTPUT);     
 } }
  
 void loop() { void loop() {
-  digitalWrite(led_pin, HIGH);   // turn the LED on +  digitalWrite(ledPin, HIGH);   // turn the LED on 
-  delay(1000);                   // wait one second +  delay(1000);                  // wait one second 
-  digitalWrite(led_pin, LOW);    // turn the LED off by making the voltage LOW +  digitalWrite(ledPin, LOW);    // turn the LED off 
-  delay(1000);                   // wait one second+  delay(1000);                  // wait one second
 } }
 </file> </file>
  
 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, OUTPUT);     
 +}
 +
 +void loop() {
 +  digitalWrite(ledPin, HIGH);   // turn the LED on
 +  delay(onTime);                // wait onTime mS
 +  digitalWrite(ledPin, LOW);    // turn the LED off
 +  delay(offTime);               // wait offTime mS
 +}
 +</file>
arduino/program_structure.txt · Last modified: 2012/09/14 01:20 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki