Having some trouble with a smartlamp I’ve created. It keeps freezing - i.e. won’t change function after 5 minutes on one function. Any suggestions?
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
SYSTEM_MODE(AUTOMATIC); //tell particle to use automatic WIFI mode.
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 16
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812B
int buttonstate = 0;
int brightness = 0;
int i=0;
int n=0;
int button=D0;
int flashes=5;
//tell the photon about the strip we're using. In this case a 12 pixel ring
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
//The setup function runs once.
void setup()
{
strip.begin();
strip.show(); // Initialize all pixels to 'off'
allpixelsoff();
Particle.subscribe("smartlight", myHandler);
Particle.function("smartlight", smartlight);
RGB.control(true); // take control of onboard RGB led
}
/*void loop(){
RGB.color(0,50,0);
checkbutton();
if (buttonstate>0) lampon();
else if (buttonstate==0) allpixelsoff();
}*/
int smartlight(String command)
{
if(command == "lamp")
{buttonstate=buttonstate+1;
lampon();
return 1;
}
if(command == "allpixelsoff")
{buttonstate=buttonstate=0;
allpixelsoff();
return 1;
}
if(command == "doorbell")
{doorbell();
return 1;
}
if(command == "redmood")
{mood_Red();
return 1;
}
if(command == "greenmood")
{mood_Green();
return 1;
}
if(command == "yellowmood")
{mood_Yellow();
return 1;
}
if (command == "purplemood")
{mood_Purple();
return 1;
}
else if(command == "gmail")
{gmail();
return 1;
}
else if(command == "bright")
{levelthree();
return 1;
}
else if(command == "dim")
{levelone();
return 1;
}
else if(command == "phone")
{phone();
return 1;
}
else return -1;
}
void checkbutton(){
if(button==HIGH){
while (button==HIGH) {}
buttonstate=buttonstate+1;
}
if (buttonstate>3){buttonstate=0;}
}
void lampon(){ //switches lamp on to one of three levels or off.
if (buttonstate>3){buttonstate=0;}
if (buttonstate==1){ brightness=100;}
else if (buttonstate==2){ brightness=180;}
else if (buttonstate==3){ brightness=250;}
else if (buttonstate==0){ brightness=0;}
delay(100);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(brightness, brightness, brightness));
};
strip.show();
}
void allpixelsoff() { //this switches all the pixels off
delay(100);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.setPixelColor(i, strip.Color(0, 0, 0));
};
strip.show();
}
void levelone() { //this switches to level one
delay(100);
allpixelsoff();
delay(100);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(100, 100, 100));
};
strip.show();
}
void leveltwo() { //this switches to level two
delay(100);
allpixelsoff();
delay(100);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(180, 180, 180));
};
strip.show();
}
void mood_Red() { //this switches to level two
allpixelsoff();
delay(100);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(250, 0, 0));
};
strip.show();
delay(500);
}
void levelthree() { //this switches to level 3
delay(100);
allpixelsoff();
delay(100);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(250, 250, 250));
};
strip.show();
}
void doorbell() {
allpixelsoff();
delay(100);
for(n=0; n<flashes; n++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 250));
};
strip.show();
delay(500);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 50));
}
strip.show();
delay(500);
}
allpixelsoff();
lampon();
}
void phone() {
allpixelsoff();
delay(100);
for(n=0; n<flashes; n++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 250, 0));
};
strip.show();
delay(500);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 100, 0));
}
}
strip.show();
delay(500);
allpixelsoff();
lampon();
}
void gmail() {
allpixelsoff();
delay(100);
for(n=0; n<flashes; n++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(250, 0, 0));
};
strip.show();
delay(500);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(100, 0, 0));
}
strip.show();
delay(500);
allpixelsoff();
lampon();
}
}
void mood_Yellow() {
allpixelsoff();
delay(100);
for (n=0; n<flashes; n++) {
for (i=0; i<strip.numPixels(); i++ ){
strip.setPixelColor(i, strip.Color(250,250,0));
};
strip.show();
delay(500);
}
}
void mood_Green() {
allpixelsoff();
delay(100);
for (n=0; n<flashes; n++) {
for (i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, strip.Color(0,0, 250));
};
strip.show();
delay(500);
}
}
void mood_Purple() {
allpixelsoff();
delay(100);
for (n=0; n<flashes; n++) {
for (i=0; i<strip.numPixels(); i++ ){
strip.setPixelColor(i, strip.Color(250,0,250));
};
strip.show();
delay(500);
}
}
// allpixelsoff();
// lampon();
void myHandler(const char *event, const char *data)
{
if (strcmp(data,"doorbell")==0) {
doorbell();
}
else if (strcmp(data,"phone")==0) {
phone();
}
}