Tuesday, December 11, 2007

Arduino Knightrider


I finally got around to ordering an Arduino from Sparkfun.com. For those of you who don't know, Arduino is an open source platform for interfacing electronic devices with a microcontroller. More information can be found on arduino.cc. I've tried to used PIC microcontrollers in the past and found that the information available is more geared towards electronic professionals, and therefore over my head. The Arduino, on the other hand, is a collaborative project designed to be used by artists to make interactive art. If an artist can use it I sure as hell can :P The programming language is based on C, which is still a bit foreign to me, so I've resorted to asking my wife to help me since she is a computer programmer by trade. One of the first projects I've decided to make is an LED sweeper, similar to the lights on the front of Kit, the car from Knighrider. I wanted to make the lights fade in and out instead of just being on or off so I used pulse width modulation, which essentially pulses the lights really quickly; so quickly in fact that the human eye cannot easily detect it. You can control the percentage of time that the light is on by assigning a value between 0 and 255. 0 is off and 255 is on. With some help from wifey I wrote the following program to sweep 4 pairs of LEDs back and forth using pulse width modulation to make the LEDs increase in brightness and then decrease back to zero sequentially and then back down the line. A picture of the circuit can be found above and a video of the resulting sweep below the code.


/*
* Loopwithfade
* by mojomoney
*
* Lights multiple LEDs in sequence, then in reverse
* using pwm to fade, creating an incandescant effect.
* In this example 4 LEDs are used
*/
int delaytime = 3; // delay time between steps
int pwmincrement = 6; // value of increment to pwm value
int x; // number of times light sequence cycles
int i; // variable to keep the actual value
int ledpin1 = 9; // light connected to digital pin 9
int ledpin2 = 10; // light connected to digital pin 10
int ledpin3 = 11; // light connected to digital pin 11
int ledpin4 = 6; // light connected to digital pin 6
int value1; // intensity of led 1
int value2; // intensity of led 2
int value3; // intensity of led 3
int value4; // intensity of led 4

void setup()
{

}

void loop()

{ for(x = 0 ; x < 10; x++ ) // makes program cylce 10 times

{ for(i = 0 ; i <= 1530; i+=pwmincrement )

{ if (i<=255)

{
value1 = 255 - i; // fade led 1

value2 = i; // brighten led 2

analogWrite(ledpin1, value1); // sets value1 (range from 0 to 255)

analogWrite(ledpin2, value2); // sets value2 (range from 0 to 255)

delay(delaytime); // waits for 30 milliseconds to see dimming effect

}

else if (i<=510)

{

value2 = 510 - i; // fade led 2

value3 = i-255; // brighten led 3

analogWrite(ledpin2, value2); // sets value2 (range from 255 to 0)

analogWrite(ledpin3, value3); // sets value3 (range from 0 to 255)

delay(delaytime); // waits for 30 milli seconds to see the dimming effect

}

else if (i<=765)

{

value3 = 765 - i; // fade led 3

value4 = i-510; // brighten led 4

analogWrite(ledpin3, value3); // sets value3(range from 255 to 0)

analogWrite(ledpin4, value4); // sets value4(range from 0 to 255)

delay(delaytime); // waits for 30 milliseconds to see dimming effect

}

else if (i<=1020)

{

value4 = 1020 - i; // fade led 4

value3 = i - 765; //brighten led 3

analogWrite(ledpin4, value4); //sets value4(range from 255 to 0)

analogWrite(ledpin3, value3); // sets value3(range from 0 to 255)

delay(delaytime);

}

else if (i<=1275)

{

value3 = 1275 - i; // fade led 3

value2 = i - 1020; // brighten led 2

analogWrite(ledpin3, value3); // sets value3(range from 255 to 0)

analogWrite(ledpin2, value2); // sets value2(range from 0 to 255)

delay(delaytime);

}

else

{

value2 = 1530 - i; // fade led 2

value1 = i - 1275; // brighten led1

analogWrite(ledpin2, value2); // sets value2(range from 255 to 0)

analogWrite(ledpin1, value1); // sets value1(range from 0 to 255)

delay(delaytime);
}

}

}

}

No comments: