// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include "neopixel.h"
#include <math.h>
// IMPORTANT: DEFINE PINS & CONSTANTS
#define button1 D2
#define button2 D3
#define button3 D4
#define button4 D5
#define button5 D6
#define button6 D7
#define PIXEL_PIN D8
#define PIXEL_COUNT 17
#define PIXEL_TYPE WS2812
#define numBoxes 6
#define numLightInBoxes 3
int LIGHTEST = 100;
int pixel_position = 0;
int delaytime;
int buttonPin [] = {button1, button2, button3, button4, button5, button6};
int buttonState [] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
//Initialize once to apply for all functions
int buttonState1; // variables to know if buttons are pressed
int buttonState2;
int buttonState3;
int buttonState4;
int buttonState5;
int buttonState6;
int nightStates[numBoxes] = {0,0,0,0,0,0}; // indicates which light should turn on at night - CHANGE WITH HOW MANY BOXES
long timeSincePress [] = {-1,-1,-1,-1,-1,-1};
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup(){
Serial.begin(96000);
pinMode(button1, INPUT_PULLUP); // sets pin as input
pinMode(button2, INPUT_PULLUP); // sets pin as input
pinMode(button3, INPUT_PULLUP); // sets pin as input
pinMode(button4, INPUT_PULLUP); // sets pin as input
pinMode(button5, INPUT_PULLUP); // sets pin as input
pinMode(button6, INPUT_PULLUP); // sets pin as input
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop(){
nightStates[0] = 0; // start the day with all leds at night to turn off
nightStates[1] = 0;
nightStates[2] = 0;
nightStates[3] = 0;
nightStates[4] = 0;
nightStates[5] = 0;
dayOperations();
nightOperations();
delay(30000); // Duration of the night
}
void dayOperations(void) {
unsigned long init = millis(); // time differences & setting to simulate sunrise to sunset
unsigned long now = millis();
int timeDel = 60000; // Duration of the day
int timeDiff = now - init;
Serial.println("day called: ");
//Serial.println(init);
// Initialize 3 blocks turn on in the morning
uint32_t b = strip.Color(160, 220, 255);
for(int i=0; i < numBoxes; i++) { // 6 represents 6 blocks
strip.setPixelColor((i*numLightInBoxes), b);
strip.setPixelColor((i*numLightInBoxes + 1), b);
strip.show();
}
// Simulates everything happening during the day
while(timeDiff < timeDel) { // 1 minute
// Dims the light one by one as day goes
int value = map(timeDiff, 0, timeDel, 0, numBoxes);
int gradientColor = map(timeDiff-value*(timeDel/numBoxes), 0, timeDel/numBoxes, 255, -1);
uint32_t c = strip.Color(gradientColor*0.63, gradientColor*0.86, gradientColor);
strip.setPixelColor((value*numLightInBoxes), c);
strip.setPixelColor((value*numLightInBoxes + 1), c);
strip.show();
now = millis();
timeDiff = now - init;
//Serial.print(now);
// Check that the current dimming block is pressed or not
uint32_t orange = strip.Color(40, 30, 0); // orange
uint32_t green = strip.Color(10, 40, 0); // green
uint32_t d = strip.Color(0, 0, 0); //dark
for(int i = 0; i <= value; i ++){
int pressMode = checkButton(i);
if(pressMode == 1){
if(i == value){
Serial.println("1 in while");
strip.setPixelColor((i*numLightInBoxes), orange);
strip.setPixelColor((i*numLightInBoxes + 1), orange);
strip.show();
delay(500);
}else{
strip.setPixelColor((i*numLightInBoxes), orange);
strip.setPixelColor((i*numLightInBoxes + 1), orange);
strip.show();
}
}
else if(pressMode == 2){
if(i == value){
strip.setPixelColor((i*numLightInBoxes), green);
strip.setPixelColor((i*numLightInBoxes + 1), green);
strip.show();
delay(500);
}else{
strip.setPixelColor((i*numLightInBoxes), green);
strip.setPixelColor((i*numLightInBoxes + 1), green);
strip.show();
delay(500);
strip.setPixelColor((i*numLightInBoxes), d);
strip.setPixelColor((i*numLightInBoxes + 1), d);
}
}
if(i < value && nightStates[i] == 1){
strip.setPixelColor((i*numLightInBoxes), orange);
strip.setPixelColor((i*numLightInBoxes + 1), orange);
strip.show();
}
}
}
}
void nightOperations(void) {
// turn all the light dim first
uint32_t c1 = strip.Color(0, 0, 0);
for(int i = 0; i < numBoxes; i++){
strip.setPixelColor((i*numLightInBoxes), c1);
strip.setPixelColor((i*numLightInBoxes + 1), c1);
}
strip.show();
// Decide which boxes to turn on at night
uint32_t c2 = strip.Color(255, 165, 0); // Represents orange to turn on the whole night
for(int i = 0; i < numBoxes; i++){
if(nightStates[i] == 1){
strip.setPixelColor((i*numLightInBoxes), c2);
strip.setPixelColor((i*numLightInBoxes + 1), c2);
}
}
strip.show();
}
int checkButton( int index ){
Serial.println("checkButton going");
int buttonReading = digitalRead( buttonPin[index] );
// has changed from the last read
if( buttonReading != buttonState[index] ){
Serial.println("checkButton: state change");
if( buttonReading == LOW && buttonState[index] == HIGH ){
// recorded time when it was pressed.
timeSincePress[index] = millis();
buttonState[index] = buttonReading;
}
if( buttonReading == HIGH && buttonState[index] == LOW ){
long timeNow = millis();
if( timeSincePress[index] + 2000 > timeNow ){
// it's been less than 2 seconds. this is a fast press
nightStates[index] = 1;
timeSincePress[index] = -1;
buttonState[index] = buttonReading;
Serial.println("1 returned in checkButton");
return 1;
}else{
/// otherwise it's a long press
nightStates[index] = 0;
timeSincePress[index] = -1;
buttonState[index] = buttonReading;
Serial.println("2 returned in checkButton");
return 2;
}
}
}
return 0;
}
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. .