Go Down

Topic: Reading and writing to a single digital pin (Read 5 times) previous topic - next topic

0

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

PeterH


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 only provide help via the forum - please do not contact me for private consultancy.

0

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: [Select]
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);
}

PeterH

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: [Select]

    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.
I only provide help via the forum - please do not contact me for private consultancy.

0

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: [Select]
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??

Go Up