Skip to content

Instantly share code, notes, and snippets.

@reening
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reening/8cb7cfa60a4cf99230be to your computer and use it in GitHub Desktop.
Save reening/8cb7cfa60a4cf99230be to your computer and use it in GitHub Desktop.
/*
* 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