Created
August 10, 2017 06:07
-
-
Save ersatzavian/9ee5943a2e688dc846e55adba9c48d73 to your computer and use it in GitHub Desktop.
Quick-and-dirty SoftPWM Blink for a Meb Project
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
#include <SoftPWM.h> | |
#include <Thread.h> | |
/* DEFINES -------------------------------------------------------------------*/ | |
#define LED_COUNT 8 | |
#define MAX_BRIGHTNESS 75 // 100 to 255 | |
#define FADEUP_TIME_MS 50 | |
#define FADEDN_TIME_MS 2900 | |
#define MAXDELAY_MS 300 | |
#define MINDELAY_MS 100 | |
/* PROTOS -------------------------------------------------------------------*/ | |
void fade(void); | |
/* LED CLASS ----------------------------------------------------------------*/ | |
class led { | |
public: | |
int pin; | |
Thread thread; | |
void fire(void); | |
private: | |
}; | |
void led::fire() { | |
SoftPWMSet(pin, MAX_BRIGHTNESS); | |
thread.onRun(fade); | |
thread.setInterval(FADEUP_TIME_MS); | |
} | |
/* GLOBALS -------------------------------------------------------------------*/ | |
led leds[LED_COUNT]; | |
Thread mainThread = Thread(); | |
void fade() { | |
for (int i = 0; i < LED_COUNT; i++) { | |
SoftPWMSet(leds[i].pin, 0); | |
} | |
} | |
void scheduleNext() { | |
// pick a random LED and fire it | |
int idx = random(LED_COUNT); | |
leds[idx].fire(); | |
// reschedule thread to fire next LED | |
mainThread.onRun(scheduleNext); | |
mainThread.setInterval(random(MINDELAY_MS, MAXDELAY_MS)); | |
} | |
/* LET'S GO ------------------------------------------------------------------*/ | |
void setup() | |
{ | |
// clockwise from power connector | |
leds[0].pin = 1; | |
leds[1].pin = 0; | |
leds[2].pin = 2; | |
leds[3].pin = 3; | |
leds[4].pin = 10; | |
leds[5].pin = 9; | |
leds[6].pin = 6; | |
leds[7].pin = 12; | |
SoftPWMBegin(); | |
for (int i = 0; i < LED_COUNT; i++) { | |
SoftPWMSet(leds[i].pin, 0); | |
leds[i].thread = Thread(); | |
SoftPWMSetFadeTime(leds[i].pin, FADEUP_TIME_MS, FADEDN_TIME_MS); | |
} | |
scheduleNext(); | |
} | |
void loop() | |
{ | |
// handle all the LED threads | |
for (int i = 0; i < LED_COUNT; i++) { | |
if(leds[i].thread.shouldRun()) { | |
leds[i].thread.run(); | |
} | |
} | |
// handle the main thread | |
if (mainThread.shouldRun()) { | |
mainThread.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment