// https://github.com/lilspikey/arduino_sketches/blob/master/larson/larson.ino
#define PIN_COUNT 8
#define UPDATE_DURATION 30
// Uno (those with the ATmega168 or ATmega328)
// PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function
// On the Arduino Mega, it works on pins 2 to 13 and 44 to 46; Provide 8-bit PWM output with the analogWrite() function.
//
int pins[PIN_COUNT] = { 2, 3, 4, 5, 6, 7, 8, 9};
int states[PIN_COUNT];
int current_pin = 0;
int dir = 1;
int update_count = 0;
void setup() {
for ( int i = 0; i < PIN_COUNT; i++ ) {
pinMode(pins[i], OUTPUT);
states[i] = 0;
}
}
void updatePins() {
for ( int i = 0; i < PIN_COUNT; i++ ) {
analogWrite(pins[i], states[i]); // AnalogWrite uses pulse width modulation (PWM),
// turning a digital pin on and off very quickly,
// to create a fading effect.
}
delay(6);
}
void decay() {
for ( int i = 0; i < PIN_COUNT; i++ ) {
states[i] = (19*states[i]/20);
}
}
void loop() {
decay();
states[current_pin] = 255 * update_count / UPDATE_DURATION;
updatePins();
update_count++;
if ( update_count > UPDATE_DURATION ) {
update_count = 0;
current_pin += dir;
if ( current_pin == 0 ) {
dir = 1;
}
else if ( current_pin == (PIN_COUNT-1) ) {
dir = -1;
}
}
}