// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include <math.h>
#define PIXEL_COUNT 16 // dot count in the strip
#define PIXEL_PIN D0 // pin
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int Color_Hue = 0;
int Color_Br = 0;
int FlagBreathe = 1;
int comingEvent = 0;
int pushEvent(String sEvent);
int effector1 = 0;
int effector2 = 0;
int effector3 = 3;
int servoPinDig = D3;
int servoPin2nd = D1;
int servoPos = 0;
// int t=0; // millis counter
// int ledPin = D0;
Servo myServo;
Servo my2ndServo;
unsigned long tNextEvent = 0;
unsigned long tCurrent = 0;
long tRemain = 0;
struct RGB
{
unsigned char R;
unsigned char G;
unsigned char B;
};
struct HSV
{
double H;
double S;
double V;
};
struct RGB HSVToRGB(struct HSV hsv) {
double r = 0, g = 0, b = 0;
if (hsv.S == 0)
{
r = hsv.V;
g = hsv.V;
b = hsv.V;
}
else
{
int i;
double f, p, q, t;
if (hsv.H == 360)
hsv.H = 0;
else
hsv.H = hsv.H / 60;
i = (int)trunc(hsv.H);
f = hsv.H - i;
p = hsv.V * (1.0 - hsv.S);
q = hsv.V * (1.0 - (hsv.S * f));
t = hsv.V * (1.0 - (hsv.S * (1.0 - f)));
switch (i)
{
case 0:
r = hsv.V;
g = t;
b = p;
break;
case 1:
r = q;
g = hsv.V;
b = p;
break;
case 2:
r = p;
g = hsv.V;
b = t;
break;
case 3:
r = p;
g = q;
b = hsv.V;
break;
case 4:
r = t;
g = p;
b = hsv.V;
break;
default:
r = hsv.V;
g = p;
b = q;
break;
}
}
struct RGB rgb;
rgb.R = r * 255;
rgb.G = g * 255;
rgb.B = b * 255;
return rgb;
}
int movemotor(String pos){
int ipos = pos.toInt();
individualServo(ipos);
delay(1000);
return 1;
}
int cancelthis(String command){
tNextEvent = 0;
strip.setBrightness(0);
strip.show();
individualServo(3);
return 1;
}
void setup() {
strip.begin();
myServo.attach( servoPinDig );
my2ndServo.attach( servoPin2nd );
Particle.function("pushEvent", pushEvent);
Particle.function("movemotor", movemotor);
Particle.function("cancelthis", cancelthis);
Particle.variable("comingEvent", &comingEvent, INT);
Particle.variable("tNextEvent", &tNextEvent, INT);
Particle.variable("effector1", &effector1, INT);
Particle.variable("effector2", &effector2, INT);
Particle.publish("COUNT_EVENT_UPDATE","Current Time: "+String(Time.now()));
tCurrent = Time.now();
}
void lightdot(int extent, int Br = 255, int stColor = 240, int edColor = 0){
int Color_H = constrain(map(extent,0,254,stColor,edColor),0,240);
Br = constrain(Br,-1,255);
struct HSV C_HColor = { Color_H, 1, 1 }; //0-360, 0-1
struct RGB C_RColor = HSVToRGB(C_HColor);
for (int i=0;i<PIXEL_COUNT;i=i+2){ //only use a half of dots, no enough power for all
strip.setPixelColor(i, C_RColor.R,C_RColor.G , C_RColor.B);
}
if (Br < 0){
if (Color_Br <= 10) {
Color_Br = 10;
FlagBreathe = FlagBreathe * -1;
}
if (Color_Br >= 255) {
Color_Br = 255;
FlagBreathe = FlagBreathe * -1;
}
strip.setBrightness(Color_Br);
Color_Br += FlagBreathe * effector3;
} else {
strip.setBrightness(Br);
}
strip.show();
}
void loop() {
tCurrent = Time.now();
tRemain = tNextEvent - tCurrent;
if (tRemain > (-1*3*60) && (tRemain) < (15*60)){
effector1 = React(tRemain,15,3,0,255); // effector act from 15 min remain and gradully go to max value at 5 min remain;
effector2 = React(tRemain,15,3,5,175); // effector act from 7 min remain and gradully go to max value at 0 min remain;
effector3 = React(tRemain,7,0,3,15);
Particle.publish("COUNT_EVENT_UPDATE","Time Remain: "+String(tRemain)+"; EF1: "+String(effector1)+"; EF2: "+String(effector2));
lightdot(effector1,-1);
individualServo(effector2);
delay(100);
}
delay(10);
}
int React(int cT, int sMin, int mMin, int sOut, int mOut){
int sMsec = sMin * 60;
int mMsec = mMin * 60;
return constrain(map(cT,mMsec,sMsec,mOut,sOut),sOut,mOut);
}
void individualServo(int distance){
distance = constrain(distance,3,179);
myServo.write(distance);
my2ndServo.write(distance);
}
int pushEvent(String sEvent){ ///IFTTT PUSH EVENT
// Particle.publish("IFTTT_PUSH",sEvent);
if ((tNextEvent - Time.now()) > (-1*3*60) && (sEvent != "force")) { // new push wont overwrite a in progress, un-expired notificition
return -1;
}
tNextEvent = Time.now() + (15 * 60);
Particle.publish("IFTTT_PUSH","Event: "+sEvent+"; Time: "+String(tNextEvent));
return 1;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .