Skip to content

Instantly share code, notes, and snippets.

@splawski
Last active December 24, 2015 10:59
Show Gist options
  • Save splawski/6787368 to your computer and use it in GitHub Desktop.
Save splawski/6787368 to your computer and use it in GitHub Desktop.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 7); // RX, TX
int remoteOutput = 0;
int previousOutput = 0;
int i = 0;
int pirPin = 4; //digital
int lightPin = 0; //light sensor pin
//int switchPin = 13; //switch pin
int stepTime = 1;
int hRedPin = 3;
int hGreenPin = 5;
int hBluePin = 6;
int lRedPin = 9;
int lGreenPin = 10;
int lBluePin = 11;
byte red = 0;
byte green = 0;
byte blue = 0;
byte redOffset = 0;
byte greenOffset = 0;
byte blueOffset = 0;
volatile int state = LOW;
int lastcode = -1;
boolean onOff = false;
// light parameters
long lightRange = 3600000;
int lightStep = 100;
int stepDirection = 1;
long hue = 0; //value in light cycle
long hueOffset = 0; //value in light cycle
int offset = 0;
float brightness = 100;
float saturation = 100;
void setup()
{
mySerial.begin(9600);
}
void loop()
{
if (mySerial.available())
{
remoteOutput = mySerial.read();
mySerial.flush();
}
switch (remoteOutput)
{
case 49: //dimmer
brightness = constrain(brightness - 2 , 0 , 100);
break;
case 50: //on/off
onOff = !onOff;
Serial.println("test");
break;
case 51://brighter
brightness = constrain(brightness + 2 , 0, 100);
break;
case 52: //Setup returns to default values
lightStep = 30;
stepDirection = 1;
hue= 0; //value in light cycle
brightness = 100;
offset = 0;
break;
case 53: //cycle faster
lightStep = constrain(lightStep / 0.8+1, 1 , 10000);
//Serial.println(lightStep);
break;
case 54: //stop or forward
switch (stepDirection)
{
case 1:
stepDirection = 0;
break;
case 0:
stepDirection = 1;
break;
case -1:
stepDirection = 1;
break;
}
break;
case 55://move light value down
hue= hue- lightRange/100;
if (hue> 0) hue= hue+ lightRange;
break;
case 56://undecided
break;
case 57://move light value up
hue= (hue+ lightRange/100) % lightRange;
break;
case 97://undecided
boolean sTest;
sTest = saturation/100;
saturation = 100 - (100 * sTest);
break;
case 98://cycle slower
lightStep = constrain(lightStep * 0.8-1, 1 , 10000);
//Serial.println(lightStep);
break;
case 99://stop or reverse
switch (stepDirection)
{
case 1:
stepDirection = -1;
break;
case 0:
stepDirection = -1;
break;
case -1:
stepDirection = 0;
break;
}
break;
case 100://undecided
hue= lightRange/9 * 0;
break;
case 101://undecided
hue= lightRange/9 * 1;
break;
case 102://undecided
hue= lightRange/9 * 2;
break;
case 103://undecided
hue= lightRange/9 * 3;
break;
case 104://undecided
hue= lightRange/9 * 4;
break;
case 105://undecided
hue= lightRange/9 * 5;
break;
case 106://undecided
hue= lightRange/9 * 6;
break;
case 107://undecided
hue= lightRange/9 * 7;
break;
case 108://undecided
hue= lightRange/9 * 8;
break;
}
remoteOutput = -1;
//on off condition
if(onOff == 0)
{
if(analogRead(lightPin) <= 12 && digitalRead(pirPin) == HIGH)
{ //was motion detected while it was dark
nightLight(true);
delay(5000);
nightLight(false);
}
else
{
nightLight(false);
}
}
else
{
hue= hue+ lightStep*stepDirection;
hue= hue% lightRange;
HSV_to_RGB(hue /10000, saturation, brightness, red, green, blue);
HSV_to_RGB(hue /10000, saturation, brightness, redOffset, greenOffset, blueOffset);
analogWrite(hRedPin, red);
analogWrite(hGreenPin, green);
analogWrite(hBluePin, blue);
analogWrite(lRedPin , redOffset);
analogWrite(lGreenPin, greenOffset);
analogWrite(lBluePin , blueOffset);
delay(stepTime);
}
}
void nightLight(boolean lightState)
{
byte onState = 100;
byte offState = 0;
if (lightState == true)
{
analogWrite(hRedPin, onState);
analogWrite(hGreenPin, onState);
analogWrite(hBluePin, onState);
}
else
{
analogWrite(hRedPin, offState);
analogWrite(hGreenPin, offState);
analogWrite(hBluePin, offState);
}
analogWrite(lRedPin, offState);
analogWrite(lGreenPin, offState);
analogWrite(lBluePin, offState);
}
void HSV_to_RGB(float h, float s, float v, byte &r, byte &g, byte &b)
{
int i;
float f,p,q,t;
h = constrain(h, 0.0, 360.0);
s = constrain(s, 0.0, 100.0);
v = constrain(v, 0.0, 100.0);
s /= 100;
v /= 100;
if (s == 0) {
// Achromatic (grey)
r = g = b = round(v*255);
return;
}
h /= 60.0;
i = floor(h); // sector 0 to 5
f = h - (float)i; // factorial part of h
p = v * (1.0 - s);
q = v * (1.0 - s * f);
t = v * (1.0 - s * (1 - f));
switch(i) {
case 0:
r = round(255*v);
g = round(255*t);
b = round(255*p);
break;
case 1:
r = round(255*q);
g = round(255*v);
b = round(255*p);
break;
case 2:
r = round(255*p);
g = round(255*v);
b = round(255*t);
break;
case 3:
r = round(255*p);
g = round(255*q);
b = round(255*v);
break;
case 4:
r = round(255*t);
g = round(255*p);
b = round(255*v);
break;
default: // case 5:
r = round(255*v);
g = round(255*p);
b = round(255*q);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment