I'm working on a project where I need to use multiple buttons to navigate through menus on an LCD screen.
I've used an LCD Shield to do something similar before. However, this time I can't have the buttons on the LCD itself, so I bought an I2C LCD and regular buttons.
I tried using the digital inputs (attaching one end of the button to 5v and the other end on a digital pin), but the digitalRead wasn't working well. I read I needed to ground the button with a resistor to fix this, but I don't have the correct resistors at the moment.
As a result, I'm trying to approach this the same way I did the LCD Shield buttons, using analog pins.
This is what I used with the LCD Shield:
int lcd_key =0;
int adc_key_in =0;
#define btnRIGHT 0 //defining buttons
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int read_LCD_buttons(){ // read the buttons
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; //distinguishing buttons from analog read
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 150) return btnUP;
if (adc_key_in < 300) return btnDOWN;
if (adc_key_in < 450) return btnLEFT;
if (adc_key_in < 680) return btnSELECT;
return btnNONE;
}
I would use read_LCD_buttons() and if statements to execute commands when certain buttons were pressed. This worked perfectly.
I'm trying to do something similar with the regular buttons now. The only difference is each button has its own analog pin. Both buttons are attached to the 5v pin and the other ends of the buttons are attached to A0 and A1 respectively.
The issue I'm having is that for some reason when I press button 2 (A1), it affects the reading of button 1 (A0). So I'll press the second button, and the reading for the first button will jump above 1000, making it seem like the first button is pressed.
I've tested both buttons with a multimeter and they work fine.
This is the code I'm using for the regular buttons (I've also attached the file):
//debounce
int buttonStates;
int lastButtonState = 0;
unsigned long lastDebounce = 0;
unsigned long debounceDelay = 60;
//buttons
int buttons(){
if (analogRead(0) > 1000){
return 1;
}
if (analogRead(1) > 1000){
return 2;
}
else {
return 0;
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = buttons();
Serial.println(analogRead(0));
//Serial.println(reading);
if (reading != lastButtonState) {
lastDebounce = millis(); //Starting lastDebounce timing
}
if ((millis() - lastDebounce) > debounceDelay) { //Check if delay has passed
if (reading != buttonStates) {
buttonStates = reading; //Set button state
if (buttonStates == 1){
Serial.println("Button 1");
}
if (buttonStates == 2){
Serial.println("Button 2");
}
}
}
lastButtonState = reading;
}
Is there something I need to change in my code or is there an entirely different way I can approach this?
Thank you in advance, and I apologize if I haven't given enough information. I can provide more if necessary.
Testing_Buttons_for_Light_Alarm.ino (953 Bytes)