A short program for fading 6 LED's (w Atemega168).
Demonstrates use of arrays
/* Fade6LEDs
Paul Badger 2007
Program to fade 6 LED's up and down.
Demonstates using arrays
*/
int i; // counter variable
float LED[] = {0,0,0,0,0,0}; // pwm brightness variables
float spChg[] = {.8, 2.75, .21, .61, .49, .99}; // speeds to fade LED's - change these for faster or slower fades
int pwmPins[] = {3,5,6,9,10,11}; // PWM pins - 3,5,6 won't work on Atmega8
int ud[]={1,1,1,1,1,1}; // up - down multiplier
void setup() {
Serial.begin(9600);
}
void loop() {
for (i=0; i<=5; i++){
if (LED > 255){
_ ud = -1;_ * }* _ else if(LED < 1){ ud = 1; } LED = LED + (spChg * ud*);* analogWrite(pwmPins, (int)LED*); } }*_
/* Fade6LEDExpon
Paul Badger 2007
Program to fade 6 LED's up and down, with an exponential curve
This one looks more organic than the arithmetic version
Demonstates using arrays
*/
int i; // counter variable
float LED[] = {1,1,1,1,1,1}; // pwm brightness variables
float spChg[] = {1.04, 1.05, 1.06, 1.07, 1.03, 1.003}; // speed varialbes to fade LED's - change these for faster or slower fades
// values should be close to and greater than 1
int pwmPins[] = {3,5,6,9,10,11}; // PWM pins - 3,5,6 won't work on Atmega8
int ud[]={1,1,1,1,1,1}; // up - down multiplier
void setup() {
Serial.begin(9600);
}
void loop() {
for (i=0; i<=5; i++){
if (LED > 255){
_ ud = -1;_ * }* _ else if(LED < 1){ ud = 1; }_
_ if (ud == 1){ // up flag, multiply spChg variable LED = LED * spChg*; // multiply by a fixed amount - spCh array controls the speed of fade* * } else* * { // down flay, divide spChg variable* LED = LED / spChg*;* * }*_
analogWrite(pwmPins_, (int)LED*); delay(1); // this will interact with the spChg variables to control ultimate fade speed* * } }*_