Browse Source

Implement color wiper

master
Mike C 6 years ago
parent
commit
3f4430a0d9
  1. 84
      src/src.ino

84
src/src.ino

@ -19,9 +19,14 @@
MAX1704 fuelGauge;
float charge_percent;
uint8_t brightness;
uint16_t color;
uint16_t raw_color;
uint8_t color;
bool always_on = false;
Adafruit_NeoPixel neopix = Adafruit_NeoPixel(NEO_PIX_NUM, NEO_PIN, NEO_GRB + NEO_KHZ800);
uint8_t rgb[3]; // index 0 = red / index 1 = green / index 2 = blue
// Various function definitions
uint8_t* hsvToRgb(int h, double s, double v); // See end of file for implementation
void setup() {
Serial.begin(9600);
@ -61,13 +66,18 @@ void setup() {
void loop() {
// Read various values needed
charge_percent = fuelGauge.stateOfCharge(); // Battery level
brightness = map(analogRead(POT_BRIGHT_PIN), 0, 1023, 0, 255); // Brightness (0-1023) mapped to 0-255
color = analogRead(POT_COLOR_PIN); // Color (0-1023 mapped to color wheel)
brightness = map(analogRead(POT_BRIGHT_PIN), 0, 1024, 0, 255); // Brightness (0-1023) mapped to 0-255
raw_color = analogRead(POT_COLOR_PIN); // Color (0-1024)
always_on = (digitalRead(BUTTON_CAP_TOGGLE) == HIGH); // Toggle button pressed == ALWAYS ON
// Convert raw color value to position on a circle and then use the circle position to figure out color on a color wheel
color = map(raw_color, 0, 1024, 0, 360);
hsvToRgb(color, 1, 1); // set rgb byte array
// NeoPixel related
neopix.setPixelColor(0, neopix.Color(255, 255, 255)); // White
neopix.setPixelColor(1, neopix.Color(0, 0, 255)); // Blue
for (uint8_t i=0; i<NEO_PIX_NUM; i++) {
neopix.setPixelColor(i, neopix.Color(rgb[0], rgb[1], rgb[2]));
}
neopix.show();
neopix.setBrightness(brightness);
@ -79,9 +89,71 @@ void loop() {
Serial.println(brightness);
Serial.print("Color: ");
Serial.println(color);
Serial.print("Red: ");
Serial.println(rgb[0]);
Serial.print("Green: ");
Serial.println(rgb[1]);
Serial.print("Blue: ");
Serial.println(rgb[2]);
Serial.print("Always on: ");
Serial.println(always_on);
Serial.println("----------");
delay(1000);
delay(250);
}
}
// Convert degrees on a circle to color values
// http://www.skylark-software.com/2011/01/arduino-notebook-rgb-led-and-color_21.html
uint8_t* hsvToRgb(int h, double s, double v) {
// Make sure our arguments stay in-range
h = max(0, min(360, h));
s = max(0, min(1.0, s));
v = max(0, min(1.0, v));
if(s == 0) {
// Achromatic (grey)
rgb[0] = rgb[1] = rgb[2] = round(v * 255);
return rgb;
}
double hs = h / 60.0; // sector 0 to 5
int i = floor(hs);
double f = hs - i; // factorial part of h
double p = v * (1 - s);
double q = v * (1 - s * f);
double t = v * (1 - s * (1 - f));
double r, g, b;
switch(i) {
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default: // case 5:
r = v;
g = p;
b = q;
}
rgb[0] = round(r * 255.0);
rgb[1] = round(g * 255.0);
rgb[2] = round(b * 255.0);
return rgb;
}

Loading…
Cancel
Save