0

I have a project having two input status

  • first the input is not continuous the output buzzer relay will be on for 10 seconds and then turned off or it can be off directly by push button

  • the second state if the input is continuous the output buzzer relay will be on for 10 seconds and then turned off or it can be off directly by push button

I made this code but it does not work can anyone suggest an edit without using millis() because it will crash after 49 days and without using delay function.

Note that the device will work 24 hours / 365 days.

const int input1 = 19;
const int input2 = 36;
const int relay1 = 10;
const int relay2 = 21;
const int reset = 40;
const int buzzerreset = 45;
const int buzzerrelay = 46;

int buttonState1 = 0;    //for input1
int buttonState2 = 0;    //for input2
int buttonState3 = 0;    //for pushbutton
int buttonState4 = 0;    //for buzzer reset
int b4 = 0;              //for buzzer reset

void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(buzzerrelay, OUTPUT);
  pinMode(input1, INPUT);
  pinMode(input2, INPUT);
  pinMode(reset, INPUT);
  pinMode(buzzerreset, INPUT);

  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(buzzerrelay, LOW);
}

void loop() {
  buttonState1 = digitalRead(input1);
  if (buttonState1 == HIGH) {
    digitalWrite(relay1, HIGH);
    digitalWrite(buzzerrelay, HIGH);
    //delay(10000);
    //digitalWrite(buzzer, LOW);
  }

  buttonState3 = digitalRead(reset);
  if (buttonState3 == HIGH) {
    digitalWrite(relay1, LOW);
    //digitalWrite(buzzer, LOW);
  } else {
    buttonState4 = digitalRead(buzzerreset);

    if ((buttonState4 == HIGH || buttonState1 == HIGH) && b4 == LOW) {
      digitalWrite(buzzerrelay, LOW);
    }
    // while (digitalRead(input1) == HIGH);
    // digitalWrite(buzzerrelay, LOW);

    (buttonState2 = digitalRead(input2));
    if (buttonState2 == HIGH) {
      digitalWrite(relay2, HIGH);
      digitalWrite(buzzerrelay, HIGH);
      //delay(10000);
      //digitalWrite(buzzer, LOW);
    }

    buttonState3 = digitalRead(reset);
    if (buttonState3 == HIGH) {
      digitalWrite(relay2, LOW);
      //digitalWrite(buzzer, LOW);
    } else {
      buttonState4 = digitalRead(buzzerreset);
      if (buttonState4 == HIGH) {
        digitalWrite(buzzerrelay, LOW);
      }
      //while (digitalRead(input2) == HIGH);
      //digitalWrite(buzzerrelay, LOW);
    }
  }
}
10
  • 1
    millis doesn't crash after 50 days. It just overflows, and starts back at zero. This is only a problem if you compare two timestamps. You can get around this problem all together by calculating the difference of the two timestamps. You can then check if the difference is more that 10000ms in you case. For example if( millis()-buzzerStartTime > 10000){/*stop buzzer*/}
    – Gerben
    Jan 14, 2021 at 19:22
  • Could you expand on the it does not work part? What happens when you run your code? What do you expect do happen when you run your code?
    – Gerben
    Jan 14, 2021 at 19:24
  • why do you bother with states? ... your description says that the buzzer does the same thing in both states ... all you have to do, is to sound the buzzer for 10 seconds
    – jsotola
    Jan 14, 2021 at 20:30
  • when i run the code if the input not contentious there is no problem with the timer ,but when is the input is contentious the buzzer is also contentious and this is the problem i want the buzzer run for 10 seconds every time one of the inputs go HIGH (i have 25 inputs) even the input is contentious or not . – Gerben
    – eng.hamza
    Jan 15, 2021 at 12:01
  • You say that when you detect an input (continuous or intermittent) you want to turn on a buzzer for 10 seconds, then turn it off again. What should it to once the buzzer is off again? Should it have a "lock-out" period where it ignores the input? Or should it simply wait for another input? Once 10 seconds has passed, should it wait until the input goes to the off sate before looking for a new trigger?
    – Duncan C
    Jan 15, 2021 at 19:21

2 Answers 2

1

I would suggest having a buzzerPlaying state, an oldInput state, and a buzzerOffTime (unsigned long millis when you should turn the buzzer off.

Ignore input if buzzerPlaying == true and just check the buzzerOffTime. When buzzerOffTime passes, turn off the buzzer, set buzzerPlaying to false, and start checking the input again.

If the buzzerPlaying == false and input is high:

  1. check buzzerPlaying. If it's true, do nothing.

  2. If buzzerPlaying is false, check oldInput. If oldInput is high, do nothing (The input has not been reset.) else set buzzerPlaying to true, set oldInput to high, turn on the buzzer, and calculate buzzerOffTime.

If the buzzerPlaying == false and input is low, set oldInput to low (Once the buzzer has stopped playing and the input has dropped low, start looking for more inputs.)

3
  • thanks for replay , im new to arduino and i have 25 inputs and 25 outputs i do not think that i could handle adding this three status to my code put i will try hope it will work @
    – eng.hamza
    Jan 15, 2021 at 19:49
  • 25 inputs and 25 outputs? That means you need 50 i/o pins. Do you have an Arduino with that many pins?
    – Duncan C
    Jan 15, 2021 at 20:08
  • i will use arduino mega 23 input and 23 output and pin for buzzer and pin for buzzer reset and pin for output reset that will be 49 i/o
    – eng.hamza
    Jan 15, 2021 at 20:15
0

Whatever the inputs do, when one of them first goes HIGH you want the buzzer to sound, stopping after 10 seconds or a button-press, whichever comes first? Then you need something like this:

if any input is HIGH,
   save millis();
   start the buzzer;

   if button is HIGH
      stop the buzzer;
   else if millis() - saved millis() >= 10*1000 millisec),
      stop the buzzer;
end;
3
  • thanks for replay i will try this
    – eng.hamza
    Jan 15, 2021 at 19:50
  • This pseudocode will have the buzzer play continuously as long as any input is high. The OP wants the buzzer to only play for 10 seconds, whether the input stays on or not. And they said they only want the buzzer to play again if the input goes low again once the buzzer stops. There need to be some state variables as I suggested in my answer.
    – Duncan C
    Jan 15, 2021 at 21:25
  • Yes, that's right. :) And the psuedo-code reflects the problem statement as it had been stated until they clarified it in response to your pointing that out. That was 5 hours after my post.
    – JRobert
    Jan 15, 2021 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.