Hi, I want to know how to calculate the values for an AnalogButton keypad, I did a search, but I dont found anything about how to calculate this, I copied an already maked circuit, but I added two buttons because I need them, and this is what I have:
BUTTONS VALUES RESISTORS
btn 1 837-838 220
btn 2 737-738 390
btn 3 610-611 680
btn 4 ???-??? 1.0K
btn 5 ???-??? 1.5K
btn 6 318-319 2.2k
btn 7 178-179 4.7k
btn 8 91-92 10k
The circuit:
The Circuit:
Analog pin 5
|
Ground--1K--|--------|--------|-------|-------|-------|-------|-------|
| | | | | | | |
btn1 btn2 btn3 btn4 btn5 btn6 btn7 btn8
| | | | | | | |
220 Ohm 390 Ohm 680 Ohm 1.0K 1.5K 2.2K 4.7K 10K
|--------|--------|-------|-------|-------|-------|-------|-- +5V
the code will be this
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and there high and low values
int Button[8][3] = {{1, 837, 838}, // button 1
{2, 737, 738}, // button 2
{3, 610, 611}, // button 3
{4, ??, ??}, // button 4
{5, ??, ??}, // button 5
{6, 318, 319}, // button 6
{7, 178, 179}, // button 7
{8, 91, 92}, // button 8
int analogpin = 5; // analog pin to read the buttons
int label = 0; // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0; // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0; // the debounced input value
int ButtonVal;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// If we have gone on to the next millisecond
if (millis() != time)
{
// check analog pin for the button value and save it to ButtonVal
ButtonVal = analogRead(analogpin);
if(ButtonVal == current_state && counter >0)
{
counter--;
}
if(ButtonVal != current_state)
{
counter++;
}
// If ButtonVal has shown the same value for long enough let's switch it
if (counter >= debounce_count)
{
counter = 0;
current_state = ButtonVal;
//Checks which button or button combo has been pressed
if (ButtonVal > 0)
{
ButtonCheck();
}
}
time = millis();
}
}
void ButtonCheck()
{
// loop for scanning the button array.
for(int i = 0; i <= 8; i++)
{
// checks the ButtonVal against the high and low vales in the array
if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
{
// stores the button number to a variable
label = Button[i][0];
Action();
}
}
}
void Action()
{
if(label == 1)
{
Serial.println("Button 1");
}
if(label == 2)
{
Serial.println("Button 2");
}
if(label == 3)
{
Serial.println("Button 3");
}
if(label == 4)
{
Serial.println("Button 4");
}
if(label == 5)
{
Serial.println("Button 5");
}
if(label == 6)
{
Serial.println("Button 6");
}
if(label == 7)
{
Serial.println("Button 7");
}
if(label == 8)
{
Serial.println("Button 8");
}
//Serial.println("Button =:");
//Serial.println(label);
//delay(200);
}
But I need a total of 16 buttons, so duplicating this will be suficient right? I need to duplicate identifiers, like ‘‘j’’ to ‘‘k’’? And the AnalogPin to use ofcourse.
Im sorry if that questions are a bit noob, is the first time I do programming …