Software Debouncing An AnalogRead Voltage Divider Rotary Switch
i have single pole 9 position rotary switch 1k resistors between outputs, +5v first output, gnd last , com connected analog input of arduino mega. works expect giving unique ranges each position. however, expected, giving 1023 value in between slots. have used software debounce on momentary , 2 , 3 position switches, bit perplexed how go have switch on in same position until turn new position. delay not option me since attempting employ larger existing code many other switches.
i have tried following existing switch debounce routines, i'm novice programmer @ best , brain getting fried. rather posting bad code, hoping tell me if should using same concept buttons or barking wrong tree.
here's normal button debounce routine i'm trying work off , getting nowhere:
i'm thinking need 1 timer rather array. buttonstate var cannot equated simple boolean since it's analog read value. if point me in right direction i'd appreciate it.
thanks!
i have tried following existing switch debounce routines, i'm novice programmer @ best , brain getting fried. rather posting bad code, hoping tell me if should using same concept buttons or barking wrong tree.
here's normal button debounce routine i'm trying work off , getting nowhere:
code: [select]
long lastdebouncetime[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
long debouncedelay = 10;
boolean lastbuttonstate[] = {high, high, high, high, high, high, high, high, high};
boolean buttonstate[] = {low, low, low, low, low, low, low, low, low};
const short button[] = {5, 6, 7, 14, 15, 16, 17, 24, 25};
const short qbutton = sizeof(button) / sizeof(button[0]);
void setup() {
serial.begin(115200);
(int = 0; < qbutton; i++) {
pinmode(button[i], input_pullup);
}
}
void loop() {
//check switches
(int = 0; < qbutton ; i++) {
boolean reading = digitalread(button[i]);
if (reading != lastbuttonstate[i]) {
lastdebouncetime[i] = millis();
}
if ((millis() - lastdebouncetime[i]) > debouncedelay) {
if (reading != buttonstate[i]) {
buttonstate[i] = reading;
switch (i) {
case 0:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 1:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 2:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 3:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 4:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 5:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 6:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 7:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
case 8:
if (buttonstate[i] == 1) {
serial.println("do something");
} else serial.println("do else");
break;
}//switch
}//end if button state
}//end if debounce timer
lastbuttonstate[i] = reading;
}//end loop
}
i'm thinking need 1 timer rather array. buttonstate var cannot equated simple boolean since it's analog read value. if point me in right direction i'd appreciate it.
thanks!
you may better off standard debouncing methods. array of buttons doesn't make sense if 1 button/position can active @ time.
Arduino Forum > Using Arduino > Project Guidance > Software Debouncing An AnalogRead Voltage Divider Rotary Switch
arduino
Comments
Post a Comment