-
-
Save bigjosh/1e9f6b109aeccc3c1d2638668588ccd4 to your computer and use it in GitHub Desktop.
Looking to have a circular fade rotating at a specific speed of degrees per second.
This file contains hidden or 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
/* | |
* Speed up the rotation of LEDs lights or slow down based on button press | |
*/ | |
#include "blinklib.h" | |
#include "Serial.h" | |
ServicePortSerial Serial; | |
#define MS_PER_S (1000) | |
uint16_t speed_d_per_s = 360; // speed is represented in degrees/sec, 360 would be a single rotation per second | |
float speed_d_per_ms = ( (float) speed_d_per_s) / MS_PER_S ; | |
uint32_t timeOfLastLoop_ms = 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"); | |
timeOfLastLoop_ms = millis(); | |
} | |
// Sin in degrees ( standard sin() takes radians ) | |
float sin_d( uint16_t degrees ) { | |
return sin( ( degrees / 360.0F ) * 2.0F * PI ); | |
} | |
float rotation_d = 0; | |
void loop() { | |
// put your main code here, to run repeatedly: | |
uint32_t now = millis(); | |
uint32_t timeDiff_ms = now - timeOfLastLoop_ms; | |
if( timeDiff_ms >= 1) { | |
//setColor( OFF ); // JL: Don't want this! You will get aliasing effects if colors just happen to get sampled when off. There is a vertcal retrace if you want to sync. | |
// Especially pronounced becuase the floating point calcs below are so s...l...o....w on 8 bit AVR. | |
// calculate the amount of rotation | |
rotation_d += timeDiff_ms * speed_d_per_ms ; | |
if(rotation_d >= 360.f) { | |
rotation_d -= 360.f; | |
} | |
FOREACH_FACE(f) { | |
// determine brightness based on the unit circle (sinusoidal fade) | |
uint16_t angle_of_face_d = 60 * f; // (360 degrees) / (6 faces) = degrees per face | |
int brightness = 255 * (1 + sin_d( angle_of_face_d + rotation_d ) ) / 2; | |
setFaceColor( f , makeColorHSB(0,0,brightness)); | |
// if(f==4) { | |
// Serial.print("rotation: "); | |
// Serial.println(rotation); | |
// Serial.print("brightness: "); | |
// Serial.println(brightness); | |
// } | |
} | |
timeOfLastLoop_ms = now; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment