Thursday, August 22, 2013

BlinkPlus

As I mentioned in my previous post, I dug into the Blink example and played around.  This got me comfy with the Arduino IDE, working with the digital out pins, breadboarding simple boarduino circuits and it allowed me to test the boarduino (at least with the digital outs) to make sure my soldering was adequate.

So, in my first test with the Blink example, I didn't use an LED, i  just let it flash the on-board LED which is connected to the duino's digital port 13.  So this time, I grabbed a generic red LED, a 330 Ohm resistor and some jumper wires (the examples say 220 Ohm, but I didn't have any and I referred to my Electrodroid app which recommended 330 for a red LED)
I plugged the duino, LED and resistor into the breadboard, wired duino gnd to the negative (short) LED pin, wired duino d13 to the resistor, and wired the resistor to the positive (long) LED pin.

Then I plugged the duino into my laptop with USB and since it was already running the Blink sketch, and since the blink sketch flashes d13, and since the on-board LED and my red LED were both wired to d13, both blinked.

I had exactly 3 more red LEDs in my bag of tricks, and at least as many 330 Ohm resistors, so I added 3 more resistor/LED pairs and wired them up like the other one, except i wired the 4 LEDs to duino ports d8, d9, d10 and d11.

Then I modified the Blink sketch to look like this:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
//int led = 13;

// array, 4 pins for 4 LEDs
int led[4] = {8,9,10,11};

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  //pinMode(led, OUTPUT);

  // loop through array of pins, set each to OUTPUT
  for (int i=0; i<4; i++)
  {
    pinMode(led[i], OUTPUT);
  }

}

// the loop routine runs over and over again forever:
void loop() {
    // loop through pins, turning all LEDs on
    for (int i=0; i<4; i++)
    {
      digitalWrite(led[i], HIGH);   // turn the LED on (HIGH is the voltage level)
    }

    delay(1000);               // wait for a second

    // loop through pins, turning all LEDs off
    for (int i=0; i<4; i++)
    {
      digitalWrite(led[i], LOW);    // turn the LED off by making the voltage LOW
    }

    delay(1000);               // wait for a second
}

Uploaded the sketch to the duino, and all four LEDs blinked.  Magic.

So then I started to add methods to the sketch that turned the LEDs on and off in patterns, like the knightrider pattern.... I added them one at a time and updated the sketch to run the same pattern over and over and finally, wrote a method that randomly sets the delay, repeat and picks a pattern to run.  I checked this code into git, named it BlinkPlus.

I am going to rerun it with ports d2-d7, d12 and then try out an analog example to test out the remaining I/O ports.  So far so good.

No comments:

Post a Comment