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
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?
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.
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
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:
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.
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.
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().
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);
}