I try to make a controller to activate a fire sirene.
On different places fire alarm buttons are placed. These buttons have an on/off switch,This means that I have to convert them to a momentery switch.
There is also a test-button ( momentary) and a reset button to stop the sirene.
The sirene has to be active for 2 minutes after a fire button or the test button is pushed.
I have 2 problems. -
- The timer is not working when I use a subroutine.
- when the reset button is pushed the sirene has to stop, so I have to reset the timer.
my sketch :
int kelder=1;
int lastkelder = 1;
int gelijkvl=1;
int lastgelijkvl = 1;
int N1=1;
int lastN1 = 1;
int N2=1;
int lastN2 = 1;
int testknop = 1;
int lasttestknop = 1;
int resetknop = 1;
int sirene = 5;
unsigned long DUUR = 10000; // 10 seconds during test
unsigned long previousMillis = 0;
void setup(){
Serial.begin(9600);
pinMode (6,INPUT_PULLUP);
pinMode (7,INPUT_PULLUP);
pinMode (8,INPUT_PULLUP);
pinMode (9,INPUT_PULLUP);
pinMode (11,INPUT_PULLUP);
pinMode (12,INPUT_PULLUP);
pinMode (A6,INPUT_PULLUP);
pinMode (A5,INPUT_PULLUP);
pinMode (sirene, OUTPUT);
digitalWrite (sirene,LOW);
delay(5);
}
void loop(){
//detectie drukknop kelder
kelder = digitalRead(6);
if (kelder !=lastkelder && kelder == LOW )
{
previousMillis = millis();
Serial.println ("knop gedetecteerd");
digitalWrite( sirene, HIGH);
kelder = lastkelder ;
tijd();
}
//detectie drukknop gelijkvloers
gelijkvl = digitalRead(7);
if (gelijkvl != lastgelijkvl && gelijkvl ==LOW)
{
previousMillis = millis();
tijd();
lastgelijkvl = gelijkvl;
}
// ........................other fire buttons
//detectie test knop
testknop = digitalRead(A5);
if (testknop == LOW)
{
Serial.println ("testknop");
previousMillis = millis();
tijd();
}
//detectie reset knop - make previousMillis bigger than the actual millis() to reset the timer
resetknop = digitalRead(A6);
if (resetknop == LOW)
{
Serial.println ("resetknop");
previousMillis =( millis()+ DUUR +100000);
tijd();
}
}
// timer
void tijd() {
if (millis() - previousMillis> DUUR)
{
digitalWrite( sirene, LOW);
Serial.println ("timer afgelopen");
}
}