Monday, 30 September 2013

My lesson 1 mod. Flash 2 LEDs alternately.

After completing the 1st lesson I challenged myself to have two LEDs flashing alternately.

Hardware setup.

The hardware setup was straightforward and just needed another LED and an extra jumper wire. How I connected it al together.

Red lit.

Green lit.
  1. From digital pins 12 & 13 to LEDs +ve pins.
  2. LEDs -ve pins connected to ground rail.
  3. Ground rail connected to power ground pin.
  4. Done.

Software.

I built the sketch from scratch using the principals learnt in lesson 1.

// Example 01 Mod : Blinking LEDs : James Margarson 30/09/13 :
const int LED = 13; 
const int LED0 = 12;// LEDs connected to
                                   // digital pins 12 & 13
void setup()
{
pinMode(LED, OUTPUT);
pinMode(LED0, OUTPUT);// sets the digital
                                              // pins as outputs
}
void loop()
{
digitalWrite(LED, HIGH); // turns LED on
digitalWrite(LED0, LOW); // Turns LED0 off
delay(250);              // waits for 250 milliseconds
digitalWrite(LED, LOW);  // turns LED of
digitalWrite(LED0, HIGH);// turns LED0 on
delay(250);              // waits for 250 milliseconds
}

From the original example sketch I added a few extra lines of code highlighted to show my additions to control the flashing of the two LEDs.
video
The final result.

No comments:

Post a Comment