Hey guys, Super newbie here. im working on tutorials on the Arduino starter book. project 2 is the spaceship interface. Super basic and easy but i decided to attempt to add functions and arrays to practice that side of things.
object. LED 3 stays on when button is LOW
when button is HIGH, LED 2 and LED 3 blink on and off.
When button is released again it goes back to relax function.(LED3 HIGH only)
Whats happening now is when i release the button its stuck in the toggleLED function and pauses in that state and will not go to relax state.
What am i doing wrong here?
#define LED_1_PIN 12
#define LED_2_PIN 11
#define LED_3_PIN 10
#define BUTTON_PIN 2
#define LED_PIN_ARRAY_SIZE 3
int LEDBlinkState = 1;
byte LEDPinArray[LED_PIN_ARRAY_SIZE] =
{LED_1_PIN, LED_2_PIN, LED_3_PIN};
void relax ()
{
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, HIGH);
}
void setLEDPinModes()
{
for (int i = 0; i < LED_PIN_ARRAY_SIZE; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}
void turnOffAllLEDs()
{
for (int i = 0; i < LED_PIN_ARRAY_SIZE; i++) {
digitalWrite(LEDPinArray[i], LOW);
}
}
void toggleLEDs()
{
if (LEDBlinkState == 1) {
digitalWrite(LED_1_PIN, HIGH);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, LOW);
LEDBlinkState = 2;
}
else {
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, HIGH);
digitalWrite(LED_3_PIN, LOW);
LEDBlinkState = 1;
}
}
void setup() {
pinMode(BUTTON_PIN, INPUT);
setLEDPinModes();
turnOffAllLEDs();
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
toggleLEDs();
delay(300);
}
else {
relax;
}
}