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);
}
}
}
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 exampleif( millis()-buzzerStartTime > 10000){/*stop buzzer*/}
it does not work
part? What happens when you run your code? What do you expect do happen when you run your code?