Thank you in advance for any help.
I'm trying to debounce multiple buttons using interrupts. I'm also displaying the info on a GLCD.
I have the buttons setup to go into the interrupt pin and a adc pin and setup a resistor ladder for all the buttons. There are four buttons two for increasing and decreasing a optionspotx and two for increasing and decreasing optionspoty. The glcd also displays the adc value on the screen too. Also ledpin13 lights up when i press a button so i know the microcontroller sees a button push.
-The problem is that it doesn't read my first button at all (adc value between 0 and 100)
-When optionspotx goes to negative then back to 0 it goes 1,11,21,31
-Sometimes when i press one button it reads the others, (might be debouncing issue)
Is this the best to have multiple buttons and a screen? If there is another way I'm open to suggestions.
Here is my sketch
/*00000000 Libraries included 00000000*/
#include <SerialGraphicLCD.h>
#include <SoftwareSerial.h>
/*00000000 GLCD screen size 00000000*/
#define maxX 127//159
#define maxY 63 //127
/*00000000 variables for PINS 00000000*/
//Bup Bdown Bleft Bright Bcenter But1 But2 But3
const int ledPin13 = 13; // the number of the LED pin
/*00000000 Table for ADC storage 00000000*/
volatile int adcval;
/*00000000 variables for debounce 00000000*/
long debouncing_time = 200; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;
volatile int state = LOW;
volatile int buttonstate;
/*00000000 variables for menus 00000000*/
int menustack[6]={1,0,0,0,0,0}; //array acting as stack for menu porder when hopping from one menu to the other.
// set array size to number of menus desired
int currentmenuspot=1;
int prevmenuspot=1;
int menustackspot=0;
int menunumber;
int menupage=0; //0=Mainmenu 1=AirQ 2=TEMP 3=HEART 4=MORE
int optionrow[4][4]={{11,12,13,14},{21,22,23,24},{31,32,33,34},{41,42,43,44}}; //2D array as a map for menu options
int optionspotx=0;
int optionspoty=0;
int enterbuttons[4]={0,0,0,0}; //debounce puts a 1 to be later tested if button was pressed 1=pressed 0=not pressed
/*00000000 LCD class declaration 00000000*/
LCD LCD;
/*00000000 SETUP 00000000*/
void setup()
{
pinMode(buttonPin[0], INPUT); //button on pin 4
pinMode(buttonPin[1], INPUT); //button on pin 5
pinMode(buttonPin[2], INPUT); //button on pin 6
pinMode(buttonPin[3], INPUT); //button on pin 7
pinMode(buttonPin[4], INPUT); //button on pin 8
pinMode(buttonPin[5], INPUT); //button on pin 9
pinMode(buttonPin[6], INPUT); //button on pin 10
pinMode(buttonPin[7], INPUT); //button on pin 11
//interrupt
attachInterrupt(0, debounce, FALLING);
// pin 13 on board LED
pinMode(ledPin13, OUTPUT);
LCD.setBacklight(20);
LCD.clearScreen();
LCD.setHome();
LCD.printStr(" Program starts in "); //displays "program is starting in 10/9/8 etc"
for(int i = 5; i >= 0; i--) //splash screen
{
LCD.setX(113);
LCD.setY(0);
LCD.printNum(i);
delay(500);
}
LCD.clearScreen();
delay(500);
}
/*00000000 LOOP 00000000*/
void loop()
{
selectmove(); //compares adc values to determine apporiate action
optionrowdisplay(); //display info on glcd
}
/*00000000 displays stuff on glcd 00000000*/
void optionrowdisplay()
{
LCD.setHome();
LCD.printStr("x spot ");
delay(100);
LCD.setX(50);
LCD.setY(00);
LCD.printNum(optionspotx);
delay(100);
LCD.setX(0);
LCD.setY(9);
LCD.printStr("y spot ");
delay(100);
LCD.setX(50);
LCD.setY(9);
LCD.printNum(optionspoty);
delay(100);
LCD.setX(0);
LCD.setY(18);
LCD.printStr("center but ");
delay(100);
LCD.setX(70);
LCD.setY(18);
LCD.printNum(enterbuttons[1]);
delay(100);
LCD.setX(0);
LCD.setY(27);
LCD.printStr("button 1 ");
delay(100);
LCD.setX(70);
LCD.setY(27);
LCD.printNum(enterbuttons[0]);
delay(100);
LCD.setX(0);
LCD.setY(36);
LCD.printStr("ADC value ");
delay(100);
LCD.setX(70);
LCD.setY(36);
LCD.printNum(adcval);
delay(100);
}
void selectmove()
{
if(buttonstate==true)//interrupt debounce changes this to true
{
if(adcval>0 && adcval<100)
{optionspoty=optionspoty+1;}
else if(adcval>225 && adcval<300)
{optionspoty=optionspoty-1;}
else if(adcval>325 && adcval<380)
{optionspotx=optionspotx+1;}
else if(adcval>390 && adcval<450)
{optionspotx=optionspotx-1;}
buttonstate=false;//resets
}
}
/*00000000 debounce functions 00000000*/
void debounce()
{
if(((long)(micros() - last_micros) >= debouncing_time * 1000) | last_micros==0)
{
digitalWrite(13, state);
state = !state;
last_micros = micros();
adcval = analogRead(A0); //adc resistor ladder
buttonstate=true; //for activating selectmove
}
}/code]

