Tried that already with a digitalWrite(button_2,HIGH); like
Last time I checked, HIGH didn't turn the pin off.
Tried that already with a digitalWrite(button_2,HIGH); like
Last time I checked, HIGH didn't turn the pin off.
Last time I checked, HIGH didn't turn the pin off.
It should in this case because of the wiring(See schematic).. writing it low will cause the led to glow.
When you push the pushbutton, according to your circuit, the LED will turn on. Without the Arduino
doing anything at all.
Yes. but I want it on after I release the button till I press any other button..
Well, you can achieve that with the circuit you showed above by configuring the pin as an input to turn the LED off and enable it to be used as a switch input, and configuring is as an output driven LOW to turn the LED on (it would not be possible to read the switch in this state).
Make sure you do not ever configure it as an output driven HIGH, since you would immediately blow the pin's output driver if the button was pressed in that state.
Personally, I'd look for other ways to resolve the number of pins issue. You can use a matrix to control a large number of LEDs from a smaller number of pins, and if you only need to turn on one at a time you don't even need to rely on POV. You can use a resistor ladder to enable multiple switches to be read from a single analog input, and again if you only need to be able to detect one at a time then this can be done very simply with one resistor per switch.
Well, you can achieve that with the circuit you showed above by configuring the pin as an input to turn the LED off and enable it to be used as a switch input, and configuring is as an output driven LOW to turn the LED on (it would not be possible to read the switch in this state).
I do believe this is what I am doing with the code. Bu still its causing problems while implementing..
You can use a resistor ladder to enable multiple switches to be read from a single analog input, and again if you only need to be able to detect one at a time then this can be done very simply with one resistor per switch.
I would try to give this a go soon.
So as of now the circuit which is put up cant work?? ![]()
000:
So as of now the circuit which is put up cant work??
The circuit you showed in Reply #15 can be made to work if you use the approach I outlined; I haven't checked your code to see whether it does that.
I haven't checked your code to see whether it does that.
I have to do that with the code given in reply#15
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);
}
This code executes when Button 1 is pressed. The intent is to turn on the LED connected tpo this pin, and stop reading the switch.
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
This order is very dangerous because it switches the pin to an output (which was previously set HIGH to enable the pull-up resistor) and we already know the button is currently pressed. This combination will overload the pin driver, very likely damaging it. You want to make absolutely sure that any of these pins being used as an OUTPUT are set LOW first. Otherwise the general idea of turning on one LED and turning off all the others, and turning the other pins into inputs, looks correct.
Sorry for the late reply. been really busy in last 2 days..
@PeterH
his order is very dangerous because it switches the pin to an output (which was previously set HIGH to enable the pull-up resistor) and we already know the button is currently pressed. This combination will overload the pin driver, very likely damaging it. You want to make absolutely sure that any of these pins being used as an OUTPUT are set LOW first.
Do you mean to say put digitalWrite(LOW,button_1) before the following?
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
Anyways tried it still the same output.. I am starting to think this single input output think may not be possible... ![]()
Any last ditch suggestion from anyone??
I am starting to think this single input output think may not be possible...
If the pin starts as an input pin, and you read that the switch is pressed, and convert it to an output pin and turn the LED on (regardless of how the switch is wired), how will you ever read the switch again?
I've thought from the beginning that using one pin for input and output would not work.
Don't forget Charlieplexing.
Don't forget Charlieplexing.
will make the circuit more complex and wiring even more difficult.. and if 1 led or so might fail it could be very hard debug... Anyways nice thought though... never thought about it myself ![]()
I've thought from the beginning that using one pin for input and output would not work.
So I guess this a dead end then...
Really hoped to get it working this way... Back to the usual method now... Thank you everyone for your valuable inputs ![]()
I believe that it will work the way you have said you want it to, if you follow PeterH's suggestions.
If it is not, check your wiring and pin assignments? What exactly is it doing/not doing?
// "When a button is pressed, latch it's own LED on and turn off all the others"
buttonState_1 = digitalRead(button_1);
buttonState_2 = digitalRead(button_2);
if(buttonState_1 == 0) // Button 1 is pressed. Switch shorted with ground
{
// "turn off all the others"
pinMode(button_2,INPUT);
digitalWrite(button_2,HIGH); // note active LOW, so this turns led2 off
// "turn my own led on"
digitalWrite(button_1,LOW); // active LOW to enable led1
pinMode(button_1, OUTPUT); // turn it on
}
if(buttonState_2 == 0) // Button 2 is pressed. Switch shorted with ground
{
// "turn off all the others"
pinMode(button_1,INPUT);
digitalWrite(button_1,HIGH);
// "turn my own led on"
digitalWrite(button_2,LOW);
pinMode(button_2, OUTPUT);
}
@johncc
Wiring and pin assignments are correct...
Please Check Reading and writing to a single digital pin - #16 by system - Programming Questions - Arduino Forum for the issues I face
Ok, I see. You have never said though, what does your serial output show? Is it tracking properly with your button presses? You might want to slow your loop down, e.g. delay(500) instead of delay(50). Also you could put serial.prints within your if statements like "Turning 1 on", "Turning 2 on". And maybe even a delay(250) in between the two if statements. All together this may allow you to see what's going on. E.g. are you sure you don't have a sticking switch, etc.
Cheers,
John
i.e. pseudocode
read both buttons
print button values
if button1
print "Turning 1 on"
turn 1 on, 2 off
endif
delay 250
if button2
print "Turning 2 on"
turn 2 on, 1 off
endif
delay 500
The symptoms seem consistent with switch 2 sticking on. Have you actually confirmed it's a momentary switch and not a latching one? Can you print out the switch state transitions that your sketch sees to confirm that the switch inputs are giving you the values you expect?
I don't think the approach you're taking is very sensible (it feels like a complex solution to a simple problem) but it is feasible and I can't see any reason why it couldn't be made to work.
but it is feasible and I can't see any reason why it couldn't be made to work.
I'm not understanding how. If the pin starts in INPUT mode, then reading the switch makes sense. If the pin is then switched to OUTPUT mode, and written HIGH or LOW, future readings of that pin will only return the state that the pin was last written to, not the state of the switch that is attached to the pin.
Yes, good point. He should avoid reading the one that has already been latched on-- and only read the other ones. That is the problem with his current code.
@OOO
I think your approach is interesting and am looking forward to seeing it work.
You need to avoid reading the button/led that you have already turned on.
Maybe something like this (compiled but not tested)
/*
"Radio Buttons"
When a button is pressed, latch its own LED on, and turn all the others off
*/
const int nButtons=3;
int buttons[]= { 7, 12, 14 }; // each "button" is a button/led pair
int currentOn = 0;
void latch( int onPin)
{
// turn onPin on,
digitalWrite( onPin, LOW);
pinMode( onPin, OUTPUT);
// if there is an other pin on, turn it off
if (currentOn > 0)
{
pinMode( currentOn, INPUT);
digitalWrite( currentOn, HIGH); // pullup
}
}
void loop()
{
for (int i=0; i< nButtons; i++)
{
// read all but the currenly-on button
if ( buttons[i] != currentOn && digitalRead( buttons[i])== LOW) // active LOW
{
latch( buttons[i]);
currentOn = buttons[i];
break;
}
}
}
Cheers,
John