Connect Several Digital Inputs to One Analog Input!
Intro: Connect Several Digital Inputs to One Analog Input!
STEP 1: Resistor Dividers in Five
STEP 2: Schematics and Explanation
Also useful is the fact that pressing two buttons simultaneously will produce a completely independent value. Based on the equation for parallel resistors, 1/R=1/R1 +1/R2 ... 1/Rx, we can determine that buttons one and two pressed together will produce an overall resistance of 1/100 +1/330 =1/R, so solving for R, the result is about 77 ohms. Plug that into the voltage divider formula with 77 ohms as R2, {77 / (1077)} * 5 = 0.35V, we get an entirely new voltage reading. Possible uses include being able to read diagonals on joysticks that use buttons, like this one: https://www.sparkfun.com/products/9182, without several if..then...else if statements on the Arduino.
For more on resistors in parallel, read this article: http://www.electronics-tutorials.ws/resistor/res_4.html
STEP 3: Final Notes & Code
Read here on the arduino website: http://arduino.cc/en/Reference/SwitchCase
Example:
int buttonValue1 = 92; //convert 0.45V into analog reading
int buttonValue2 = 254; //convert 1.24V into analog reading
int buttonValue3 = 327; //convert 1.6V into analog reading
int value;
void setup ()
{
}
void loop() {
value = analogRead(A1);
switch (value) {
case 92:
//button one was pressed
break;
case 254:
//button two was pressed
break;
case 327:
//button three was pressed
break;
case 72: //convert 0.35V into analog reading
//button one AND button two were pressed
default:
//no button pressed
break;
}
}
This can only be used for digital input devices such as phototransistors, switches, and the like.
Please post any questions, comments, corrections, or ideas in the comments. Thanks for reading, and I hope I gave you some ideas!
5 Comments
justbennett 11 years ago
sidmani 11 years ago
sidmani 11 years ago
justbennett 11 years ago
Along the way I learned, as russ_hensel has pointed out that the Arduino can only read analog to digital about 10,000 times a second. Pshh.
russ_hensel 11 years ago