Tuesday, 27 December 2011

Knight Rider

So for my birthday one of my friends got me a soldering iron so I was finally able to dismantle a bunch of LEDs that Talia had given to by several months ago. The fun part was that this gave me enough LEDs to do my next electronics project. It was another fairly simple project that just turned on one of 10 leds in a row producing an effect similar to the one for the knightrider car KITT (yes, I'm that old).


Code:


int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int currentLED = 0;
int dir = 1;
int ledDelay = 65;

void setup()
{
  for(currentLED = 0; currentLED < 10; currentLED++)
  {
    pinMode(ledPins[currentLED], OUTPUT);
  }
  currentLED = 0;
}

void loop()
{
  digitalWrite(ledPins[currentLED], HIGH);
  delay(ledDelay);
  digitalWrite(ledPins[currentLED], LOW);
  currentLED = currentLED + dir;
  if(currentLED == 9)
  {
    dir = -1;
  }
  if(currentLED == 0)
  {
    dir = 1;
  }
}

Nothings really that complicated. Used an array to save all the pin numbers, and an index to point to the current led. Sadly I didn't have enough wires to connect things properly (I really need to get some wire strippers and a big spool of solid core wire) so I improvised a little with the ground wires :)

~(' ')~

No comments:

Post a Comment