Friday, April 27, 2012

Arduino controlled LEDs

I have started making some simple Arduino programs. Here is one where I turn a knob and LED racer lights move faster or slower:






Code used to run it:
int val = 0;         
int potPin = 2;  // the pin connected to the LED

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 3; thisPin < 13; thisPin++)  {
    pinMode(thisPin, OUTPUT);     
  }
}

void loop() {
  // turn on pins from the lowest pin to the highest:
  for (int thisPin = 3; thisPin < 13; thisPin++) {

    digitalWrite(thisPin, HIGH);      // turn the pin on:
    val = analogRead(potPin)/5;      // set the delay length based on potentiometer setting
    delay(val);                      // delay for the specified time
   
    digitalWrite(thisPin, LOW);    // turn the pin off:
  }
  }
The middle lead of the potentiometer was connected to analog pin #2, while digital pins 3-13 were used for digital input.

No comments: