Tutorial.TwoSwitchesOnePin History

Show minor edits - Show changes to markup

October 18, 2008, at 07:03 PM by David A. Mellis -
Changed lines 3-4 from:

There are handy 20K pullup resistors (resistors connected internally between Arduino I/O pins and VCC - +5 volts in the Arduino's case) built into the Atmega chip upon which Freeduino's are based. They are accessible from software by using the digitalWrite() function, when the pin is set to an input.

to:

There are handy 20K pullup resistors (resistors connected internally between Arduino I/O pins and VCC - +5 volts in the Arduino's case) built into the Atmega chip upon which Arduino boards are based. They are accessible from software by using the digitalWrite() function, when the pin is set to an input.

April 09, 2008, at 03:24 AM by David A. Mellis -
Changed line 17 from:
 * Read two pushbutton switches or one center-off toggle switch with one Freeduino pin
to:
 * Read two pushbutton switches or one center-off toggle switch with one Arduino pin
April 09, 2008, at 03:23 AM by David A. Mellis - freeduino -> arduino
Changed lines 3-4 from:

There are handy 20K pullup resistors (resistors connected internally between Freeduino I/O pins and VCC - +5 volts in the Freeduino's case) built into the Atmega chip upon which Freeduino's are based. They are accessible from software by using the digitalWrite() function, when the pin is set to an input.

to:

There are handy 20K pullup resistors (resistors connected internally between Arduino I/O pins and VCC - +5 volts in the Arduino's case) built into the Atmega chip upon which Freeduino's are based. They are accessible from software by using the digitalWrite() function, when the pin is set to an input.

April 09, 2008, at 02:59 AM by Paul Badger -
Changed lines 9-12 from:

if both buttons are pushed at the same time. In this case the scheme just reports that sw2 is pushed. The job of the 10K series resistor incidentally is to prevent a short circuit if a pesky user pushes both buttons at once. It can be omitted on a center-off slide or toggle switch where the states are mutually exclusive.

to:

if both buttons are pushed at the same time. In this case the scheme just reports that sw2 is pushed. The job of the 10K series resistor, incidentally, is to prevent a short circuit if a pesky user pushes both buttons at once. It can be omitted on a center-off slide or toggle switch where the states are mutually exclusive.

April 09, 2008, at 02:57 AM by Paul Badger -
Deleted lines 25-30:
 * One downside of the scheme (there always has to be a downside doesn't there?) is that you can't tell 
 * if both buttons are pushed at the same time. In this case the scheme just reports sw2 is pushed. Swap the 10k 
 * resistor to the bottom of the schematic if you want it to favor sw1. The job of the 10K series resitor is to prevent
 * a short circuit if pesky user pushes both buttons at once. It can be ommitted on a center-off slide or toggle 
 * where states are mutually exclusive.
 *
April 09, 2008, at 02:56 AM by Paul Badger -
Changed line 16 from:
 * Read_Two_Switches_ON_One_Pin
to:
 * Read_Two_Switches_On_One_Pin
April 09, 2008, at 02:56 AM by Paul Badger -
Changed lines 9-12 from:

if both buttons are pushed at the same time. In this case the scheme just reports that sw2 is pushed. The job of the 10K series resitor incidentally is to prevent a short circuit if a pesky user pushes both buttons at once. It can be ommitted on a center-off slide or toggle swithc where the states are mutually exclusive.

to:

if both buttons are pushed at the same time. In this case the scheme just reports that sw2 is pushed. The job of the 10K series resistor incidentally is to prevent a short circuit if a pesky user pushes both buttons at once. It can be omitted on a center-off slide or toggle switch where the states are mutually exclusive.

April 09, 2008, at 02:55 AM by Paul Badger -
Changed lines 5-7 from:

This sketch exploits the pullup resistors under software control. The idea is that an external 200K resistor to ground will cause the input pin to report LOW when the internal (20K) pullup resistor is turned off. When the internal pullup resistor is turned on, it will overwhelm the external 200K resistor and the pin will report HIGH.

to:

This sketch exploits the pullup resistors under software control. The idea is that an external 200K resistor to ground will cause an input pin to report LOW when the internal (20K) pullup resistor is turned off. When the internal pullup resistor is turned on however, it will overwhelm the external 200K resistor and the pin will report HIGH.

April 09, 2008, at 02:54 AM by Paul Badger -
Changed lines 3-4 from:

There are handy 20K pullup resistors (resistors connected internally between Freeduino I/O pins and VCC - +5 volts in the Freeduino's case). They are accessible from software by using the digitalWrite() function, when the pin is set to an input.

to:

There are handy 20K pullup resistors (resistors connected internally between Freeduino I/O pins and VCC - +5 volts in the Freeduino's case) built into the Atmega chip upon which Freeduino's are based. They are accessible from software by using the digitalWrite() function, when the pin is set to an input.

April 09, 2008, at 02:53 AM by Paul Badger -
Added lines 3-12:

There are handy 20K pullup resistors (resistors connected internally between Freeduino I/O pins and VCC - +5 volts in the Freeduino's case). They are accessible from software by using the digitalWrite() function, when the pin is set to an input.

This sketch exploits the pullup resistors under software control. The idea is that an external 200K resistor to ground will cause the input pin to report LOW when the internal (20K) pullup resistor is turned off. When the internal pullup resistor is turned on, it will overwhelm the external 200K resistor and the pin will report HIGH.

One downside of the scheme (there always has to be a downside doesn't there?) is that one can't tell if both buttons are pushed at the same time. In this case the scheme just reports that sw2 is pushed. The job of the 10K series resitor incidentally is to prevent a short circuit if a pesky user pushes both buttons at once. It can be ommitted on a center-off slide or toggle swithc where the states are mutually exclusive.

April 09, 2008, at 02:43 AM by Paul Badger -
Added lines 1-86:

Read Two Switches With One I/O Pin


/*
 * Read_Two_Switches_ON_One_Pin
 * Read two pushbutton switches or one center-off toggle switch with one Freeduino pin
 * Paul Badger 2008 
 * From an idea in EDN (Electronic Design News)
 *
 * Exploits the pullup resistors available on each I/O and analog pin
 * The idea is that the 200K resistor to ground will cause the input pin to report LOW when the 
 * (20K) pullup resistor is turned off, but when the pullup resistor is turned on, 
 * it will overwhelm the 200K resistor and the pin will report HIGH.
 *
 * One downside of the scheme (there always has to be a downside doesn't there?) is that you can't tell 
 * if both buttons are pushed at the same time. In this case the scheme just reports sw2 is pushed. Swap the 10k 
 * resistor to the bottom of the schematic if you want it to favor sw1. The job of the 10K series resitor is to prevent
 * a short circuit if pesky user pushes both buttons at once. It can be ommitted on a center-off slide or toggle 
 * where states are mutually exclusive.
 *
 * Schematic Diagram    ( can't belive I drew this funky ascii schematic )     
 *
 *
 *                             +5 V
 *                                |
 *                                \
 *                                /   
 *                                \    10K
 *                                /
 *                                \
 *                                |
 *                               /    switch 1  or 1/2 of center-off toggle or slide switch
 *                              /       
 *                                |
 *            digital pin ________+_____________/\/\/\____________   ground              
 *                                |               
 *                                |               200K to 1M  (not critical)
 *                               /   
 *                              /        switch 2 or 1/2 of center-off toggle or slide switch
 *                                |
 *                                |
 *                              _____   
 *                               ___     ground
 *                                _
 *
 */


#define swPin 2                 // pin for input  - note: no semicolon after #define
int stateA, stateB;             // variables to store pin states
int sw1, sw2;                   // variables to represent switch states 

void setup()                   
{
   Serial.begin(9600);
}

void loop()            
{
   digitalWrite(swPin, LOW);                   // make sure the puillup resistors are off
   stateA = digitalRead(swPin);
   digitalWrite(swPin, HIGH);                  // turn on the puillup resistors
   stateB = digitalRead(swPin);

   if ( stateA == 1 && stateB == 1 ){          // both states HIGH - switch 1 must be pushed
      sw1 = 1;
      sw2 = 0;
   }
   else if ( stateA == 0 && stateB == 0 ){     // both states LOW - switch 2 must be pushed
      sw1 = 0;
      sw2 = 1;
   }
   else{                                       // stateA HIGH and stateB LOW 
      sw1 = 0;                                 // no switches pushed - or center-off toggle in middle position
      sw2 = 0;
   }  

   Serial.print(sw1);
   Serial.print("    ");     // pad some spaces to format print output
   Serial.println(sw2);

   delay(100);  
}


Share