Multi Button Programming

I have an esp8266-12f with 3 buttons and 4 leds connected, I need a code to light each led with pressing its button ( other led should be off ) and if no button pressed led 4 should be light and others are off
this is my code

  #define      R1             14   // Output relay
  #define      R2            12   // Output relay
  #define      R3            13   // Output relay
  #define      R4             15   // Output relay
  #define      B1           5   // Button
  #define      B2            4   // Button
  #define      B3            2   // Button
void setup() {
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(B1, INPUT_PULLUP);
  pinMode(B2, INPUT_PULLUP);
  pinMode(B3, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {
  
  if(digitalRead(B1==HIGH)){
    digitalWrite(R1, HIGH);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
   if(digitalRead(B2==HIGH)){
    digitalWrite(R1, LOW);
    digitalWrite(R2, HIGH);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
  if(digitalRead(B3==HIGH)){
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, HIGH);
    digitalWrite(R4, LOW);
  }
      digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
    digitalWrite(R4, HIGH);
   }

But when run all leds are light without any buttons pressed
My Buttons are connected to 3.3v when pressed
Please help

  • not sure how that is possible because none of your cases sets all the LEDS HIGH or LOW

  • regardless if any of the conditions is true, the last set of statements is unconditional, not part of an if/else statement and will always take effect

  • 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.

Are your leds wired from output pin to ground via resistor, or output pin to +5 via resistor?
Are your buttons wired from input pin to ground, or input pin to +5?

think you meant..

 if(digitalRead(B1)==HIGH){

with input pull ups, the above is true when the button is not pressed..

if(!digitalRead(B1)){

the above will trigger when button pressed..

After modification

#define      R1             14   // Output relay
  #define      R2            12   // Output relay
  #define      R3            13   // Output relay
  #define      R4             15   // Output relay
  #define      B1           5   // Button
  #define      B2            4   // Button
  #define      B3            2   // Button
void setup() {
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(B1, INPUT_PULLUP);
  pinMode(B2, INPUT_PULLUP);
  pinMode(B3, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {
  
  if(!digitalRead(B1)){
    digitalWrite(R1, HIGH);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
   if(!digitalRead(B2)){
    digitalWrite(R1, LOW);
    digitalWrite(R2, HIGH);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
  if(!digitalRead(B3)){
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, HIGH);
    digitalWrite(R4, LOW);
  }
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
    digitalWrite(R4, HIGH);
   }

Now led R1,R2,R4 are on at start and led 3 off
When pressed B1 => led1 off
When pressed B2 => led2 off
When pressed B3 => nothing happen

all leds wired from output pin to ground via resistor and buttons wired from input pin to +3.3v with a resistor 10K to ground.

ok, missed the esp reference.
Using INPUT_PULLUP, you need the buttons to pull the pin to ground when you press them.
Hence looking for !button in the logic.

If not rewiring buttons, then remove ! from logic.

Your if structure looks okay, but you're missing an else at the bottom, to only do the fourth structure when no buttons are pressed.

I change the buttons to connect to gnd when pressing now
Now led R1,R2,R4 are on at start and led 3 off
When pressed B1 => nothing happen
When pressed B2 => nothing happen
When pressed B3 => led 3 on

still no else condition

Okay, so you now have a unit which pulls a button low when you press it. x3.
That's a start.
here's what you posted, reformmated (ctrl-t) to clearly show the problem:

#define      R1             14   // Output relay
#define      R2            12   // Output relay
#define      R3            13   // Output relay
#define      R4             15   // Output relay
#define      B1           5   // Button
#define      B2            4   // Button
#define      B3            2   // Button
void setup() {
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(B1, INPUT_PULLUP);
  pinMode(B2, INPUT_PULLUP);
  pinMode(B3, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {

  if (!digitalRead(B1)) {
    digitalWrite(R1, HIGH);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
  if (!digitalRead(B2)) {
    digitalWrite(R1, LOW);
    digitalWrite(R2, HIGH);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
  if (!digitalRead(B3)) {
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, HIGH);
    digitalWrite(R4, LOW);
  }
  digitalWrite(R1, LOW);
  digitalWrite(R2, LOW);
  digitalWrite(R3, LOW);
  digitalWrite(R4, HIGH);
}

Looking at your output order at the top, and the result you're getting, I'm pretty sure you've got an LED miswiring, plus the obvious logic error, as Mike pointed out.

all conditions should be mutually exclusive (i.e. else if) so that the else occurs when no other condition is true

Here, use this.

[code]
#define      R1             14   // Output relay
#define      R2            12   // Output relay
#define      R3            13   // Output relay
#define      R4             15   // Output relay
#define      B1           5   // Button
#define      B2            4   // Button
#define      B3            2   // Button
void setup() {
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(B1, INPUT_PULLUP);
  pinMode(B2, INPUT_PULLUP);
  pinMode(B3, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {

  if (!digitalRead(B1)) {
    digitalWrite(R1, HIGH);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
  else if (!digitalRead(B2)) {
    digitalWrite(R1, LOW);
    digitalWrite(R2, HIGH);
    digitalWrite(R3, LOW);
    digitalWrite(R4, LOW);
  }
  else if (!digitalRead(B3)) {
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, HIGH);
    digitalWrite(R4, LOW);
  }
  else {
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
    digitalWrite(R4, HIGH);
  }
}
[/code]

Do you want the lights to stay on after you release the button or only have them stay on while the button is pressed? Can only one button be pressed at a time?

Also, in you setup, you have to do more than just declare the pins as output. You have to write them LOW as well. The pins will most likely be floating at boot so you have to declare them as output and then write them low to be safe and complete.

I want leds to be light only while the button is pressed

Then try pulling them low in your setup and you should be good.

Disagree. Setup() makes them outputs. State doesn't matter, because in every path through his code, all 4 are set to a specific value on every pass through loop(). No ambiguity, no missed paths. Setup() doesn't need to write anything, they're immediately written by loop().

At least, if he used my posted code.

Hi,
Did you Google;

keypad 8266

They both used keypad library that would make things easier.

Tom... :smiley: :+1: :coffee: :australia:

Please see below another solution for your reference on Arduino Uno,

#define B1 A2  // Button
#define B2 A1  // Button
#define B3 A0  // Button
#define R1 12 // Output relay
#define R2 11 // Output relay
#define R3 10 // Output relay
#define R4 9  // Output relay

#define commonStatusPin R4        // Common Status Pin
const uint8_t numberOfGroup = 3;  // Number Of Group Switch and Relay
const uint8_t buttonPins[] = {B1, B2, B3};  // An array of button pins
const uint8_t relayPins[] = {R1, R2, R3};   // An array of relay pins
bool isActive = false;  // The status for one of the buttons is activated.

void setup() {
  for (uint8_t index = 0; index < numberOfGroup; index ++) {
    pinMode(buttonPins[index], INPUT_PULLUP);
    pinMode(relayPins[index], OUTPUT);
  }
  pinMode(commonStatusPin, OUTPUT);
}

void loop() {
  isActive = false; // Reset
  for (uint8_t index = 0; index < numberOfGroup; index ++) {
    bool value = !digitalRead(buttonPins[index]);
    digitalWrite(relayPins[index], value & !isActive);
    isActive |= value;  // Check if any one of the buttons is activated
  }
  digitalWrite(commonStatusPin, !isActive);
}

Wokwi Simulator link: Arduino Forum - Multi Button Programming

consider
the UNO does have digital pins 14 and 15.
switches need to be wired between pin and ground

const byte PinRelay [] = { 14, 12, 13, 15 };
const byte PinBut   [] = { 5, 4, 2 };

const int Nrelay = sizeof(PinRelay);
const int Nbut   = sizeof(PinBut);

enum { Off = HIGH, On = LOW };

void
setLeds (
    byte   rly0,
    byte   rly1,
    byte   rly2,
    byte   rly3 )
{
    digitalWrite (PinRelay [0], rly0);
    digitalWrite (PinRelay [1], rly1);
    digitalWrite (PinRelay [2], rly2);
    digitalWrite (PinRelay [3], rly3);
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (LOW == digitalRead (PinBut [0]))
        setLeds (On, Off, Off, Off);
    else if (LOW == digitalRead (PinBut [1]))
        setLeds (Off, On, Off, Off);
    else if (LOW == digitalRead (PinBut [2]))
        setLeds (Off, Off, On, Off);
    else
        setLeds (Off, Off, Off, Off);
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; Nrelay > n; n++)  {
        pinMode      (PinRelay [n], OUTPUT);
        digitalWrite (PinRelay [n], Off);
    }

    for (unsigned n = 0; Nbut > n; n++)
        pinMode (PinBut [n], INPUT_PULLUP);
}