Imagine | Develop | Create
#include <LED_fade.h> #define ActivePin 3 #define allOffDelay 4000 #define NumberOfLights 4 struct Lights { LED_fade led; int inputPin; boolean buttonState; boolean mode; unsigned long lastOn; } light[NumberOfLights] = { {LED_fade(6) ,19,false,false,0}, {LED_fade(9) ,18,false,false,0}, {LED_fade(10),17,false,false,0}, {LED_fade(11),16,false,false,0}, }; void setup() { pinMode(ActivePin,OUTPUT); digitalWrite(ActivePin,HIGH); for(int i = 0; i<NumberOfLights;i++) { pinMode(light[i].inputPin,INPUT); } } void loop() { for(int i = 0; i<NumberOfLights;i++) { bool Read = digitalRead(light[i].inputPin); if(Read != light[i].buttonState) { if(Read == LOW) { light[i].lastOn = millis(); light[i].mode = !light[i].mode; if(light[i].mode == true) { light[i].led.SetFade(255,800); } else { light[i].led.SetFade(0,800); } } light[i].buttonState = Read; } if(Read == HIGH && light[i].lastOn+allOffDelay < millis()) { allOff(); } setlight(); } setlight(); } void setlight() { bool powerOn = false; for(int i = 0; i<NumberOfLights;i++) { light[i].led.run(); if(!light[i].led.isFinished() || light[i].mode == true) { powerOn = true; } } if(powerOn) digitalWrite(ActivePin,LOW); else digitalWrite(ActivePin,HIGH); } void allOff() { for(int i = 0; i<NumberOfLights;i++) { light[i].mode = false; light[i].led.SetFade(0,800); } setlight(); while(1) { bool isActive = false; for(int i = 0; i<NumberOfLights;i++) { setlight(); if(digitalRead(light[i].inputPin) == HIGH) { isActive = true; break; } } if(!isActive) break; } }