February 14, 2011

Arduino UNO Programming Basics

So let's take the blink program and tinker it!

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}


You can see that we have 2 functions, setup() and loop(). What is expected is that when the program starts, setup will run once, and then loop() will run indefinitely.
To test this theory let's modify the program and add the following code in the setup() function:

  digitalWrite(13, HIGH);   // set the LED on
  delay(5000);              // wait for 5 seconds
  digitalWrite(13, LOW);    // set the LED off
  delay(5000);              // wait for 5 seconds

What we expect now is that the program will run the above piece of code once, so the LED will light for 5 seconds, then it will turn off for 5 seconds, then the blink program will execute as normal, blinking for 1 second, infinitely. 


video


So it works! :)

Here is the full code:
 
/*
 * blink_tinkered.pde
 * ------------------------
 * Turns the LED 13 ON and OFF for 5 seconds
 * the blink program starts normally
 *
 * http://spacetinkerer.blogspot.com
 *
 */
void setup() {             
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);  
    digitalWrite(13, HIGH);   // set the LED on
  delay(5000);              // wait for 5 seconds
  digitalWrite(13, LOW);    // set the LED off
  delay(5000);              // wait for 5 seconds
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}



You can download the code here.



If you like my posts then subscribe via rss, or via e-mail to stay updated.

Happy Tinkering!


If you liked this article then take a second to bookmark it with...


No comments:

Post a Comment