Thanks @PeterH
Your circuit looks less messy anyways and it should work. Rewired it like that. Attached schematic.
This circuit works just fine for 1 unit connected to 1 pin. But when I connect 2 units in different pins it doesnt work(same as the case with my previous design). So I think it should be a problem with code.
What I am trying to do when I press button_1, only the LED connected to that must glow(rest must turn off if already on)
and for button_2 press, only the LED connected to that must glow(rest must turn off if already on)
int button_1 = 7 , button_2 = 12;
int buttonState_1, buttonState_2;
void setup()
{
Serial.begin(9600);
pinMode(button_1,INPUT_PULLUP); // Enabling internal pullups
pinMode(button_2,INPUT_PULLUP);
}
void loop()
{
// Reading button states
buttonState_1 = digitalRead(button_1);
buttonState_2 = digitalRead(button_2);
// Printing for Debugging
Serial.print("buttonState_1 = ");
Serial.println(buttonState_1);
Serial.print("buttonState_2 = ");
Serial.println(buttonState_2);
if(buttonState_1 == 0) // Button 1 is pressed. Switch shorted with ground
{
// If this button is pressed, the rest of the buttons (button 2 in trial case) should go off
pinMode(button_2,INPUT);
digitalWrite(button_2,HIGH); // these two statements must ideally turn off the LED in other pins or does it?
pinMode(button_1, OUTPUT); // Change from Input to Output Mode
digitalWrite(button_1,LOW); // Write Low in the pin so that pin continues to glow even after button press is released
}
if(buttonState_2 == 0) // Button 2 is pressed. Switch shorted with ground
{
// If this button is pressed, the rest of the buttons (button 1 in trial case) should go off
pinMode(button_1,INPUT);
digitalWrite(button_1,HIGH); // these two statements must ideally turn off the LED in other pins or does it?
pinMode(button_2, OUTPUT); // Change from Input to Output Mode
digitalWrite(button_2,LOW); // Write Low in the pin so that pin continues to glow even after button press is released
}
delay(50);
}
Here is the output results I get(buttons connected pins 7(button_1) and 12(button_2))
case (1):
when button_1 is pressed, LED 1 turns on and stays turned on.
After that if button_2 is pressed, LED 2 turns on and stays turned on and LED_1 turns off.
If I press button_1 again. LED 1 turns on only for the time I press it and turns off when I release the button. and LED_2 does not turn off
case(2):
when button_2 is pressed LED 2 turns on and stays turned on.
If I press button_1 again. LED 1 turns on only for the time I press it and turns off when I release the button. and LED_2 does not turn off
So problem i guess starts when LED 2 turns on. It never turns off. (Its not a problem with my pins because I have tried changing them and still it works the same)
Can someone help figure out whats wrong here?