LED Blink using Arduino

Now let’s have some more blinky fun,a horizontal row with each light blinking in sequence from left to right to left…
Your mission, is to simply recreate this with your Arduino, using not one but eight LEDs. Blink them in an endless loop in the sequence 1-2-3-4-5-6-7-8-7-6-5-4-3-2-1-2-3-… with a delay of one second.

You will need:

  1. Your standard Arduino setup (computer, cable, Uno or compatible)
  2. 8 light emitting diodes (LEDs). Anything as long as they draw less than 40mA.
  3. 8 560 ohm 0.25 W resistors. They are to reduce the current to protect the LEDs
  4. a breadboard and some connecting wire


Hint – make the delay a variable so you can alter it easily.

In previous tutorial we connected led to pin 13. This time, you can use digital pins 2 to 9. The hardware side is easy – run a wire from each digital output pin to the anode of an LED, then put a 560 ohm resistor from the cathode back to ground. 

See the diagram below:


Now it is your turn to do something – write the code! But before you do that, plan what you want to do first (some old-schoolers would call the plan an algorithm). For example, for this exercise you might write something like…

exercise 0.1 plan

  1. set all the pins I want to use as outputs
  2. create a variable to store the delay duration in milliseconds
  3. start an infinite loop
  4. turn on pin 2, wait for delay duration, turn it off
  5. turn on pin 3, wait for delay duration, turn it off
  6. repeat for pins 4 to 9
  7. then do the same thing but backwards down to pin 3
  8. end of infinite loop


So how did you go? Did it work first time? It’s ok if it didn’t, as you learn by doing and fixing your mistakes. Remember – if you have any questions, leave a comment at the bottom of this post and I will get back to you. But to save you time, here is the sketch:


CODE

 http://www.projectbandya.com
 Blinks LEDs from output pin 2~9 in a forwards<>backward motion
 The circuit:
 an LED is connected to each output pin from 2 to 8, thence to a 560 ohm resistor, then to ground (pin 4 on the bottom left of the Arduino).
 */

// The setup() method runs once, when the sketch starts


int del=100; // sets a default delay time, 100 milliseconds (one tenth of a second)
void setup()
{
  // initialize the digital pins as outputs:
  // later on there will be easier ways to do this
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}
// the loop() method repeats indefinitely until you turn off the power
void loop()
{
  digitalWrite(2, HIGH);   // turn on LED on pin 2
  delay(del);              // wait (length determined by value of 'del')
  digitalWrite(2, LOW);    // turn it off
  digitalWrite(3, HIGH);   // turn on LED on pin 3
  delay(del);              // wait
  digitalWrite(3, LOW);    // turn it off
  digitalWrite(4, HIGH);   // turn on LED on pin 4
  delay(del);              // wait
  digitalWrite(4, LOW);    // turn it off
  digitalWrite(5, HIGH);   // turn on LED on pin 5
  delay(del);              // wait
  digitalWrite(5, LOW);    // turn it off
  digitalWrite(6, HIGH);   // turn on LED on pin 6
  delay(del);              // wait
  digitalWrite(6, LOW);    // turn it off
  digitalWrite(7, HIGH);   // turn on LED on pin 7
  delay(del);              // wait
  digitalWrite(7, LOW);    // turn it off
  digitalWrite(8, HIGH);   // turn on LED on pin 8
  delay(del);              // wait
  digitalWrite(8, LOW);    // turn it off
  digitalWrite(9, HIGH);   // turn on LED on pin 9
  delay(del);              // wait
  digitalWrite(9, LOW);    // turn it off
  digitalWrite(8, HIGH);   // turn on LED on pin 8
  delay(del);              // wait
  digitalWrite(8, LOW);    // turn it off
  digitalWrite(7, HIGH);   // turn on LED on pin 7
  delay(del);              // wait
  digitalWrite(7, LOW);    // turn it off
  digitalWrite(6, HIGH);   // turn on LED on pin 6
  delay(del);              // wait
  digitalWrite(6, LOW);    // turn it off
  digitalWrite(5, HIGH);   // turn on LED on pin 5
  delay(del);              // wait
  digitalWrite(5, LOW);    // turn it off
  digitalWrite(4, HIGH);   // turn on LED on pin 4
  delay(del);              // wait
  digitalWrite(4, LOW);    // turn it off
  digitalWrite(3, HIGH);   // turn on LED on pin 3
  delay(del);              // wait
  digitalWrite(3, LOW);    // turn it off
}

So there you have it. Today you have learned how to set up a computer to program your Arduino, and written some sketches to create outputs based on your design. Although it wasn’t that much, we have to start somewhere… but great things will follow.So there you have it. Today you have learned how to set up a computer to program your Arduino, and written some sketches to create outputs based on your design. Although it wasn’t that much, we have to start somewhere… but great things will follow.

projectbandya

No comments:

Post a Comment