buttonState refreshing

Hello,

I am having some issues in refreshing the buttonState variable.
I tried both with while loop and with delay.
Here is the code:

boolean checkState() {
buttonState = digitalRead(button);

if (buttonState == HIGH) {
  activateAlarmSound();
  delay(2000);
  if (buttonState == LOW) { //is buttonState still high after the delay?
    deactivateAlarmSound(); //turn-off sound if not
  }
  else {
    initialize = true; //activate the alarm
    }
  }
else if (buttonState == LOW) {
  initialize = false; //no need to initialize
  deactivateAlarmSound();
}
return initialize;
}

Basically, I want an activation sound in case the buttonState is high, and then after a 2000 ms delay, check if the buttonState is still high or not.
Problem is, it completely skips the second check, after the delay...

buttonstate is a variable, and will only change if you assign something new to it.

So you would need to add another buttonState = digitalRead(button); statement before checking it again.

if (buttonState == HIGH) {
  activateAlarmSound();
  delay(2000);
  buttonState = digitalRead(button);  // Check the button again
  if (buttonState == LOW) { //is buttonState still high after the delay?
    deactivateAlarmSound(); //turn-off sound if not
  }

And what happens if after the 2 seconds the user still pushing the button? The sound will remain forever?

TRex:
buttonstate is a variable, and will only change if you assign something new to it.

So you would need to add another buttonState = digitalRead(button); statement before checking it again.

if (buttonState == HIGH) {

activateAlarmSound();
  delay(2000);
  buttonState = digitalRead(button);  // Check the button again
  if (buttonState == LOW) { //is buttonState still high after the delay?
    deactivateAlarmSound(); //turn-off sound if not
  }

Oh yes, that was it, rookie mistake, thanks.

And yes, the sound loop is supposed to be repeated for as long as the button is pressed.

You don't understand. I put the question in a different way. If the used push the button, releases the button for 1 sec. then pushes the button again for 2 more sec. and then releases the button. What happens? The sound is on or off? If is on for how long?
Is that the behaviour that you what?

Currently, I am making it so if the user pushes the button, you get an alarmActivated sound, then it waits for few miliseconds, to check if it is still on, then it triggers the alarm sound.
In case the button is released during the delay or during the alarm sound phase, the alarm will stop, and will play an alarmDeactivated sound.

Unicus:
Oh yes, that was it, rookie mistake, thanks.

And yes, the sound loop is supposed to be repeated for as long as the button is pressed.

then you may want to simplify as follows:

boolean checkState() 
{
  if (digitalRead(button) == LOW)
  {
    initialize = false; //no need to initialize
  }
  else
  {
    activateAlarmSound();
    do // will always run at least once
    {
      delay(2000); // we don't like delays here, but this will accomplish what you want in two second buckets
    }while (digitalRead(button) == HIGH);
    deactivateAlarmSound();
    initialize = true;
  }
  return initialize;
}

good place for a do/while loop

sorry about the typo, lost power at th house just then!

OK. I don't like it (maybe because I don't are seeing the whole sketch), but now I understand your idea.

What is that, BulldogLowell? I think it don't even compiles!
The lines:

    do while (digitalRead(button) == HIGH) // will always run at least once
    {
      delay(2000); // we don't like delays here, but this will accomplish what you want in two second buckets
    }

are right? Don't you miss something?

BulldogLowell:

Unicus:
Oh yes, that was it, rookie mistake, thanks.

And yes, the sound loop is supposed to be repeated for as long as the button is pressed.

then you may want to simplify as follows:

boolean checkState() 

{
 if (digitalRead(button) == LOW)
 {
   initialize = false; //no need to initialize
 }
 else
 {
   activateAlarmSound();
   do while (digitalRead(button) == HIGH) // will always run at least once
   {
     delay(2000); // we don't like delays here, but this will accomplish what you want in two second buckets
   }
   deactivateAlarmSound();
   initialize = true;
 }
 return initialize;
}




good place for a do/while loop

Yes, thanks, now that I solved that rookie issue, I am now topping off with a while loop, that checks at each incrementation, if the button is still on :slight_smile:

luisilva:
What is that, BulldogLowell? I think it don't even compiles!

Right! and I deserve the snarky comment, too!

I lost power in the middle of my edit and ruined my beautifully crafted code :smiley: :smiley: :smiley:

that probably never happens in Portugal, only here in Princeton

so sorry about that

Luis, you don't happen to be from Madeira, do you?

Unicus:
Luis, you don't happen to be from Madeira, do you?

No. Why you're asking?