Hello All,
First time poster here, but I do know the frustration of asking a question that has already been answered. I am new to programming, but trying to build projects as they come and figure it out as I go.
I have a project where I have 6 contact switches (Level 1-6) that need to indicate to 6 LED's respectively. I also have a 5v buzzer as an audible alarm that one of the switches has closed. I have that much figured out.
Where I am getting into a land of frustration, is that I am trying to add a button as an acknowledge switch that would disable the buzzer for a set duration of time (somewhere between 10-15 minutes), while still allowing the other contact switches to indicate open/close with the LED's during that time
.
I have ran this code with the delay function successfully , until the blocking does not allow the other led's to operate correctly.
I believe I understand the concept of the millis() function, but I am having trouble applying it. As I understand it, If a button was pressed to turn on an LED at the currentMillis of, just say,100, and you programmed a wait time of 500, the LED would turn off at 600. With that logic, if the button was pressed again at 2000, with the same wait time the led would turn off at 2500.
Currently the LED's and buzzer operate correctly,
With the acknowledge button depressed, the buzzer does turn off
With the acknowledge button depressed, other LED's will indicate open/closed
When the acknowledge button is released, with a contact switched closed, the buzzer turns back on with no delay.
Thank you for the help,
const int LED1 = 3;
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 8;
int Level1 = A0;
int Level2 = A1;
int Level3 = A2;
int Level4 = A3;
int Level5 = A4;
int Level6 = A5;
int Buzzer = 2;
int Button = 9;
int buttonState = 0;
boolean BuzzerState = false;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 4000;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(Level1, INPUT);
pinMode(Level2, INPUT);
pinMode(Level3, INPUT);
pinMode(Level4, INPUT);
pinMode(Level5, INPUT);
pinMode(Level6, INPUT);
pinMode(Button, INPUT);
pinMode(Buzzer, OUTPUT);
Serial.begin(9600);
startMillis = millis();
}
void loop() {
Serial.println(digitalRead(Button));
buttonState = digitalRead(Button);
digitalWrite(Buzzer, LOW);
BuzzerState = false;
if (digitalRead(Level1) == HIGH) {
digitalWrite(LED1, HIGH);
BuzzerState = true;
} else {
digitalWrite(LED1, LOW);
}
if (digitalRead(Level2) == HIGH) {
digitalWrite(LED2, HIGH);
BuzzerState = true;
} else {
digitalWrite(LED2, LOW);
}
if (digitalRead(Level3) == HIGH) {
digitalWrite(LED3, HIGH);
BuzzerState = true;
} else {
digitalWrite(LED3, LOW);
}
if (digitalRead(Level4) == HIGH) {
digitalWrite(LED4, HIGH);
BuzzerState = true;
} else {
digitalWrite(LED4, LOW);
}
if (digitalRead(Level5) == HIGH) {
digitalWrite(LED5, HIGH);
BuzzerState = true;
} else {
digitalWrite(LED5, LOW);
}
if (digitalRead(Level6) == HIGH) {
digitalWrite(LED6, HIGH);
BuzzerState = true;
} else {
digitalWrite(LED6, LOW);
}
if (BuzzerState == true) {
if (digitalRead(Button) == HIGH) {
currentMillis = millis();
if (currentMillis - startMillis >= period) {
digitalWrite(Buzzer, LOW);
startMillis = currentMillis;
}
} else {
digitalWrite(Buzzer, HIGH);
}
}
}