How to switch off a serie of led's with a single pressed btn at the same time

i have wrote this code

const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
const int led4 = 13;
const int buzz = 9;
const int btn = 8;
char choice;

void setup()  {
Serial.begin(9600);
pinMode(led1,  OUTPUT);
pinMode(led2,  OUTPUT);
pinMode(led3,  OUTPUT);
pinMode(led4,  OUTPUT);
pinMode(buzz,  OUTPUT);
pinMode(btn,  INPUT);
}
void loop() {
while(Serial.available()>0){ 
choice = Serial.read();
Serial.println(choice);
delay(15);
  switch(choice){
    case 'A':
    digitalWrite(led1, HIGH);
    digitalWrite(buzz, HIGH);
    choice = "";
    break;
    case 'B':
    digitalWrite(led2, HIGH);
    digitalWrite(buzz, HIGH);
    choice = "";
    break;
    case 'C':
    digitalWrite(led3, HIGH);
    digitalWrite(buzz, HIGH);
    choice = "";
    break;
    case 'D':
    digitalWrite(led4, HIGH);
    digitalWrite(buzz, HIGH);
    choice = "";
    break;
    }
    }
}

to light a specific led and start buzzer based on the msg sent by bluetooth
and i want to make a btn to stop every led that is working and stop the buzzer when it get pressed

just do this upon reception of the command

    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(buzz, LOW);
1 Like

thanks i just found the solution

if (digitalRead(btn) == HIGH){
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
      digitalWrite(buzz, LOW);
    }

i add this

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.