Variable not updating

I am completely new to Arduino and am having an issue with my code, which I couldn’t find a solution to in the forums. I was making a program that would trigger an alarm for a set time of that starts at 5 seconds when a motion sensor is triggered. To stop it from going for the whole time something is moving I set a delay of 5 minutes. (as a side note any help with how to delay the alarm without delaying the whole loop would be greatly appreciated) This is all working fine but I also want to be able to change the times with buttons. I have it post to the serial monitor, however when I pressed the buttons nothing happened to the variables, they just stayed constant. Please let me know if I am missing something or should explain something better. Thanks in advance for the help.

Buzzer4.ino (2.5 KB)

You are missing reading the sticky posts at the top of the forum, about how to post code in code tags.

You have a trailing ';' after all your if() statements. That is not the correct syntax. Remove them. With them present, they terminate the if() statement and all your code you think is associated with the if() is not associated and will always execute.

I have a similar question. Here is my code:

void loop() {
  unsigned long LowAt;
  
  if (digitalRead(Pin) == HIGH) {
    Serial.print("HIGH at ");
    Serial.print(millis());
    Serial.print(" was last low at ");
    Serial.print(LowAt);
    Serial.print(" high for ");
    Serial.print((millis() - LowAt)/1000);
    Serial.println("s");
  } else {
    LowAt = millis();
    Serial.print("LOW at ");
    Serial.println(LowAt);
  }
  
  delay(100);
}

When this runs, and I toggle PIN from HIGH to LOW, I get this:

LOW at 0 LOW at 99 HIGH at 199 was last low at 0 high for 0s HIGH at 300 was last low at 0 high for 0s LOW at 401 LOW at 500 HIGH at 601 was last low at 0 high for 0s LOW at 702 LOW at 801 HIGH at 902 was last low at 0 high for 0s HIGH at 1003 was last low at 0 high for 1s LOW at 1103 LOW at 1204 LOW at 1304 HIGH at 1404 was last low at 0 high for 1s HIGH at 1505 was last low at 0 high for 1s

AtLow is getting updated in the ELSE clause but in the IF clause, AtLow is always reporting 0 (zero).

You are resetting the value of AtLow every time through loop().

Declare it as static to stop that happening

That makes sense. Thank you!

Taking away the ";" worked but now the variables are being changed when the inputs haven't been touched. This is from the code at the top of the thread.

Don't know what you mean.... Every time through loop you can potentially change AtLow. Or are you referring to something else? Please post your current code and the output and what you expect vs. what you are getting.

At first I thought the issue was not putting "InputPullup" However adding this didn't solve the issue. The serial monitor reads out the values of "buzzdelay" and "motiondelay" which should be "5000 and "300000" respectively until an input button is pressed which will change these values. However I never touched the buttons and the values change randomly (up and down and at no specific time interval) by the amount (1000 or 60000) as if the buttons were being depressed. Example: The serial monitor may read for "buzzdelay": "5000, 6000, 6000, 7000, 6000, 5000, 4000". I hope this makes more sense.

int buzzPin = 8;                      // choose the pin for the LED
int inputPin = 7;                     // choose the input pin (for PIR sensor)
int pirState = LOW;                   // we start, assuming no motion detected
int val = 0;                          // variable for reading the pin status
int buzzdelay = 5000;                 // variable for buzz time
long int motiondelay = 300000;     // variable for motion delay time
int buzzplusPin = 5;            // pin for adding time to buzzer
int buzzminusPin = 2;           // pin for subtracting time from buzzer
int motionplusPin = 3;          // pin for adding time to motion delay
int motionminusPin = 4;         // pin for subtraction time from motion delay


void setup() {
  pinMode(buzzPin, OUTPUT);       // declare LED as output
  pinMode(inputPin, INPUT_PULLUP);       // declare sensor as input
  pinMode(buzzplusPin, INPUT_PULLUP);    // declare button as input
  pinMode(buzzminusPin, INPUT_PULLUP);   // declare button as input
  pinMode(motionplusPin, INPUT_PULLUP);  // declare button as input
  pinMode(motionminusPin, INPUT_PULLUP); // declare button as input
  Serial.begin(9600);
}

void loop(){

  if (digitalRead(buzzplusPin) == LOW)      //add 1 second to buzzer delay time
  {
    buzzdelay = buzzdelay + 1000;
  }
  if (digitalRead(buzzminusPin) == LOW)     //subtract 1 second from buzzer delay time
  {
    buzzdelay = buzzdelay + -1000;
  }
  if (digitalRead(motionplusPin) == LOW)     //add 1 min to motion delay time
  {
    motiondelay = motiondelay + 60000;
  }
  if (digitalRead(motionminusPin) == LOW)    // subtract 1 min from motion delay time
  {
    motiondelay = motiondelay + -60000 ;
  }
  Serial.print("Buzzer Delay:  ");
  Serial.println(buzzdelay);
  Serial.print("Motion Delay:  ");
  Serial.println(motiondelay);
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(buzzPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      pirState = HIGH;
      delay(buzzdelay);           // delay buzzer delay time
    }
  } else {
    digitalWrite(buzzPin, LOW); // turn buzzer OFF
    if (pirState == HIGH){
      // we have just turned off
      pirState = LOW;
      delay(motiondelay);           // delay motion delay time
    }
  }
}

How are the inputs wired ?

If these values are changing without you touching any buttons, your buttons are most likely wired wrong. When using INPUT_PULLUP, one side of a button goes to an arduino pin and the other side goes to ground. This way, when the button is not pressed, it is connected to VCC through the internal pullup resistor which means it will read HIGH. When the button is pressed, it will be shorted to ground and read LOW.

If your inputPin is wired up like this as well, your logic needs to be reversed.

You should also look at the State Change Detection example (File->examples->02.Digital->StateChangeDetection) since you want to change your delay variables when a button becomes pressed, not when it is pressed. Otherwise, loop() will cycle so quickly, you will increment/decrement many times for what a human would consider one press. Do this for all your buttons.

Finally, you can also capture the Serial Monitor output and post it inside its own code block, following your code and then everyone can see exactly what is happening vs. describing it.

blh64:
You have a trailing ‘;’ after all your if() statements. That is not the correct syntax.

The syntax is correct, or the compiler would have complained.
It is the semantics that are incorrect.