Skip to content

Instantly share code, notes, and snippets.

@bhollis
Created February 15, 2023 02:52
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 bhollis/acc44cc261e3152d8997dc2e7a6b4dfe to your computer and use it in GitHub Desktop.
Save bhollis/acc44cc261e3152d8997dc2e7a6b4dfe to your computer and use it in GitHub Desktop.
GlowBeast arduino code - breathing glow effect
// Just for reference
//int static_pin = 11;
//int dynamic_pins[] = {3, 5, 6, 9, 10};
// I'm using parallel arrays to hold my object info here - each column is
// an LED.
// CONSTANTS
int pwm_pins[] = { 3, 5, 6, 9, 10, 11 };
float base_speeds[] = { 1, 1, 1, 1, 1, .5 }; // in deg/cycle
float speeds[] = { 1, 1, 1, 1, 1, .5 }; // in deg/cycle, should be a copy of base_speeds[]
int max_bright[] = { 255, 255, 255, 255, 255, 128 };
int min_bright[] = { 100, 100, 100, 100, 100, 64 };
// DYNAMIC STATE
// Different states:
// 0 = rising
// 1 = falling
int state[] = { 0, 0, 0, 0, 0, 0 };
// Current "degrees" in a cycle (0-360);
float value[] = { 0, 0, 0, 0, 0, 0 };
// Current value, in # of cycles, to pause before resuming animation
int pause[] = { 0, 100, 200, 300, 400, 0 };
int superbright_dim = 64;
// The time, in ms, between each step
int time_step = 20;
// The factor of the base_speed that the speed will be modified each round
float speed_mod_factor = .5;
void setup()
{
for ( int i = 0; i < 6; i++) {
analogWrite(pwm_pins[i], min_bright[i]);
}
}
void loop()
{
for ( int i = 0; i < 6; i++ ) {
if ( pause[i] > 0 ) {
pause[i]--;
} else {
value[i] += speeds[i];
if ( value[i] > 90 && state[i] == 0 ) {
value[i] = 90;
state[i] = 1; // falling
} else if ( value[i] > 180 ) {
float speed_mod = base_speeds[i] * speed_mod_factor;
// It can get slower, but not faster.
int low = int((base_speeds[i] - speed_mod) * 1000);
int high = int(base_speeds[i] * 1000);
speeds[i] = float(random(low, high)) / 1000.0; // random returns long
value[i] -= 180;
state[i] = 0; // rising
}
analogWrite(pwm_pins[i], sin_inverted(value[i], i));
}
}
delay(time_step);
}
int sin_inverted(float input, int led) {
input = input + 90;
if ( input > 360 ) {
input -= 360;
}
float rads = radians(input);
float output = 1 - abs(sin(rads));
return convert_output(output, led);
}
// Converts something from 0 to 1 into brightnesses
int convert_output(float output, int led) {
return floor(output * (max_bright[led] - min_bright[led])) + min_bright[led];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment