Hello, I'm working on a project where I want to push a button switch and print "ON" to the LCD screen and then "OFF" when I release my finger from the button. So far I was able to print both on the LCD screen, however when I press the button there is a delay of 6 to 10 seconds before it. I want it to print "OFF" immediately when I release the button. What are some suggestions I could change in order to do so? Any thing would help.
[code]
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int Test_pin=7;
int state=0;
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(Test_pin,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
state=digitalRead(Test_pin);
switch(state) {
case LOW:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("OFF"); // LCD screen displays "OFF" when button is not pressed
break;
case HIGH:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ON"); //LCD screen displays "ON" when button is pressed
break;
}
Serial.println(state);
delay(500);
}
[/code]
Do you realize that in either pin states, you're essentially updating your display twice every second with mostly the same information? Personally I prefer to only print to a display if there's anything to print that's different from what's already on there.
Do you have a pulldown resistor in place to keep it at a LOW voltage when the button is not pressed or is it floating at an unknow, maybe HIGH, maybe LOW voltage when not pressed ?
Consider using INPUT_PULLUP in pinMode() to activate the built in pullup resistor to avoid the need for an external component
I have my switch connected to pin 7 and the other to 5Volts. So I would change pinMode(Test_pin,INPUT); to pinMode(Test_pin,INPUT_PULLUP); ? I made that change but when I reupload it the screen only displays "ON" and nothing changes when I press the button.
Yeah, that won't work. As you observed, that way the input always remains high. Try leaving the internal_pullup in your sketch but tie the switch to the pin and gnd instead of pin and 5v.
Your reply actually explains why you had this oddly long delay before a button release was noted by the arduino -when not pressed, the button effectively floated. A tiny leakage current would slowly take the pin back low after releasing the button, with a long and somewhat random delay.