Skip to content

Instantly share code, notes, and snippets.

@christianhent
Created November 13, 2021 21:43
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 christianhent/c874c96d9e621e3540557f5c44f5a977 to your computer and use it in GitHub Desktop.
Save christianhent/c874c96d9e621e3540557f5c44f5a977 to your computer and use it in GitHub Desktop.
#include <Thread.h>
int pwmPins[] = {3, 5, 6, 9, 10, 11};
int pwmCount = 6;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int ioPins[] = {1, 0, A5, A4, A3, A2, A1, A0, 2, 4, 7, 8, 12, 13};
int ioCount = 14;
int chaseDirection = 1;
byte activePos = 0, lastPos = 0;
// 2 Threads
Thread pwmThread = Thread();
Thread ioThread = Thread();
// callback for pwmThread
void pwmCallback()
{
for (int thisPin = 0; thisPin < pwmCount; thisPin++) {
analogWrite(pwmPins[thisPin], brightness);
}
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
}
// callback for ioThread
void ioCallback()
{
lastPos = activePos;
activePos += chaseDirection;
digitalWrite(ioPins[activePos], HIGH);
digitalWrite(ioPins[lastPos], LOW);
if (activePos >= ioCount-1 || activePos < 1) {
chaseDirection *= -1;
}
}
void setup()
{
for (int thisPin = 0; thisPin < pwmCount; thisPin++) {
pinMode(pwmPins[thisPin], OUTPUT);
}
for (int thisPin = 0; thisPin < ioCount; thisPin++) {
pinMode(ioPins[thisPin], OUTPUT);
}
pwmThread.onRun(pwmCallback);
pwmThread.setInterval(30);
ioThread.onRun(ioCallback);
ioThread.setInterval(150);
}
void loop()
{
// checks if pwmThread should run
if(pwmThread.shouldRun()){
pwmThread.run();
}
// checks if ioThread should run
if(ioThread.shouldRun()) {
ioThread.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment