Skip to content

Instantly share code, notes, and snippets.

@jbobrow
Created December 1, 2017 12:24
Show Gist options
  • Save jbobrow/a096a4cbfa3a37f82f7943c8062054cf to your computer and use it in GitHub Desktop.
Save jbobrow/a096a4cbfa3a37f82f7943c8062054cf to your computer and use it in GitHub Desktop.
Looking to have a circular fade rotating at a specific speed of degrees per second.
/*
* Speed up the rotation of LEDs lights or slow down based on button press
*/
#include "blinklib.h"
#include "Serial.h"
ServicePortSerial Serial;
float rotation = 0;
uint16_t speed = 360; // speed is represented in degrees/sec, 360 would be a single rotation per second
uint32_t timeOfLastLoop = 0;
void setup() {
// put your setup code here, to run once:
// No setup needed for this simple example!
Serial.begin();
Serial.println("Ring Animation Debug");
}
void loop() {
// put your main code here, to run repeatedly:
uint32_t timeDiff = millis() - timeOfLastLoop;
if(timeDiff >= 1) {
setColor( OFF );
// calculate the amount of rotation
rotation += (timeDiff * speed) / 1000.f;
if(rotation >= 360.f) {
rotation -= 360.f;
}
FOREACH_FACE(f) {
// determine brightness based on the unit circle (sinusoidal fade)
int brightness = int(255.f * (1 + sin(((30.f * f) + rotation)/180.f))/2.f);
setFaceColor( f , makeColorHSB(0,0,brightness));
// if(f==4) {
// Serial.print("rotation: ");
// Serial.println(rotation);
// Serial.print("brightness: ");
// Serial.println(brightness);
// }
}
timeOfLastLoop = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment