It's my problem
• Create a device that controls LED and buzzer using 3 buttons.
• Buttons are located at the top, middle, and bottom.
• 1 button: When the button is pressed, the 3 red LEDs turn on for 0.3 seconds in sequence and then turn off.
• 2 button: When the button is pressed, the buzzer sounds for 0.3 seconds in 3 steps in sequence.
• 3 button: When the button is pressed, the 3 blue LEDs turn on for 0.3 seconds in sequence and then turn off.
• The above function is repeated until the button is pressed again.
Constraints
All functions are performed with the operation cycle synchronized.
#define LED_PIN_1 11
#define LED_PIN_2 10
#define LED_PIN_3 9
#define LED_PIN_4 6
#define LED_PIN_5 5
#define LED_PIN_6 4
#define PUSH_PIN_1 12
#define PUSH_PIN_2 7
#define PUSH_PIN_3 3
#define BUZ_PIN 13
int buz[] = {BUZ_PIN};
int led[] = {LED_PIN_1, LED_PIN_2, LED_PIN_3};
int led2[] = {LED_PIN_4, LED_PIN_5, LED_PIN_6};
int push[] = {PUSH_PIN_1, PUSH_PIN_2, PUSH_PIN_3};
bool state[3] = {false, false, false};
bool lastState[3] = {true, true, true};
bool buttonReleased[3] = {true, true, true};
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(led[i], OUTPUT);
pinMode(push[i], INPUT_PULLUP);
}
for (int w = 0; w < 1; w++) {
pinMode(buz[w], OUTPUT);
}
for (int t = 0; t < 3; t++) {
pinMode(led2[t], OUTPUT);
}
}
void loop() {
for (int i = 0; i < 3; i++) {
if (digitalRead(push[i]) == LOW && buttonReleased[i]) {
state[i] = !state[i];
buttonReleased[i] = false;
}
else if (digitalRead(push[i]) == HIGH) {
buttonReleased[i] = true;
}
}
if (state[0] != lastState[0]) {
lastState[0] = state[0];
if (state[0] == true) {
while (state[0] == true) {
for (int i = 0; i < 3; i++) {
digitalWrite(led[i], HIGH);
delay(300);
digitalWrite(led[i], LOW);
}
delay(300);
}
for (int i = 0; i < 3; i++) {
digitalWrite(led[i], LOW);
}
}
}
if (state[1] != lastState[1]) {
lastState[1] = state[1];
if (state[1] == true) {
while (state[1] == true) {
for (int i = 0; i < 3; i++) {
tone(BUZ_PIN, 200*(i+1), 300);
delay(300);
}
noTone(BUZ_PIN);
delay(300);
}
}
}
if (state[2] != lastState[2]) {
lastState[2] = state[2];
if (state[2] == true) {
while (state[2] == true) {
for (int i = 0; i < 3; i++) {
digitalWrite(led2[i], HIGH);
delay(300);
digitalWrite(led2[i], LOW);
}
delay(300);
}
for (int i = 0; i < 3; i++) {
digitalWrite(led2[i], LOW);
}
}
}
}
If you press the first button randomly among the buttons once, the red LED lights up in sequence. But even if I press the first button again, the led doesn't turn off.
And when you press the first button, the function of the first button is repeated, right?
After that, when I press the second button, the buzzer should repeat, but it doesn't work. The problem is the same with the third button.
When I saw it, bool seems to be the problem, but I lack the ability to solve it. What should I do?