Created
March 24, 2017 08:26
-
-
Save howardgrigg/c8602b20774062e43b492c08cda0b9a5 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
#include <FastLED.h> | |
#include <SoftwareSerial.h> | |
SoftwareSerial mySerial(4, 5); // RX, TX | |
#define REDPIN 10 | |
#define GREENPIN 11 | |
#define BLUEPIN 9 | |
#define BUTTONPIN 12 | |
const byte numChars = 14; | |
char SerialHSV[numChars]; // an array to store the received data | |
boolean newData = false; | |
int currentHue; | |
int currentSat; | |
int currentVal; | |
int oldHue; | |
int oldSat; | |
int oldVal; | |
int targetHue; | |
int targetSat; | |
int targetVal; | |
int totalSteps; | |
int currentStep; | |
boolean bluetooth = true; | |
int buttonState = 0; | |
int prevButtonState = 0; | |
void showAnalogRGB( const CRGB& rgb) | |
{ | |
analogWrite(REDPIN, rgb.r ); | |
analogWrite(GREENPIN, rgb.g ); | |
analogWrite(BLUEPIN, rgb.b ); | |
} | |
void colorBars() | |
{ | |
showAnalogRGB( CRGB::Red ); delay(250); | |
showAnalogRGB( CRGB::Green ); delay(250); | |
showAnalogRGB( CRGB::Blue ); delay(250); | |
showAnalogRGB( CRGB::Black ); delay(250); | |
} | |
void recvWithEndMarker() { | |
static byte ndx = 0; | |
char endMarker = '\n'; | |
char rc; | |
while (Serial.available() > 0 && newData == false) { | |
rc = Serial.read(); | |
if (rc != endMarker) { | |
SerialHSV[ndx] = rc; | |
ndx++; | |
if (ndx >= numChars) { | |
ndx = numChars - 1; | |
} | |
} | |
else { | |
SerialHSV[ndx] = '\0'; // terminate the string | |
ndx = 0; | |
newData = true; | |
} | |
} | |
} | |
void updateData(){ | |
if (newData == true) { | |
String StringHSV = String(SerialHSV); | |
if(StringHSV.length() == 13){ | |
oldHue = currentHue; | |
oldSat = currentSat; | |
oldVal = currentVal; | |
targetHue = StringHSV.substring(0,3).toInt(); | |
targetSat = StringHSV.substring(3,6).toInt(); | |
targetVal = StringHSV.substring(6,9).toInt(); | |
totalSteps = StringHSV.substring(9,14).toInt(); | |
currentStep = 0; | |
Serial.println("New targets: Hue:" + String(targetHue) + " Saturation:" + String(targetSat) + " Value:" + String(targetVal) + " Steps:" + String(totalSteps)); | |
}; | |
newData = false; | |
} | |
} | |
void showColor(){ | |
if (currentStep != totalSteps + 1){ | |
// Serial.println("Fadeinput: "+String(oldHue)+", "+String(currentHue)+", "+String(targetHue)+", "+String(currentStep)+", "+String(totalSteps)); | |
currentHue = fadeStep(oldHue, currentHue, targetHue, currentStep, totalSteps); | |
currentSat = fadeStep(oldSat, currentSat, targetSat, currentStep, totalSteps); | |
currentVal = fadeStep(oldVal, currentVal, targetVal, currentStep, totalSteps); | |
currentStep++; | |
// Serial.println(String(currentHue)+", "+String(currentSat)+", "+String(currentVal)+", Step:"+String(currentStep)); | |
showAnalogRGB( CHSV( currentHue, currentSat, currentVal) ); | |
} | |
} | |
int fadeStep(float oldLevel, int currentLevel, float targetLevel, int currentStep, int totalSteps){ | |
// Serial.println(String(oldLevel)+", "+String(currentLevel)+", "+String(targetLevel)+", "+String(currentStep)+", "+String(totalSteps)); | |
float change = (targetLevel-oldLevel) / totalSteps; | |
float newLevel = oldLevel + (change * currentStep); | |
int newLevelInt = round(newLevel); | |
return newLevelInt; | |
} | |
void blueFade(){ | |
for(int x = 0; x < 100; x++){ | |
showAnalogRGB( CHSV(128,255,x) ); | |
delay(4); | |
} | |
for(int x = 99; x < 100 && x > 1; x--){ | |
showAnalogRGB( CHSV(128,255,x) ); | |
delay(4); | |
} | |
} | |
void setup() { | |
pinMode(REDPIN, OUTPUT); | |
pinMode(GREENPIN, OUTPUT); | |
pinMode(BLUEPIN, OUTPUT); | |
pinMode(BUTTONPIN, INPUT); | |
Serial.begin(9600); | |
mySerial.println("Connected via bluetooth to Howard's Light thingy"); | |
Serial.println("Connected via USB to Howard's Light thingy"); | |
mySerial.begin(9600); | |
// colorBars(); | |
blueFade(); | |
showAnalogRGB( CHSV(0,0,0) ); | |
} | |
void loop() { | |
if(bluetooth == true){ | |
recvWithEndMarker(); | |
updateData(); | |
showColor(); | |
}else{ | |
showAnalogRGB( CRGB(255,80,10) ); | |
} | |
buttonState = digitalRead(BUTTONPIN); | |
if(buttonState != prevButtonState && buttonState == 1){ | |
if(bluetooth == true){ | |
bluetooth = false; | |
Serial.println("Bluetooth off"); | |
}else{ | |
bluetooth = true; | |
Serial.println("Bluetooth on"); | |
blueFade(); | |
showAnalogRGB( CHSV(0,0,0) ); | |
} | |
} | |
prevButtonState = buttonState; | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment