Last active
August 29, 2015 14:07
-
-
Save reening/8cb7cfa60a4cf99230be to your computer and use it in GitHub Desktop.
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
/* | |
* Project week 5: Introduction to the Arduino | |
* Night Rider-like fading LEDs | |
* | |
* Martijn Reening (s1523996) | |
*/ | |
#include <SoftPWM.h> | |
/* Define the fade time in milliseconds */ | |
#define FADE_TIME 60 | |
#define GLOW_TIME (FADE_TIME * 3) | |
/* Map the pins in order and calculate the pin count for later use */ | |
int pins[] = { 12, 11, 10, 9, 8, 7, 6, 5 }; | |
int pin_count = sizeof(pins) / sizeof(int); | |
/* Generic counter variable */ | |
static volatile int i; | |
void setup() { | |
/* Initialize the SoftPWM library */ | |
SoftPWMBegin(); | |
/* Initialize all of the pins and set the fade time */ | |
for(i=0; i<pin_count; ++i) { | |
SoftPWMSet(pins[i], 0); | |
SoftPWMSetFadeTime(pins[i], 0, GLOW_TIME); | |
} | |
} | |
void loop() { | |
/* Fade in each LED in order of pins, and fade out when it's on */ | |
for (i=0; i<pin_count; ++i) { | |
SoftPWMSet(pins[i], 255); | |
delay(FADE_TIME); | |
SoftPWMSet(pins[i], 0); | |
} | |
/* Wait until the last LED is fully on */ | |
delay(FADE_TIME); | |
/* Same as above, but in reverse order */ | |
for (--i; i>=0; --i) { | |
SoftPWMSet(pins[i], 255); | |
delay(FADE_TIME); | |
SoftPWMSet(pins[i], 0); | |
} | |
/* Wait until the last LED is fully on */ | |
delay(FADE_TIME); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment