Pages: 1 [2] 3   Go Down
Author Topic: Reading and writing to a single digital pin  (Read 4952 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks @PeterH
Your circuit looks less messy anyways smiley-grin 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)

Code:
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?



* pic2.JPG (24.25 KB, 564x555 - viewed 31 times.)
« Last Edit: February 13, 2013, 01:52:26 am by 000 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you need to keep track of the previous state of the buttons, and do things only if the current state is not the same as the previous state. As that code is now, the switch may appear pressed for many iterations of loop.

Why are you hung up on using a pin for both input and output? Is it that you don't have enough pins? How many inputs and how many outputs do you need?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As that code is now, the switch may appear pressed for many iterations of loop.
Even though this is the case here, shouldnt actually turn off LED 2 ?

Quote
Why are you hung up on using a pin for both input and output? Is it that you don't have enough pins? How many inputs and how many outputs do you need?
I am planning to implement 20 LED + button combos. So if I can make i work fine with a single pin I would be saving 20pins in my MEGA and more importantly reduce the number of wires that go into Arduino MEGA. So if there is no way I can get this method working then I would switch to the usual model of using 2 pins for each module.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Even though this is the case here, shouldnt actually turn off LED 2 ?
It seems like it should. Add some Serial.print() statements to find out why it doesn't.

Perhaps the thing that you need to do is turn the other pin(s) off, first, and then change them to input. Toggling the state of an input pin only turns on and off the pullup resistor, not the pin.

Code:
  if(buttonState_1 == 0)   // Button 1 is pressed. Switch shorted with ground
It's better to use the constants HIGH and LOW for switch state comparisons.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
  }

Its entering this if statement but not actually executing first 2 statements. (Or rather I cant see the effect happening anyway)

Quote
Perhaps the thing that you need to do is turn the other pin(s) off, first, and then change them to input. Toggling the state of an input pin only turns on and off the pullup resistor, not the pin.

Tried that already with a digitalWrite(button_2,HIGH); like

Code:
if(buttonState_1 == 0)   // Button 1 is pressed. Switch shorted with ground
  {
    digitalWrite(button_2,HIGH);
    // 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
  }

It didnt cause any change in the output...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Tried that already with a digitalWrite(button_2,HIGH); like
Last time I checked, HIGH didn't turn the pin off.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you push the pushbutton,   according to your circuit,   the LED will turn on.  Without the Arduino
doing anything at all.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes. but I want it on after I release the button till I press any other button..
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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??  smiley-sad
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So as of now the circuit which is put up cant work??  smiley-sad

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

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I haven't checked your code to see whether it does that.

I have to do that with the code given in  reply#15
Code:
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);
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
    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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the late reply. been really busy in last 2 days..
@PeterH
Quote
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?
Code:
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... smiley-sad
Any last ditch suggestion from anyone??
Logged

Pages: 1 [2] 3   Go Up
Jump to: