Hi all. I am using a single analog pin to control multiple buttons.
How can I repeat this scheme using GVS button?
http://static12.insales.ru/images/products/1/8155/27508699/troyka-button.2.jpg
Here is the arduino code:
#define ERROR_WINDOW 50 // +/- this value
#define BUTTONDELAY 20
#define DEBUG_ON
int analogPin = 3; // switch circuit input connected to analog pin 3
long buttonLastChecked = 0; // variable to limit the button getting checked every cycle
void setup () {
Serial.begin(9600);
pinMode(A3, INPUT);
}
void loop()
{
if( buttonLastChecked == 0 ) // see if this is the first time checking the buttons
buttonLastChecked = millis()+BUTTONDELAY; // force a check this cycle
if( millis() - buttonLastChecked > BUTTONDELAY ) { // make sure a reasonable delay passed
if( int buttNum = buttonPushed(analogPin) ) {
Serial.print("Button "); Serial.print(buttNum); Serial.println(" was pushed.");
}
buttonLastChecked = millis(); // reset the lastChecked value
}
}
int buttonPushed(int pinNum) {
int val = 0; // variable to store the read value
digitalWrite((14+pinNum), HIGH); // enable the 20k internal pullup
val = analogRead(pinNum); // read the input pin
// we don't use the upper position because that is the same as the
// all-open switch value when the internal 20K ohm pullup is enabled.
//if( val >= 923 and val <= 1023 )
// Serial.println("switch 0 pressed/triggered");
if( val >= 475 and val <= 890 ) { // 830
#ifdef DEBUG_ON
Serial.println("switch 1 pressed/triggered");
#endif
return 1;
}
else if ( val >= (375-ERROR_WINDOW) and val <= (375+ERROR_WINDOW) ) { // 693
#ifdef DEBUG_ON
Serial.println("switch 2 pressed/triggered");
#endif
return 2;
}
else if ( val >= (275-ERROR_WINDOW) and val <= (275+ERROR_WINDOW) ) { // 430
#ifdef DEBUG_ON
Serial.println("switch 3 pressed/triggered");
#endif
return 3;
}
else if ( val >= (175-ERROR_WINDOW) and val <= (175+ERROR_WINDOW) ) { // 230
#ifdef DEBUG_ON
Serial.println("switch 4 pressed/triggered");
#endif
return 4;
}
else
return 0; // no button found to have been pushed
}
Thanks in advance!