Tutorial.TwoSwitchesOnePin History

Hide minor edits - Show changes to markup

June 05, 2010, at 11:12 AM by Equipo Traduccion -
Changed lines 1-12 from:

Read Two Switches With One I/O Pin

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.

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.

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

Lee dos interruptores con un solo pin de E/S

El chip ATmega, en el cual esta basado el Arduino, posee resistencias internas de 20k que pueden ser muy útiles(resistencias conectadas internamente entre las E/S de Arduino y VCC - +5 voltios). Estas resistencias son accesibles a través del software usando la función digitalWrite(), cuando se configura el pin como una entrada.

Este programa aprovecha las resistencias de pullup utilizando el software. La idea es que conectando una resistencia de 200K entre el pin de entrada y masa hará que cuando la resistencia interna (20K) este desactivada, la entrada detecte nivel bajo en la entrada. Sin embargo cuando se active la resistencia interna de pullup (20k) esta, governara sobre la de 200k y por tanto se leerá en la entrada un nivel alto.

Lo único malo del esquema (¿Siempre tiene que haber algo malo no?) es que no podemos saber si se ambos botones se pulsan a al vez. En este caso solo se detecta que el interruptor dos ha sido pulsado. La función de la resistencia de 10K que se pone en serie es evitar cortocircuitos si un usuario pulsa ambos botones a la vez. Puede ser omitida si usamos un interruptor de palanca en el cual tiene un punto central de apagado y que excluye de forma manual que ambos estén conectados a la vez.

Changed lines 15-16 from:
 * Read_Two_Switches_On_One_Pin
 * Read two pushbutton switches or one center-off toggle switch with one Arduino pin
to:
 * Lee_Dos_Interruptores_En_Un_Pin
 * Lee dos pulsadores o un interruptor de palanca usando un solo pin de Arduino.
Changed line 19 from:
 * From an idea in EDN (Electronic Design News)
to:
 * A partir de una idea en EDN (Electronic Design News)
Changed lines 21-24 from:
 * 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.
to:
 * Aprovecha la resistencias de pullup que poseen cada una de las E/S y los pines analógicos.
 * La idea es que la resistencia de 200k que se conecta a masa hará que si la resistencia de pullup
 * (20K) está apagada, se detectará nivel bajo en la entrada (0V) pero cuando la resistencia de pullup esté, 
 * activada la resistencia de 20K prevaleserá sobre la de 200K y el pin detectará un nivel alto.
Changed line 26 from:
 * Schematic Diagram    ( can't belive I drew this funky ascii schematic )     
to:
 * Esquemático    ( No puedo creer que haya dibujado este esqumático funky en ASCII )     
Changed line 37 from:
 *                               /    switch 1  or 1/2 of center-off toggle or slide switch
to:
 *                               /    interruptor 1  o 1/2 del interruptor de palanca
Changed line 40 from:
 *            digital pin ________+_____________/\/\/\____________   ground              
to:
 *            pin digital ________+_____________/\/\/\____________   masa              
Changed line 42 from:
 *                                |               200K to 1M  (not critical)
to:
 *                                |               200K a 1M  (no es crítica)
Changed line 44 from:
 *                              /        switch 2 or 1/2 of center-off toggle or slide switch
to:
 *                              /        interruptor 2  o 1/2 del interruptor de palanca
Changed line 48 from:
 *                               ___     ground
to:
 *                               ___     masa
Changed lines 54-57 from:
  1. 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

to:
  1. define swPin 2 // pin de entrad, sin punto y coma después del #define

int stateA, stateB; // variable para almacenar el valor de los pines int sw1, sw2; // variables para representar el estado de los pinse

Changed line 65 from:
   digitalWrite(swPin, LOW);                   // make sure the puillup resistors are off
to:
   digitalWrite(swPin, LOW);                   // asegurate de que las resistencias de pullup están desactivadas
Changed line 67 from:
   digitalWrite(swPin, HIGH);                  // turn on the puillup resistors
to:
   digitalWrite(swPin, HIGH);                  // activa las resistencias de pullup
Changed line 70 from:
   if ( stateA == 1 && stateB == 1 ){          // both states HIGH - switch 1 must be pushed
to:
   if ( stateA == 1 && stateB == 1 ){          // ambos estados a nivel alto,el interruptor 1 se ha pulsado
Changed line 74 from:
   else if ( stateA == 0 && stateB == 0 ){     // both states LOW - switch 2 must be pushed
to:
   else if ( stateA == 0 && stateB == 0 ){     // ambos estados a nivel bajo,el interruptor 2 se ha pulsado
Changed lines 78-79 from:
   else{                                       // stateA HIGH and stateB LOW 
      sw1 = 0;                                 // no switches pushed - or center-off toggle in middle position
to:
   else{                                       // stateA a nivel bajo y stateB a nivel bajo 
      sw1 = 0;                                 // no se han pulsado interruptores o el de palanca está en el centro
Changed line 85 from:
   Serial.print("    ");     // pad some spaces to format print output
to:
   Serial.print("    ");     // Algunos espacios para darle formato al texto en pantalla
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