Skip to content

Instantly share code, notes, and snippets.

@jbobrow
Created November 12, 2018 23:57
Show Gist options
  • Save jbobrow/efc45cdbb2bc70c7cd774f7460b1faa6 to your computer and use it in GitHub Desktop.
Save jbobrow/efc45cdbb2bc70c7cd774f7460b1faa6 to your computer and use it in GitHub Desktop.
Blink animation for flowing one direction into another Blink
/*
* Animation Sketch
* Directional flow towards one side of a Blink
*
*/
Timer aniTimer;
#define ANI_DURATION 2000
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
if (buttonPressed()) {
aniTimer.set(ANI_DURATION);
}
if (!aniTimer.isExpired()) {
FOREACH_FACE(f) {
byte val = getFaceValueForSendAnimation(0, f, ANI_DURATION, aniTimer.getRemaining(), 0, 255);
setFaceColor(f, dim(ORANGE, val));
}
}
}
byte getFaceValueForSendAnimation(byte actionFace, byte f, long duration, long progress, byte low, byte high) {
long offset = duration / 6;
byte dist = (actionFace + 6 - f) % 6;
byte phase;
switch (dist) {
case 0: phase = 0; break;
case 1: phase = 1; break;
case 2: phase = 2; break;
case 3: phase = 3; break;
case 4: phase = 2; break;
case 5: phase = 1; break;
}
long t0 = (phase * offset) + (duration - (offset * 3));
long t1 = (phase * offset);
byte value = map_m(progress, t0, t1, high, low);
if (progress > t0) value = high;
if (progress < t1) value = low;
return value;
}
/*
This map() functuion is now in Arduino.h in /dev
It is replicated here so this skect can compile on older API commits
*/
long map_m(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment