Hi people, how are you?
I'm having trouble getting the buzzer to ring for 500 milliseconds and pause 500 milliseconds
const int led1 = 9; // define pin led 1
const int led2 = 10; // define pin led 2
const int led3 = 11; // define pin led 3
const int led4 = 12; // define pin led 4
const int led5 = 13; // define pin led 5
const int buzzer = 4; // define pin buzzer
const int timeVermelho = 5; // define red squad
const int timeVerde = 6; // define pin green squad
const int botao = 2; // define pin button 1
int estadoBotao; // variable that keeps the button state 1
const int botao2 = 3; // define pin button 2
int estadoBotao2; // variable that keeps the button state 2
unsigned long momento; // variable to receive millis from green squad
unsigned long momento2; // variable to receive millis from red squad
unsigned long momento3; // variable to receive millis from buzzer
int frequenciaAciona; // variable sound when triggering
int frequenciaFinal; // variable confirms activation
int taAceso1; // variable to store if led1 is on
int taAceso2; // variable to store if led2 is on
int taAceso3; // variable to store if led3 is on
int taAceso4; // variable to store if led4 is on
int taAceso5; // variable to store if led5 is on
void setup() {
frequenciaAciona = 2000; // trigger tone
frequenciaFinal = 1000; // tone confirms
pinMode(led1, OUTPUT); // define led as output
pinMode(led2, OUTPUT); // define led as output
pinMode(led3, OUTPUT); // define led as output
pinMode(led4, OUTPUT); // define led as output
pinMode(led5, OUTPUT); // define led as output
pinMode(timeVerde, OUTPUT); // define green squad led as output
pinMode(timeVermelho, OUTPUT); // define red squad led as output
pinMode(botao, INPUT_PULLUP); // sets button 1 to HIGH
pinMode(botao2, INPUT_PULLUP); // sets button 2 to HIGH
pinMode(buzzer, OUTPUT); // define buzzer as output
}
void loop() {
// green button behavior
estadoBotao = digitalRead(botao); // reads the state that button 1 is in: pressed (LOW) or released (HIGH)
taAceso1 = digitalRead(led1);
taAceso2 = digitalRead(led2);
taAceso3 = digitalRead(led3);
taAceso4 = digitalRead(led4);
taAceso5 = digitalRead(led5);
if ( (millis() - momento) > 0) { // if the elapsed time is greater than 0 seconds
if (estadoBotao == LOW) { // if button 1 is pressed
digitalWrite(led1, HIGH); // turn on the led 1
if (taAceso1 == HIGH) { // if led1 is on, activates the buzzer
tone(buzzer, frequenciaAciona);
}
}
}
if ( (millis() - momento) > 1000) { // if the elapsed time is greater than 1 seconds
if (estadoBotao == LOW) { // if button 1 is pressed
digitalWrite(led2, HIGH); // turn on the led 2
if (taAceso2 == HIGH) { // if led2 is on, activates the buzzer
tone(buzzer, frequenciaAciona);
}
}
}
if ( (millis() - momento) > 2000) { // f the elapsed time is greater than 2 seconds
if (estadoBotao == LOW) { // if button 1 is pressed
digitalWrite(led3, HIGH); // turn on the led 3
if (taAceso3 == HIGH) { // if led3 is on, activates the buzzer
tone(buzzer, frequenciaAciona);
}
}
}
if ( (millis() - momento) > 3000) { // f the elapsed time is greater than 3 seconds
if (estadoBotao == LOW) { // if button 1 is pressed
digitalWrite(led4, HIGH); // turn on the led 4
if (taAceso4 == HIGH) { // if led4 is on, activates the buzzer
tone(buzzer, frequenciaAciona);
}
}
}
if ( (millis() - momento) > 4000) { // f the elapsed time is greater than 4 seconds
if (estadoBotao == LOW) { // if button 1 is pressed
digitalWrite(led5, HIGH); // turn on the led 5
digitalWrite(timeVerde, HIGH); // turn on the led green
digitalWrite(timeVermelho, LOW); // turn off the led red
if (taAceso5 == HIGH) { // if led5 is on, activates the buzzer
tone(buzzer, frequenciaAciona);
}
}
} if (estadoBotao == HIGH) { // if button 1 is released
digitalWrite(led1, LOW); // turn off led 1
digitalWrite(led2, LOW); // turn off led 2
digitalWrite(led3, LOW); // turn off led 3
digitalWrite(led4, LOW); // turn off led 4
digitalWrite(led5, LOW); // turn off led 5
noTone(buzzer);
momento = millis(); // updates the moment variable to the current time
}
// red button behavior
estadoBotao2 = digitalRead(botao2); // reads the state that button 2 is in: pressed (LOW) or released (HIGH)
if ( (millis() - momento2) > 0) { // if the elapsed time is greater than 0 seconds
if (estadoBotao2 == LOW) { // if button 2 is pressed
digitalWrite(led5, HIGH); // turn on the led 5
if (taAceso5 == HIGH) {
tone(buzzer, frequenciaAciona);
}
}
if ( (millis() - momento2) > 1000) { // if the elapsed time is greater than 1 seconds
if (estadoBotao2 == LOW) { // if button 2 is pressed
digitalWrite(led4, HIGH); // turn on the led 4
if (taAceso4 == HIGH) {
tone(buzzer, frequenciaAciona);
}
}
}
if ( (millis() - momento2) > 2000) { // if the elapsed time is greater than 2 seconds
if (estadoBotao2 == LOW) { // if button 2 is pressed
digitalWrite(led3, HIGH); // turn on the led 3
if (taAceso3 == HIGH) {
tone(buzzer, frequenciaAciona);
}
}
}
if ( (millis() - momento2) > 3000) { // if the elapsed time is greater than 3 seconds
if (estadoBotao2 == LOW) { // if button 2 is pressed
digitalWrite(led2, HIGH); // turn on the led 2
if (taAceso2 == HIGH) {
tone(buzzer, frequenciaAciona);
}
}
}
if ( (millis() - momento2) > 4000) { // if the elapsed time is greater than 4 seconds
if (estadoBotao2 == LOW) { // if button 2 is pressed
digitalWrite(led1, HIGH); // turn on the led 1
digitalWrite(timeVermelho, HIGH); // turn on the led red
digitalWrite(timeVerde, LOW); // turn off the led gree
if (taAceso1 == HIGH) {
tone(buzzer, frequenciaAciona);
}
}
} if (estadoBotao2 == HIGH) { // if button 1 is released
digitalWrite(led1, LOW); // turn off led 1
digitalWrite(led2, LOW); // turn off led 2
digitalWrite(led3, LOW); // turn off led 3
digitalWrite(led4, LOW); // turn off led 4
digitalWrite(led5, LOW); // turn off led 5
momento2 = millis(); // updates the moment variable to the current time
}
}
}
In this part of the code
if (taAceso1 == HIGH) { // if led1 is on, activates the buzzer
tone(buzzer, frequenciaAciona);
}
Can someone help me?