Go Down

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

0

Hi,
I am trying to read and write to a single digital pin. Is it something which is possible with a full success rate? I am trying to read a switch output and drive an LED via the same pin. (Schematic attached)
It seemed working with one unit (switch + LED) on a single pin. Code which I used is given below. Tried to comment wherever possible in the code based on my understanding. What it did does is that reads the switch and turns on the LED for 2sec and turns it off and loops..

Code: [Select]

/*
Single Digital pin Read and Write
*/

// Pushbutton 1 end to 5V, other end to digital pin(7)
// Same digital pin(7) connected to 10k to ground
// in parallel to 10k connect led positive + 1k resistor to ground

int pushButton = 7;


void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // Enable pushbutton's pins internal pull up resistor(20k) to 5V:
  pinMode(pushButton,INPUT_PULLUP);
    // Same can be achieved by the following two lines
       // pinMode(pushButton, INPUT);
       // digitalWrite(pushButton,HIGH);
}


void loop() {
 
  int buttonState = digitalRead(pushButton);  // Read the push button pin. Usually its zero, when pressed it goes to HIGH or 1
  Serial.println(buttonState);  // Printing on serial monitor the status
  if(buttonState==1)  // When button is pressed
  {
    pinMode(pushButton, OUTPUT);  // Changing to OUTPUT mode creates low impedance path from pin (and deactivates the pullup) it gives +5V which makes LED glow.
    delay(2000);
    pinMode(pushButton,INPUT_PULLUP);  // Turn on the pullup again, thereby turning off the LED.
      //  OR
      //    pinMode(pushButton, INPUT);
      //    digitalWrite(pushButton,HIGH);
  } 
  delay(50);       
}




But is it suppossed work like this? Because when I tried to do it with multiple units(switch + LED) on 2 different pins it was not working. What I wanted to do was when a press the switch that unit need to light up turning out the any other LEDs if they were ON. Here is a crude code I tried and it wasnt working properly.

Code: [Select]
/*
Multiple Digital pin Read and Write
*/

// Pushbutton 1 end to 5V, other end to digital pin(7)
// Same digital pin(7) connected to 10k to ground
// in parallel to 10k connect led positive + 1k resistor to ground
//Same with digital pin (8)

int pushButton_1 = 8;
int pushButton_2 = 7;

int buttonState_1, buttonState_2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // Enable pushbutton's pins internal pull up resistor(20k) to 5V:
  pinMode(pushButton_1,INPUT_PULLUP);
  pinMode(pushButton_2,INPUT_PULLUP);
    // Same can be achieved by the following two lines
       // pinMode(pushButton, INPUT);
       // digitalWrite(pushButton,HIGH);
}

// the loop routine runs over and over again forever:
void loop() {
 
  buttonState_1 = digitalRead(pushButton_1);  // Read the push button pin. Usually its zero, when pressed it goes to HIGH or 1
  buttonState_2 = digitalRead(pushButton_2);
  Serial.print("buttonState_1 = "); Serial.println(buttonState_1);  // Printing on serial monitor the status
  Serial.print("buttonState_2 = "); Serial.println(buttonState_2);
  if(buttonState_1==1)  // When button is pressed
  {
    //digitalWrite(pushButton_2,LOW);
    pinMode(pushButton_2,INPUT_PULLUP);
    pinMode(pushButton_1, OUTPUT);  // Changing to OUTPUT mode creates low impedance path from pin (and deactivates the pullup) it gives +5V which makes LED glow.
   

  } 
  if(buttonState_2==1)
  {
    //digitalWrite(pushButton_1,LOW);
    pinMode(pushButton_1,INPUT_PULLUP);
    pinMode(pushButton_2, OUTPUT);
   
  }
  delay(50);       
}




Can anyone please help me figure out the problem? Is it a code issue or a schematic issue or is it just plain impossible do with arduino?

New code and circuit here : http://arduino.cc/forum/index.php/topic,148314.msg1116872.html#msg1116872

Krodal

I get a little lost reading your code.
Why do enable the internal pull-up resistor, and have an external pull-down resistor. That doesn't make sense.
Can you make two functions ? One to turn the led on and off, and another to read the switch ?

Code: [Select]

// example code, not tested.

int ledIsOn = false;

// Function Led.
//    Usage: Led(true); to turn led on
//               Led(false); to turn led off
void Led( int On)
{
  if( On)
  {
    // Set output high, before pin is made output.
    // If the output turns low, it could shortcut if button is pressed.
    digitalWrite( pushButton, HIGH);
    pinMode( pushButton, OUTPUT);
    // write once more to be sure.
    digitalWrite( pushButton, HIGH);

    ledIsOn = true;       // remember state of pin
  }
  else
  {
    // Can't make the output pin low,
    // that would shortcut if the button is pressed.
    // Return the pin to input state, without internal pull-up.
    // The led will turn off.
    pinMode( pushButton, INPUT);

    ledIsOn = false;       // remember state of pin
  }
}


// Function Switch.
//    Usage: return value is true of switch is pressed.
int Switch( void)
{
  int val;

  if( ledIsOn)
  {
    // The pin is in output state, we have to change that
    // to read the switch.
    pinMode( pushButton, INPUT);
    delay( 1);           // wait, due to the capacitance of the circuit.
    val = digitalRead( pushButton);
    Led( true);          // turn led on again.
  }
  else
  {
    val = digitalRead( pushButton);
  }
  return( val);
}


PaulS

If you have the internal pullup resistor enabled, you should NOT have an external resistor. One side of the switch goes to the digital pin, and the other side goes to ground.

Code: [Select]
  int buttonState = digitalRead(pushButton);  // Read the push button pin. Usually its zero, when pressed it goes to HIGH or 1
Not with a pullup resistor. The pin is HIGH when the switch is not pressed and LOW when pressed.

UKHeliBob

Putting aside the complication of the electrical connections could that others have pointed out, could
Quote
What it did does is that reads the switch and turns on the LED for 2sec and turns it off and loops..
have anything to do with the delay(2000) in the loop() function and the switching of the pinMode ?

Are you so short of pins that you need to connect the switch and the LED to the same pin ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Shpaget

Why go through all the trouble with the software when you can do this?

Go Up