Created
March 30, 2016 15:41
-
-
Save renoeno/545d193595c799a22c1eb5b93cfa6a3c 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
/* | |
* Do Park | |
* Reno Almeida | |
OCAD University | |
Created on March 30, 2016 | |
*/ | |
//adding neopixel library. | |
#include "neopixel/neopixel.h" | |
#include "application.h" | |
//assigning the input D0 to constant PIN | |
#define PIN D0 | |
//defining number of pixels that the neopixel ring has and below the type. | |
#define NUMPIXELS 16 | |
#define PIXEL_TYPE WS2812B | |
//creating neopixel object. | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, PIXEL_TYPE); | |
int delayval = 500; | |
//variables that hold real time data coming from javascript application. Sunset, sunrise and current time | |
//and percentage of clouds at a certain time and day. | |
int sunsetTime; | |
int sunriseTime; | |
int currentTime; | |
int clouds; | |
//variable that controls the colour displayed. below are variables for red, green and blue colours. | |
int mode = 0; | |
int r, g, b; | |
bool sunrise = false; // check sunset, and sunrise | |
bool sunset = false; | |
bool AMPM = false; // check if the time is AM or PM | |
void setup() { | |
//initiating neopixel ring | |
pixels.begin(); | |
//assigning all functions that are called from the javascript file, so they can be called outside this application. | |
Particle.function("sunset", getSunset); | |
Particle.function("sunrise", getSunrise); | |
Particle.function("time", getTime); | |
Particle.function("clouds", getClouds); | |
Particle.function("shine", blinkRing); | |
//really don't remember what this is about, but gonna leave for the sake of everything working fine. | |
Particle.variable("sunsetTime", &sunsetTime, INT); | |
//initiating variables. | |
r = 0; | |
g = 0; | |
b = 0; | |
clouds = 0; | |
sunsetTime = 0; | |
sunriseTime = 0; | |
currentTime = 0; | |
} | |
void loop() { | |
//calling functions that change the colours according to current time. since any function created has | |
//to receive a String argument, I'm sending a random argument. | |
AMPM_Checker("nanana"); | |
Sunrise_Checker("nanana"); | |
Sunset_Checker("nanana"); | |
//changing the mode (that controls colours) according to current state. | |
if(!sunrise && !sunset ){ | |
mode = 0; | |
} | |
else if(sunrise && !sunset){ | |
mode = 1; | |
} | |
else if(!sunrise && sunset){ | |
mode = 2; | |
} | |
else if(!sunrise && !sunset){ | |
mode = 3; | |
} | |
} | |
int blinkRing(String command){ | |
/* | |
if(currentTime <= 12){ | |
if(currentTime > sunriseTime){ | |
r = map(currentTime - sunriseTime, 12, 0, 110, 30) - clouds/2; | |
g = map(currentTime - sunriseTime, 12, 0, 145, 90) - clouds/2; | |
b = map(currentTime - sunriseTime, 12, 0, 195, 164) - clouds/2; | |
}else{ | |
r = 10; | |
g = 40; | |
b = 80; | |
} | |
}else{ | |
if(sunsetTime > currentTime){ | |
r = map(sunsetTime - currentTime, 12, 0, 75, 150) - clouds/2; | |
g = map(sunsetTime - currentTime, 12, 0, 140, 185) - clouds/2; | |
b = map(sunsetTime - currentTime, 12, 0, 220, 235) - clouds/2; | |
}else{ | |
r = 15; | |
g = 45; | |
b = 90; | |
} | |
} | |
*/ | |
//lighting the neopixel ring. | |
for(int i=0;i<NUMPIXELS;i++){ | |
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 | |
if(mode == 0) | |
{ | |
pixels.setPixelColor(i, pixels.Color(0, b-80, b)); | |
}// Moderately bright green color. | |
if(mode == 1) | |
{ | |
pixels.setPixelColor(i, pixels.Color(255, b-80, 0)); | |
} | |
if(mode == 2) | |
{ | |
pixels.setPixelColor(i, pixels.Color(255, b+40, 0)); | |
} | |
if(mode == 3) | |
{ | |
pixels.setPixelColor(i, pixels.Color(0, b-80, b)); | |
} | |
pixels.show(); // This sends the updated pixel color to the hardware. | |
delay(10); // Delay for a period of time (in milliseconds). | |
} | |
} | |
//function that receives sunset time coming from javascript application. | |
int getSunset(String command){ | |
sunsetTime = command.toInt(); | |
return 0; | |
} | |
//function that receives sunrise time coming from javascript application. | |
int getSunrise(String command){ | |
sunriseTime = command.toInt(); | |
return 0; | |
} | |
//function that receives current time coming from javascript application. | |
int getTime(String command){ | |
currentTime = command.toInt(); | |
return 0; | |
} | |
//function that receives quantity of clouds coming from javascript application. | |
int getClouds(String command){ | |
clouds = command.toInt(); | |
return 0; | |
} | |
//fuction that checks if current time it before or after sunrise. | |
int Sunrise_Checker(String command) | |
{ | |
if(sunriseTime == currentTime && abs(sunriseTime - currentTime)<=5) | |
{ | |
sunrise = true; | |
} | |
if(sunrise && abs(sunriseTime - currentTime)>5) | |
{ | |
sunrise = false; | |
} | |
return 0; | |
} | |
//fuction that checks if current time it before or after sunset. | |
int Sunset_Checker(String command) | |
{ | |
if(sunsetTime == currentTime && abs(sunsetTime - currentTime)<=3) | |
{ | |
sunset = true; | |
} | |
if(sunset && abs(sunsetTime - currentTime)>3) | |
{ | |
sunset = false; | |
} | |
return 0; | |
} | |
//fuction that checks if current time it before or after noon. | |
int AMPM_Checker(String command) | |
{ | |
if(currentTime>=12)AMPM = true; | |
if(currentTime<12)AMPM = false; | |
if(AMPM == false) | |
{ | |
b = map(currentTime%12,0,12,80,255); // map the value 0 - 12 to 80 - 255 | |
} | |
else if(AMPM == true) | |
{ | |
b = map(currentTime%12,0,12,255,80); // map the value 0 - 12 to 255 - 80 (reverse) | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment