Hello!! I'd need a little advice for a project. I'm currently using a dfrobot lcd keypad shield, which has 6 button(SELECT, LEFT, RIGHT, UP, DOWN , RST). I want to write the code in such a way I'm able to display a message on the LCD only when a specific button is pressed. More especially, I'd want to display a message only when I press the SELECT button. The other buttons(except the RST button, of course) should do nothing when pressed. I currently run this code, which displays a message for every pressed button:
#include <LiquidCrystal.h>
//LCD pin to Arduino
const int rs=8, en=9, d4=4, d5=5, d6=6, d7=7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int pin_BL = 10;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Press Key:");
}
void loop() {
int x;
x = analogRead (0);
lcd.setCursor(10,1);
if (x < 60) {
lcd.print ("Right ");
}
else if (x < 200) {
lcd.print ("Up ");
}
else if (x < 400){
lcd.print ("Down ");
}
else if (x < 600){
lcd.print ("Left ");
}
if (x < 800){
lcd.print ("Select");
}
}
I tried to change it, but unfortunately I was unsuccessful, and browsing the internet for the past two hours wasn't helpful. Can someone help me with this implementation?
I'd have one more question...I can make the text appear when I press the SELECT button, but how could I make the text disappear using the same button? Here is the code I tried:
#include <LiquidCrystal.h>
//LCD pin to Arduino
const int rs=8, en=9, d4=4, d5=5, d6=6, d7=7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int pin_BL = 10;
bool buttonPressed=false;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,0);
}
void loop() {
int x;
x = analogRead (0);
//lcd.setCursor(10,1);
x = analogRead (0);
//lcd.setCursor(10,1);
if ((x >= 600 && x < 800)&&buttonPressed==false) {
buttonPressed=true;
lcd.print("Standby Mode");
}
if ((x >= 600 && x < 800)&&buttonPressed==true){
buttonPressed=false;
lcd.clear();
}
}
The problem is that the boolean variable will rapidly change its states while the button is being pressed, and the logics won't therefore work properly. Has anyone an idea of how to change the code?
hmm...if I were to visualize what's happening in the code...we needed another variable(in this case standbyMode) because we couldn't control the states of the programme using only buttonPressed variable. As the Arduino board runs this sequences of code very fast, buttonPressed var would change its states too rapidly, and the standby mode couldn't be entered. At least...this is what I think happens...
I think you partly understand the problem, but I don't think you've 100% got it yet.
The problem with your original code is that it is not detecting when the state of the "Select" button changes, only what it's current state is.
Your code can correctly read that the button is pressed, and change the mode to standby. But as you said, the code will execute several times during the time your finger is holding the button down. So the second time the code reads that the button is pressed, it changes the mode again, canceling standby mode. The third time, it changes the mode to standby again, the fourth time it cancels standby...
What the updated code does is read that the button is pressed, but it also checks that it was not pressed when the code was executed the previous time. Only then does it change the standby mode. The next time the code executes and the button is still pressed, it checks and finds that it was pressed on the previous execution, so there has been no change of state of the button, so the code does not change the standby status.