Created
November 5, 2015 23:39
-
-
Save AbhishekGhosh/7027342ab4a1cca663ab to your computer and use it in GitHub Desktop.
Arduino LED chase effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
volatile byte wantedPin; // which pin to pulse on and off | |
const byte PWMiterations = 40; | |
const byte PWMdutyCycle = 2; // <-- should be less than PWMiterations | |
volatile byte pwmCount; | |
ISR(TIMER1_COMPA_vect) | |
{ | |
// if in "on" cycle turn the LED on | |
if (pwmCount > PWMdutyCycle) | |
digitalWrite(wantedPin, LOW); | |
else | |
digitalWrite(wantedPin, HIGH); | |
if (++pwmCount >= PWMiterations) | |
pwmCount = 0; | |
} // end of TIMER1_COMPA_vect | |
const int numberOfLEDs = 11; | |
const byte LEDorder [numberOfLEDs] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
void setup() | |
{ | |
for (byte i = 0; i < numberOfLEDs; i++) | |
pinMode(LEDorder [i], OUTPUT); | |
// set up Timer 1 | |
TCCR1A = 0; // normal operation | |
TCNT1 = 0; // make sure we start at zero | |
TCCR1B = _BV(WGM12) | _BV(CS10) | _BV (CS12); // CTC, scale to clock / 1024 | |
OCR1A = 5; // compare A register value (1000 * clock speed / 1024) | |
TIMSK1 = _BV (OCIE1A); // interrupt on Compare A Match | |
} // end of setup | |
void doCircle (const unsigned long delayAmount) | |
{ | |
for (byte i = 0; i < numberOfLEDs; i++) | |
{ | |
noInterrupts (); | |
byte pin = LEDorder [i]; | |
wantedPin = pin; | |
pwmCount = 0; | |
interrupts (); | |
delay(delayAmount); | |
digitalWrite(pin, LOW); | |
} // end of for | |
} // end of doCircle | |
void loop() | |
{ | |
for (unsigned long n = 500; n > 0; n -= 100) | |
doCircle (n); | |
} // end of loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment