Arduino Project Video - Advanced Blinky Lights

I'm working on learning the Arduino, an electronics board with many interesting application for getting the physical world and the digital world to interact. It has been used to create interactive art, musical instruments, mass transit alerts, rc helicopters and super accurate espresso machines along with tons of other things. It has been used to take input from websites, streams of water, wii nunchucks and various items you might not expect.

For now, I'm just blinking lights and sounding alarms. I've given it three patterns for blinking 5 lights and am demonstrating the ability for me to instruct the arduino which pattern to use for the blinking. I'm also pointing out some other interesting things I stumbled on as I tinkered.

Arduino Project - Advanced Blinky Lights from Josiah Ritchie on Vimeo.

Here is the sketch used in this demonstration. (A sketch is just a program used to give instructions to the arduino.) Consider it free to do with as you wish, not attribution needed. I stole much of the code from several other sketches around the web.

int lightArray[] = { 11, 9, 6, 5, 3 }; // an array of the pins available
int timer = 100;                       // used to indicate how long the pin should be on or off
int count = 0;                         // this is used to iterate over the pins. It should always start at 0
int style = 49;                        // the initial starting style is set to the ASCII code for 1

void setup()                      // run once, when the sketch starts
{
  for (count=0; count<6; count++)  // setup the pins for output
  {
   pinMode(lightArray[count], OUTPUT);
  }
  Serial.begin(9600);            // tell arduino to start up serial comms
}
 
void loop()                      // run over and over again
{
  blink(style);                  // blink in the style you want
  style = styleCheck();          // check to see if the preferred style has changed
  blink(0);                      // clear the LEDs so the next pattern has a clean slate (blink's default)
}

int styleCheck()                   // get the style input from the serial port
{
  int input = style;
  int inputInt = 0;
  Serial.println("Which style? (0-3)");
 
  if( Serial.available() ){
    input = Serial.read();
  }
 
  inputInt = input;              // future enhancement, convert the ASCII character to an integer
  Serial.println(inputInt);
  return inputInt;
}

void blink(int type)        // blink lights according to the chosen style
{
  switch (type) {
   
    case 49:  // the ASCII code for 1
      digitalWrite(lightArray[1], HIGH);
      delay(timer);
      digitalWrite(lightArray[3], HIGH);
      delay(timer);
      digitalWrite(lightArray[1], LOW);
      delay(timer*2);
      break;
     
    case 50:  // the ASCII code for 2
      digitalWrite(lightArray[0], HIGH);
      delay(timer);
      digitalWrite(lightArray[2], HIGH);
      delay(timer);
      digitalWrite(lightArray[2], LOW);
      delay(timer);
      digitalWrite(lightArray[0], LOW);
      delay(timer);
      break;
     
    case 51: // the ASCII code for 3
      for (count=0; count<6; count++) // iterate over pins
      {
        digitalWrite(lightArray[count], HIGH);
        delay(timer*5);
        digitalWrite(lightArray[count], LOW);
        delay(timer);
      }
      for (count=5; count>0; count--)  // iterate over pins in reverse order
      {
        digitalWrite(lightArray[count], HIGH);
        delay(timer*5);
        digitalWrite(lightArray[count], LOW);
        delay(timer);
      }
    default:
      for (count=0; count<6; count++) // iterate over pins, turning them all off
      {
        digitalWrite(lightArray[count], LOW);
      }
  }
}