Created
April 3, 2015 21:53
-
-
Save mplewis/8183d65a4c82ceb19707 to your computer and use it in GitHub Desktop.
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
#include <Adafruit_NeoPixel.h> | |
#include "HSBColor.h" | |
// The pin that is connected to the NeoPixels | |
#define NEOPIXEL_PIN 1 | |
// The pin connected to the button | |
#define BUTTON_PIN 2 | |
// The amount of LEDs in the NeoPixels | |
#define NUM_PIXELS 7 | |
// The tick delays for rainbow and random fades, in ms | |
#define RAINBOW_DELAY 50 | |
#define RANDOM_DELAY 50 | |
// The amount of time to delay between random fades, in ms | |
#define RANDOM_STALL 60000 | |
// NeoPixel brightness, from 0 to 100. 100 is really high! | |
#define BRIGHTNESS 50 | |
// Color saturation, from 0 to 100 | |
#define SATURATION 75 | |
/* LedReading is the type we get when we call Bean.getLedValues(); | |
* For example, to get the amount of red in the Bean's LED, | |
* we use ledColor.red to get a value from 0 to 255 | |
*/ | |
LedReading ledColor; | |
// previousLedColor will be used to check if the LED's color has changed | |
LedReading previousLedColor; | |
bool usedSandbox = false; | |
bool setOnce = false; | |
// rainbowHue is a hue from 0 to 359 | |
int rainbowHue = 0; | |
/* currHue is the current hue from 0 to 359 | |
* targetHue is the hue being faded to from 0 to 359 | |
*/ | |
int currHue = 0; | |
int targetHue = 0; | |
// stallUntil is the millis() at which to resume fading | |
long stallUntil = 0; | |
/* mode represents the current LED mode | |
* 0: Random fade | |
* 1: Rainbow fade | |
* 2: Static color from sandbox | |
* 3: Off | |
*/ | |
int mode = 0; | |
// currButton and lastButton are the state of the pressed button | |
bool currButton; | |
bool lastButton; | |
// Set up the NeoPixel library | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, | |
NEO_GRB + NEO_KHZ800); | |
void setPixels(int hue, int saturation, int brightness) | |
{ | |
int rgb[3]; | |
H2R_HSBtoRGB(hue, saturation, brightness, rgb); | |
for (int i = 0; i < NUM_PIXELS; i++) { | |
pixels.setPixelColor(i, pixels.Color(rgb[0], rgb[1], rgb[2])); | |
pixels.show(); | |
} | |
#ifdef DEBUG_LED | |
Serial.print(hue, DEC); | |
Serial.print(' '); | |
Serial.print(saturation, DEC); | |
Serial.print(' '); | |
Serial.print(brightness, DEC); | |
Serial.print(" --> "); | |
Serial.print(rgb[0]); | |
Serial.print(' '); | |
Serial.print(rgb[1]); | |
Serial.print(' '); | |
Serial.println(rgb[2]); | |
#endif | |
} | |
void setPixelsRGB(int r, int g, int b) | |
{ | |
for (int i = 0; i < NUM_PIXELS; i++) { | |
pixels.setPixelColor(i, pixels.Color(r, g, b)); | |
pixels.show(); | |
} | |
} | |
void setFromRandomFade() | |
{ | |
int rgb[3]; | |
if (currHue == targetHue) { | |
if (millis() < stallUntil) return; | |
bool done = false; | |
while ( ! done ) { | |
targetHue = random(360); | |
if (abs( currHue - targetHue ) > 120 && | |
abs( currHue - (targetHue + 360) ) > 120 && | |
abs( (currHue + 360) - targetHue ) > 120) { | |
done = true; | |
} | |
Serial.print(millis(), DEC); | |
Serial.print(" Target hue: "); | |
Serial.println(targetHue, DEC); | |
} | |
stallUntil = millis() + RANDOM_STALL; | |
} else { | |
currHue++; | |
currHue = currHue % 360; | |
setPixels(currHue, SATURATION, BRIGHTNESS); | |
} | |
Bean.sleep(RANDOM_DELAY); | |
} | |
void setFromRainbow() | |
{ | |
rainbowHue++; | |
rainbowHue = rainbowHue % 360; | |
setPixels(rainbowHue, SATURATION, BRIGHTNESS); | |
Bean.sleep(RAINBOW_DELAY); | |
} | |
void setFromSandbox() | |
{ | |
// Default color if we haven't read the sandbox LED | |
if ( !usedSandbox && !setOnce ) { | |
setPixels(180, SATURATION, BRIGHTNESS); | |
setOnce = true; | |
return; | |
} | |
// Check if the Bean is connected to another device | |
// to keep the NeoPixels from turning off when it's disconnected | |
if (Bean.getConnectionState()) { | |
// Get the values from the Bean's onboard LED | |
ledColor = Bean.getLed(); | |
// Ensure color isn't black (after a disconnection) | |
if (ledColor.red == 0 && ledColor.green == 0 && ledColor.blue == 0) return; | |
// Check if the color has changed | |
if (ledColor.red != previousLedColor.red || | |
ledColor.green != previousLedColor.green || | |
ledColor.blue != previousLedColor.blue) { | |
// Set the NeoPixels to the same color as the Bean's LED | |
setPixelsRGB(ledColor.red, ledColor.green, ledColor.blue); | |
// Update previousLedColor for the next loop | |
previousLedColor = ledColor; | |
} | |
usedSandbox = true; | |
// Sleep to avoid the flickers | |
Bean.sleep(50); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(57600); | |
// Initialize the NeoPixels | |
pixels.begin(); | |
// Set up the button | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
} | |
void loop() { | |
if (mode == 0) { | |
setFromRandomFade(); | |
} else if (mode == 1) { | |
setFromRainbow(); | |
} else if (mode == 2) { | |
setFromSandbox(); | |
} else { | |
setPixels(0, 0, 0); // Turn them off! | |
} | |
currButton = digitalRead(BUTTON_PIN); | |
if (currButton == LOW && lastButton == HIGH) { | |
mode++; | |
mode = mode % 4; | |
Serial.print("Mode "); | |
Serial.println(mode, DEC); | |
setOnce = false; | |
stallUntil = 0; | |
} | |
lastButton = currButton; | |
} |
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
#include "HSBColor.h" | |
/* HSB to RGB conversion function | |
INPUT PARAMETERS: Hue values should range from 0 to 360, Saturation and Brightness values should range from 0 to 100 | |
Colors pointer should resolve to an array with 3 elements | |
RGB values are passed back using via the array. Each value will range between 0 and 255 | |
*/ | |
void H2R_HSBtoRGB(int hue, int sat, int bright, int* colors) { | |
// constrain all input variables to expected range | |
hue = constrain(hue, 0, 360); | |
sat = constrain(sat, 0, 100); | |
bright = constrain(bright, 0, 100); | |
// define maximum value for RGB array elements | |
float max_rgb_val = H2R_MAX_RGB_val; | |
// convert saturation and brightness value to decimals and init r, g, b variables | |
float sat_f = float(sat) / 100.0; | |
float bright_f = float(bright) / 100.0; | |
int r, g, b; | |
// If brightness is 0 then color is black (achromatic) | |
// therefore, R, G and B values will all equal to 0 | |
if (bright <= 0) { | |
colors[0] = 0; | |
colors[1] = 0; | |
colors[2] = 0; | |
} | |
// If saturation is 0 then color is gray (achromatic) | |
// therefore, R, G and B values will all equal the current brightness | |
if (sat <= 0) { | |
colors[0] = bright_f * max_rgb_val; | |
colors[1] = bright_f * max_rgb_val; | |
colors[2] = bright_f * max_rgb_val; | |
} | |
// if saturation and brightness are greater than 0 then calculate | |
// R, G and B values based on the current hue and brightness | |
else { | |
if (hue >= 0 && hue < 120) { | |
float hue_primary = 1.0 - (float(hue) / 120.0); | |
float hue_secondary = float(hue) / 120.0; | |
float sat_primary = (1.0 - hue_primary) * (1.0 - sat_f); | |
float sat_secondary = (1.0 - hue_secondary) * (1.0 - sat_f); | |
float sat_tertiary = 1.0 - sat_f; | |
r = (bright_f * max_rgb_val) * (hue_primary + sat_primary); | |
g = (bright_f * max_rgb_val) * (hue_secondary + sat_secondary); | |
b = (bright_f * max_rgb_val) * sat_tertiary; | |
} | |
else if (hue >= 120 && hue < 240) { | |
float hue_primary = 1.0 - ((float(hue)-120.0) / 120.0); | |
float hue_secondary = (float(hue)-120.0) / 120.0; | |
float sat_primary = (1.0 - hue_primary) * (1.0 - sat_f); | |
float sat_secondary = (1.0 - hue_secondary) * (1.0 - sat_f); | |
float sat_tertiary = 1.0 - sat_f; | |
r = (bright_f * max_rgb_val) * sat_tertiary; | |
g = (bright_f * max_rgb_val) * (hue_primary + sat_primary); | |
b = (bright_f * max_rgb_val) * (hue_secondary + sat_secondary); | |
} | |
else if (hue >= 240 && hue <= 360) { | |
float hue_primary = 1.0 - ((float(hue)-240.0) / 120.0); | |
float hue_secondary = (float(hue)-240.0) / 120.0; | |
float sat_primary = (1.0 - hue_primary) * (1.0 - sat_f); | |
float sat_secondary = (1.0 - hue_secondary) * (1.0 - sat_f); | |
float sat_tertiary = 1.0 - sat_f; | |
r = (bright_f * max_rgb_val) * (hue_secondary + sat_secondary); | |
g = (bright_f * max_rgb_val) * sat_tertiary; | |
b = (bright_f * max_rgb_val) * (hue_primary + sat_primary); | |
} | |
colors[0]=r; | |
colors[1]=g; | |
colors[2]=b; | |
} | |
} | |
void H2R_HSBtoRGBfloat(float hue, float sat, float bright, int* colors) { | |
if (hue > 1) hue = 1.0; | |
if (sat > 1) sat = 1.0; | |
if (bright > 1) bright = 1.0; | |
H2R_HSBtoRGB(hue*360.0, sat*100.0, bright*100.0, colors); | |
} | |
/* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. | |
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send | |
a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. | |
*/ |
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
/* | |
HSB Color, | |
Library that converts HSB color values to RGB colors. | |
Created by Julio Terra, June 4, 2011. | |
Header File Name: HSBColor.h | |
Implementation File Name: HSBColor.h | |
*/ | |
#ifndef HSBColor_h | |
#define HSBColor_h | |
#if defined(ARDUINO) && ARDUINO >= 100 | |
#include "Arduino.h" | |
#else | |
#include "WProgram.h" | |
#endif | |
#define H2R_MAX_RGB_val 255.0 | |
void H2R_HSBtoRGB(int, int, int, int*); | |
void H2R_HSBtoRGBfloat(float, float, float, int*); | |
#endif | |
/* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. | |
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send | |
a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment