timer with reset

hi,

I want to make an alarmsystem with fire switches.

To detect a switch is pressed, I use the edge detection.

Problem is to stop the timer by pressing a reset button. The timer is made with milis().

How can I reset the timer with the reset button so that the alarm stops ?

my actual sketch :

// switches
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;
bool siren_on = false ;

// unsigned long starttijd = millis();
unsigned long DUUR = 10000;
int relaisState = LOW;
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 (A7,INPUT_PULLUP);
pinMode (A6,INPUT_PULLUP);
pinMode (A5,INPUT_PULLUP);

pinMode (sirene, OUTPUT);
digitalWrite (sirene,LOW);
digitalWrite (13,LOW);

delay(5);
}

void loop(){

//detectie drukknop kelder
kelder = digitalRead(6);
//Serial.println (kelder);

if (kelder !=lastkelder && kelder == LOW )
{
previousMillis = millis();
lastkelder = kelder;
Serial.println ("knop gedetecteerd");
digitalWrite( sirene, HIGH);
siren_on = true;

Serial.println ("timer start");
tijd();
}

lastkelder = kelder;

//detectie reset knop
resetknop = digitalRead(A6);
if (resetknop == LOW)
{
Serial.println ("resetknop");

previousMillis =( millis()+ DUUR +10);
tijd();
}
}

// timer
void tijd() {

if (siren_on == true &&(millis() - previousMillis)> DUUR) {
//unsigned long currentmillis = millis();
//if (millis() - previousMillis> DUUR) {
digitalWrite( sirene, LOW);
siren_on = false;
Serial.println ("timer afgelopen");
//return;
}
}

I would assume....

digitalWrite( sirene, LOW);
siren_on = false;

You seem to be trying to “fake” the timer into triggering it by adjusting previousMillis. That’s an odd way to go about it if you ask me, but if you really want to do it that way...

 previousMillis = millis() -DUUR;

Welcome to the Forum. Start by reading the stickies at the top of every section which tell you how to post on the forum and what information to provide. This will help others to help you get the answer you need.


In particular make sure that your code is inside code tags. You can edit a post to include them. If the code is too big attach it as a file or create a smaller code that is able to compile and incudes the problem (do not use snippets). It is preferable if you auto format your code before loading it as this will make it easier to read (use CTRL T in the IDE).

Schematics are often useful. You can draw them using a pencil and paper, or use an online tool and then post a picture of it. To post a picture upload it to an image hosting site and then put the link between image tags

If you do these simple things you will get Karma and are much more likely to get the answers you are looking for.

Have a look at the tutorials in those stickies in particular millis and state machines. They will get you there. You are currently solving the wrong problem.