Trying to make an alarm that also triggers blinking lights + a stop button

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?

int btn = A0;
int alarm = 8;
//int btnState = 0;

int led1 = 3;
int led2 = 4;
int led3 = 5;
int stopBtn = A1;
int state = 0;

void setup()
{
  pinMode(btn, INPUT);
  pinMode(alarm, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop(){
  if(digitalRead(btn)){
    state ++;
    delay(200);
    while (digitalRead(btn) == HIGH); // esperar hasta que suelte el boton
  }
  
  if (state % 2 == 0) {
    noTone(alarm);
  } else {
    if (digitalRead(stopBtn) == LOW) { // Check if the stop button is not pressed
      sound();
      lights();
    }
  }
}

void lights(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
}

void sound(){
  digitalWrite(alarm, HIGH);
  delay(500);
  digitalWrite(alarm, LOW);
}

Sorry I cannot follow your picture as there is no indication of the button contacts.

Both your sound and light functions look like they need a delay at the end so the activity is on for some time, then off for some time.

Add some serial printing to check why the flow is doing what it should not be.

In setup()

  Serial.begin(115200);

and various places in the loop you can

  Serial.println("Hi Mom!");

print stuff and the value of variables.

Open the serial monitor and set the baud rate to match 115200.

a7

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.

a7

Hello juliojeda

The stopBtn isn´t configured as INPUT.

I recommend configuring the input as INPUT_PULLUP to save external resistors. This simply eliminates the sources of possible malfunctions.

Take a look at the connection notes:

The program still contains great potential for optimisation.
Keyword: Nonblocking timer for periodic tasks. .

True, good eye. Here the default pin mode of INPUT saves @juliojeda.

I agree that INPUT_PULLUP, switches wired between the input pins and ground and using

digitslRead(somePin) == LOW

to recognize a pushed button would be better.

There is no doubt

I had to keep reminding myself the buttons would be very sluggish as the activities are timed with delay calls.

But it does limp along.

a7

I'm sorry but I'm super confused about the changes you guys are helping me make

I'm sorry too.

I haven't had my morning coffee yet and the IDE hasn't started either.

Are you familiar with the ENUM and STRUCT instructions?

haha don't be sorry, you're helping me a lot.
I don't I'm familiar with that...

int btn = A0;
int alarm = 8;
//int btnState = 0;

int led1 = 3;
int led2 = 4;
int led3 = 5;
int stopBtn = A1;
int state = 0;

void setup()
{
  pinMode(btn, INPUT);
  pinMode(alarm, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(stopBtn, INPUT_PULLUP);
}

void loop(){
  if (digitalRead(btn)) {
    state ++;
    delay(50);
    while (digitalRead(btn) == HIGH); // esperar hasta que suelte el boton
    delay(50);
  }
  
  if (state % 2 == 0) {
    noTone(alarm);
  } else {
    if (digitalRead(A1) == LOW) { // Check if the stop button is not pressed
      sound();
      lights();
    }
  }
}

void lights(){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
  delay(500);
}

void sound(){
  tone(alarm, 500, 300);
  delay(50);
  noTone;
  delay(50);
}

the problem still stands, the alarm and the lights are only stopped momentarily when the stopBtn is pushed. when I stop pushing it it continues.

I can see that you have not read and understood the supplied connection instructions.

If you have made a change to the programme, check this change for the logical effects in the programme.

And now I'll take care of my coffee machine, otherwise I won't get my morning coffee.

1 Like

Oh, sry.

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
  • a second button turns off the alarm

a7

I am at a similar but different disadvantage.

a7