I am an arduino beginner and ran into a problem. I tried to control a lcd display with a button input, but when I upload my code the disply flickers. Here is my code:
No, but I've changed my code and now it won't upload to my lcd anymore. In arduino ide it said: upload succesfull, but the code wasn't shown on the lcd.
The lcd has an I2C adapter. Ground goes from the lcd to the breadboard which is connected to ground on my arduino. The same for 5v. The output pins from the lcd are connected to my arduino in A4 and A5. The button is connected with a ressistor and has an output to pin 3. The rest of there to complete the circuits.
You need to use setCursor() just before you print your message. Once you do that you will have another query about why it seems to print "pressedssed", unless you deal with that yourself
One problem you may be running into is the fact that "pressed" or "not pressed" is being printed as fast as the microcontroller can handle.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int buttonPin = 3;
const int ledPin = 2;
volatile bool buttonIsPressed = false;
void setup() {
lcd.setCursor(0,0);
attachInterrupt(digitalPinToInterrupt(buttonPin), pressEvent, RISING);
}
void loop() {
if(buttonIsPressed)
myLCDprint("pressed");
else
myLCDprint("not pressed");
}
//Good practice to keep the ISR as short as possible
void pressEvent(){
buttonIsPressed = true;
}
//Adding a delay after a print will help with flickering
void myLCDprint(const char *str){
lcd.print(str);
delay(100);
buttonIsPressed = false;
}
now, the lcd will only re-write every 100 ms at most. By using interrupt service routines (ISR), the "print" message will only print once even if you have the button held down, so you won't get the "pressed" message multiple times when you only press the button once.