Only two wires go to the Arduino (plus power wires)
How can you detect which of the buttons btn1 or btn2 was pressed (if any)?
Well, physics will tell you the voltage at A0 equals the ratio of R1 respectively R2 and the value of R0+R1 respectively R0+R2. Given that R1 and R2 are different you would know which of them has been pressed. If no button is pressed the voltage should be near +Vcc. As the resolution of the ADC is 10 bit you might expect you could attach 2^10=1024 buttons, but the ADC has an error of some steps and the precision of resistors usually is only 5 %, so not so many buttons can be distinguished. If more than one button is pressed at the same time things begin to get a bit complicated.
In this project I present three different ways of schematics how it can be done:
Schematics 1:
Schematics 2:
If you don't like this drawing you might easily rearrange it like this:
The advantage is you are getting seven totally equal elements!
A similar circuit can be fpound at Tic Tac Toe and Lights Out Game
Schematics 3:
More or less, it is a matter of your taste which one you will prefer.
And it depends on what resistors you find in your boxes. In Schematics-2, you need 8 resistors of equal values, e.g. 10 kOhms, in Schematics 3, you need 7 resistors of equal values plus one with a much higher value as pull-up resistor.
In schematics 1, things are a bit complicated. In order to get equal voltage ranges for all buttons I recommend these values:
The one on top leading to +Vcc should be 15 kOhms. The other ones have to be taken from the E12 series: 0 (zero), 2.7k, 6.8k, 12k, 22k, 39k, 68k, and 100k. If you want more or less than 7 buttons you have to do the math yourself.
/*
Schematics-1
external pullup-resistor = 15 kOhms
Res ADC btn
0 000 1
2.7k 153 2
6.8k 314 3
12k 452 4
22k 605 5
39k 737 6
100k 890 7
inf 1023 8
*/
int len = 7;
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip(len, 2, NEO_GRB + NEO_KHZ800);
const float ref = 7 / 1023.0;
void setup() {
Serial.begin(9600);
Serial.println(__FILE__);
strip.begin();
}
void loop() {
int voltage = analogRead(A0);
int btn = voltage * ref;
Serial.print(voltage);
Serial.print("\t");
Serial.println((btn + 1) * 100);
strip.fill(0x010101, 0, len);
strip.setPixelColor(btn, 255);
strip.show();
delay(100);
}

_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)









_3u05Tpwasz.png?auto=compress%2Cformat&w=40&h=40&fit=fillmax&bg=fff&dpr=2)
Comments