Hide minor edits - Show changes to markup
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.
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.
* Read_Two_Switches_On_One_Pin * Read two pushbutton switches or one center-off toggle switch with one Arduino pin
* Lee_Dos_Interruptores_En_Un_Pin * Lee dos pulsadores o un interruptor de palanca usando un solo pin de Arduino.
* From an idea in EDN (Electronic Design News)
* A partir de una idea en 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.
* 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.
* Schematic Diagram ( can't belive I drew this funky ascii schematic )
* Esquemático ( No puedo creer que haya dibujado este esqumático funky en ASCII )
* / switch 1 or 1/2 of center-off toggle or slide switch
* / interruptor 1 o 1/2 del interruptor de palanca
* digital pin ________+_____________/\/\/\____________ ground
* pin digital ________+_____________/\/\/\____________ masa
* | 200K to 1M (not critical)
* | 200K a 1M (no es crítica)
* / switch 2 or 1/2 of center-off toggle or slide switch
* / interruptor 2 o 1/2 del interruptor de palanca
* ___ ground
* ___ masa
int stateA, stateB; // variables to store pin states int sw1, sw2; // variables to represent switch states
int stateA, stateB; // variable para almacenar el valor de los pines int sw1, sw2; // variables para representar el estado de los pinse
digitalWrite(swPin, LOW); // make sure the puillup resistors are off
digitalWrite(swPin, LOW); // asegurate de que las resistencias de pullup están desactivadas
digitalWrite(swPin, HIGH); // turn on the puillup resistors
digitalWrite(swPin, HIGH); // activa las resistencias de pullup
if ( stateA == 1 && stateB == 1 ){ // both states HIGH - switch 1 must be pushed
if ( stateA == 1 && stateB == 1 ){ // ambos estados a nivel alto,el interruptor 1 se ha pulsado
else if ( stateA == 0 && stateB == 0 ){ // both states LOW - switch 2 must be pushed
else if ( stateA == 0 && stateB == 0 ){ // ambos estados a nivel bajo,el interruptor 2 se ha pulsado
else{ // stateA HIGH and stateB LOW
sw1 = 0; // no switches pushed - or center-off toggle in middle position
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
Serial.print(" "); // pad some spaces to format print output
Serial.print(" "); // Algunos espacios para darle formato al texto en pantalla
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.
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.
* Read two pushbutton switches or one center-off toggle switch with one Freeduino pin
* Read two pushbutton switches or one center-off toggle switch with one Arduino pin
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.
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.
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.
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.
* 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. *
* Read_Two_Switches_ON_One_Pin
* Read_Two_Switches_On_One_Pin
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.
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.
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.
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.
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.
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.
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.
/*
* 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);
}