#include <mechButton.h>
#include <neoPixel.h>
#include <squareWave.h>
// ********** trigger class **********
class trigger : public mechButton {
public:
trigger(int btnPin,int inLEDNum);
~trigger(void);
void begin(neoPixel* inTheLEDs);
void tripSwitch(void);
void resetSwitch(void);
virtual void takeAction(void);
bool tripped;
neoPixel* theLEDs;
int LEDNum;
};
trigger::trigger(int btnPin,int inLEDNum)
:mechButton(btnPin) {
theLEDs = NULL;
LEDNum = inLEDNum;
tripped = false;
}
trigger::~trigger(void) { }
void trigger::begin(neoPixel* inTheLEDs) {
theLEDs = inTheLEDs;
theLEDs->setPixelColor(LEDNum,&green);
theLEDs->show();
hookup();
}
void trigger::tripSwitch(void) {
theLEDs->setPixelColor(LEDNum,&red);
theLEDs->show();
tripped = true;
}
void trigger::resetSwitch(void) {
theLEDs->setPixelColor(LEDNum,&green);
theLEDs->show();
tripped = false;
}
void trigger::takeAction(void) { if (!getState()) tripSwitch(); }
// ********** alarm class **********
class alarm : public squareWave {
public:
alarm(int inPin);
virtual ~alarm(void);
virtual void pulseOn(void);
virtual void pulseOff(void);
int pin;
};
alarm::alarm(int inPin)
:squareWave(1000,500) { pin = inPin; }
alarm::~alarm(void) { }
void alarm::pulseOn(void) { tone(pin,500); }
void alarm::pulseOff(void){ noTone(pin); }
// ********** The main sketch **********
trigger button[5] {{2,0},{3,1},{4,2},{5,3},{6,4}};
mechButton clearButton(12);
neoPixel theLEDs(5,7);
alarm theAlarm(9);
void setup() {
theLEDs.begin();
for (int i=0;i<5;i++) button[i].begin(&theLEDs);
}
void loop() {
int count;
idle();
if (!clearButton.getState()) {
for (int i=0;i<5;i++) button[i].resetSwitch();
theAlarm.setOnOff(false);
}
count = 0;
if (!theAlarm.running()) {
for (int i=0;i<5;i++) {
if (button[i].tripped) count++;
}
if (count==5) theAlarm.setOnOff(true);
}
}