Skip to content

Instantly share code, notes, and snippets.

@bhollis
Created June 3, 2011 02:38
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/1005767 to your computer and use it in GitHub Desktop.
Save bhollis/1005767 to your computer and use it in GitHub Desktop.
Arduino program for animating LEDs for the GlowBack ceramic sculpture
/*
Arduino program for animating LEDs for the GlowBack ceramic sculpture
http://benhollis.net/blog/2010/02/04/glowback-arduino-powered-glowing-ceramic-creature/
Copyright (C) 2010 by Benjamin Hollis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// 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