Show Posts
Pages: 1 [2] 3 4 ... 16
16  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 18, 2013, 02:39:39 am
@johncc @peterH
I had tried that earlier by stopping the reading when it was LED was on. but it didnt work...

But your code actually works... Thanks  smiley  So I guess mine was a coding error.

Sorry will write up a detailed reply tomorrow. got to prepare for a test for tomorrow...
Thank you again will get back to you guys tomorrow
17  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 17, 2013, 01:42:34 pm
@johncc
Wiring and pin assignments are correct...
Please Check http://arduino.cc/forum/index.php/topic,148314.msg1116872.html#msg1116872 for the issues I face
18  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 17, 2013, 10:08:49 am
Quote
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 smiley

Quote
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... smiley-sad Really hoped to get it working this way... Back to the usual method now... Thank you everyone for your valuable inputs smiley
19  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 16, 2013, 04:04:23 pm
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??
20  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 14, 2013, 01:39:33 pm
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);
}
21  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 14, 2013, 01:20:54 pm
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
22  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 13, 2013, 10:09:08 am
Yes. but I want it on after I release the button till I press any other button..
23  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 13, 2013, 08:12:17 am
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.
24  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 13, 2013, 06:48:05 am
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...
25  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 13, 2013, 05:30:58 am
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.
26  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 13, 2013, 01:50:40 am
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?

27  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 12, 2013, 03:32:57 pm
@GoForSmoke 
Thanks for the tips. but don't think I can apply any of that here.
@PeterH
Quote
However, the effect would be that when the switch was closed and the external pull-down resistor was connected, the voltage at the Arduino pin would drop causing the digitalRead() result to change from HIGH (switch open) to LOW (switch closed). That's the reverse of what you said.
Here is my understanding of the circuit. When I push the button, the 10k resistor will get 5V even if Pullup enabled. right? which will cause the digitalRead to read this as high. right??
28  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 12, 2013, 01:45:04 am
@GoForSmoke
Will read up on that when I get home.
So is the hardware wired up properly to work? and its just the code issue? To be more specific, the timing issue?
29  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 12, 2013, 01:35:26 am
Quote
Well, I bet if you reduce the delay to 1 second then the LED will stay on for 1 second, thus proving the link between the two.

Well that would obviously work right? I can change the delay and make it glow for any amount of time. How will that ensure the switching speed is met or not?
30  Using Arduino / Programming Questions / Re: Reading and writing to a single digital pin on: February 11, 2013, 11:02:25 pm
Thanx for all your replies smiley

@Krodal
I thought of a circuit that could work and this was what that came to my mind at that moment.Hence the internal pullup. Any other circuit to wire this on to a single pin is most welcome.
Will check out your code soon. A bit busy today. Thanx smiley

@PaulS
Code:
int buttonState = digitalRead(pushButton);  // Read the push button pin. Usually its zero, when pressed it goes to HIGH or 1

Shouldnt it work like this? because I think the internal resistor is atleast 20k or more. (Could not find the exact value from datasheet) so it will be like when pull up is enabled. Shouldnt only a maximum of 5/3 = 1.66 V come across the 10k external pull down resistor? which I believe should not cause the digitalRead to read it as an input 1...

@UKHeliBob
The first code I posted is apparently working fine. Only the second code gives me problems.
Quote
have anything to do with the delay(2000) in the loop() function and the switching of the pinMode ?

Cant say I havent thought of this but 2sec for switching is too much I guess. Or Am I wrong. Cant seem to figure out how to resolve the issue if thats the case.

Quote
Are you so short of pins that you need to connect the switch and the LED to the same pin ?
Well I am not actually. I would like reduce the wires going to arduino and save pins if its possible. If this doesnt  work out I can always go back to 2 pins for a unit but I really would like to get this one working.

@Shpaget
Quote
Why go through all the trouble with the software when you can do this?
For obvious reasons of hardware requirement.
Pages: 1 [2] 3 4 ... 16