Created
April 6, 2013 01:09
-
-
Save miek/5324079 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* | |
* BlinkM connections to Arduino | |
* PWR - -- gnd -- black -- Gnd | |
* PWR + -- +5V -- red -- 5V | |
* I2C d -- SDA -- green -- Analog In 4 | |
* I2C c -- SCK -- blue -- Analog In 5 | |
* | |
* There are 27 BlinkMs are addressed from 10..36. | |
*/ | |
#include "Wire.h" | |
#include "BlinkM_funcs.h" | |
const int num_blinkms = 27; | |
const int blinkm_start_addr = 10; | |
// msec delay between frames | |
const int wait_time = 40; | |
// Flip direction after x frames (set to 0 to disable) | |
const int flip = 35; | |
// Speed for fadeout | |
const int fade_speed = 5; | |
void setup() | |
{ | |
Serial.begin(19200); | |
BlinkM_begin(); | |
delay(1000); | |
// set all BlinkMs to known state | |
BlinkM_stopScript( 0 ); | |
BlinkM_setFadeSpeed( 0, 10 ); | |
BlinkM_fadeToRGB( 0, 0,0,0); // fade to black | |
Serial.print("BlinkMCylon ready\n"); | |
delay(1000); | |
} | |
bool clockwise = true; | |
long last_time = 0; | |
int current_led = blinkm_start_addr; | |
int flip_count = 0; | |
void loop() | |
{ | |
if (millis() - last_time > wait_time) { | |
int addr = current_led; | |
// Set new LED to full brightness instantly | |
BlinkM_setFadeSpeed(addr, 0xFF); | |
BlinkM_fadeToHSB(addr, addr_to_hue(addr), 0xFF, 0xFF); | |
addr = clamp_addr(clockwise ? addr - 1 : addr + 1); | |
// Slow down trailing LED + fade to off | |
BlinkM_setFadeSpeed(addr, fade_speed); | |
BlinkM_fadeToHSB(addr, addr_to_hue(addr), 0xFF, 0x0); | |
current_led = clamp_addr(clockwise ? current_led + 1 : current_led - 1); | |
if (flip) { | |
flip_count++; | |
if (flip_count > flip) { | |
// Flip sooner in one direction, so the direction change point moves | |
flip_count = clockwise ? 5 : 0; | |
clockwise = !clockwise; | |
} | |
} | |
last_time = millis(); | |
} | |
} | |
byte clamp_addr(byte addr) { | |
if (addr >= blinkm_start_addr + num_blinkms) return blinkm_start_addr; | |
if (addr < blinkm_start_addr) return blinkm_start_addr + num_blinkms - 1; | |
return addr; | |
} | |
byte addr_to_hue(byte addr) { | |
return ((addr - blinkm_start_addr) * 0xFF) / num_blinkms; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment