Hello, I'm not sure why it's not working. after pressing the on button for the first time it just never stops repeating the alarm + blinking lights no matter if I press the stop button. help please?
I built your circuit. And added the delays I suggested.
You are catching a bounce with your primitive debouncing logic.
Try
if (digitalRead(btn)) {
state ++;
delay(50);
while (digitalRead(btn) == HIGH); // esperar hasta que suelte el boton
delay(50);
}
The delay can be shorter, and you need one after the switch reads LOW, otherwise the loop comes around so fast (if the state has turned off the alarm) that the first read sees HIGH again and turns the alarm right back on.
I fixed the problem your sketch has, I did not fix it to work in the manner you now describe..
First, let us be sure your sketch works as it appeared to, something you were close to but had a bit wrong.
If you press and hold the btn button for two seconds, the alarm activities should start if they were not operating, and stop if they were when you release the button.
On and off with the btn button.
If you press and hold the stopBtn, while you are holding the button the alarm activities will be suppressed, should they have been operating, and resume such operation when you release the stopBtn.
Does this agree with your observations?
I can't test your version at the moment, but that is what my version does, and that is what the code you originally supplied seemed to be aiming for.
Now if the code you posted does what I said and isn't what you want explain again how you want the two buttons to work.
There are many possibilities; no one wants to guess and we all have ideas about how two buttons and an alarm LED and buzzer system might work.
One example, adjust it to express your desire:
a button when pressed and released turns on the alarm. Pressing again does nothing