User Tools

Site Tools


arduino:program_structure

This is an old revision of the document!


Program structure

Main loop

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).
}

Blinking an LED is the microcontroller equivalent to a “Hello world” program in general programming.

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, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait one second (1000 milliseconds)
  digitalWrite(13, LOW);     // turn the LED off by making the voltage LOW
  delay(1000);               // wait one second
}

Here is the same example but using a variable to store the pin number.

BlinkMe2.ino
/*
  BlinkMe
  Turn an LED on and off once per second (improved version).
 */
 
// use led_pin to store the pin number that drives the LED
int led_pin = 13;
 
void setup() {                
  // make the pin an output
  pinMode(led_pin, OUTPUT);     
}
 
void loop() {
  digitalWrite(led_pin, HIGH);   // turn the LED on
  delay(1000);                   // wait one second
  digitalWrite(led_pin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                   // wait one second
}

Now if we need to change the pin number, we only need to change it in one place.

arduino/program_structure.1347511069.txt.gz · Last modified: 2012/09/13 04:37 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki