Hey everyone, I am trying to let a lcd screen display something when turning it on, and when turning it over(tilt sensor) to display another random message. After uploading the code and plugging the board to power, it only turns on but does not display any characters. Also, I have a potentiometer attached to regulate the contrast, however this does not change anything. Sometimes, some lines/dots appear on the display, and sometimes entire blocks are lit up. Is the display maybe broken?
Thanks in advance,
Daniel
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup() {
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
lcd.print("Frag den");
lcd.setCursor(0, 1);
lcd.print("Fussballgott");
}
void loop() {
switchState = digitalRead(switchPin);
if (switchState = prevSwitchState) {
if(switchState ==LOW) {
reply = random(2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Klarer Sieg:");
lcd.setCursor(0, 1);
switch(reply){
case 0:
lcd.print("3:0");
break;
case 1:
lcd.print("4:1");
}
}
}
prevSwitchState = switchState;
}
Is the RW (LCD pin 5) connected to ground? A schematic of your wiring and a photo or 2 will help us to help you.
How is the tilt switch wired? Is there a pullup or pulldown on the input connected to the switch?
Have you tried the "Hello World" example that comes with the LiquidCrystal library that you are using?
if (switchState = prevSwitchState)
Shouldn't that be
if (switchState != prevSwitchState) // if the current state is different from the last state
Single = is assignment, == is comparison.
See the if structure reference.
Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.
If you had compile warnings in the IDE (File, Preferences) a warning would have been generated by that line of code.
I made a couple of change to your code, loaded it to an Uno with 16x2 LCD and ran the code. The code performs as expected so it is not a software problem. Either your wiring is wrong or the display is broken.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup()
{
lcd.begin(16, 2);
pinMode(switchPin, INPUT_PULLUP); // enable internal pullup
lcd.print("Frag den");
lcd.setCursor(0, 1);
lcd.print("Fussballgott");
}
void loop()
{
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) // if the current state is different from the last state
{
if (switchState == LOW)
{
reply = random(2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Klarer Sieg:");
lcd.setCursor(0, 1);
switch (reply)
{
case 0:
lcd.print("3:0");
break;
case 1:
lcd.print("4:1");
}
}
}
prevSwitchState = switchState;
}
Wow, thank you groundFungus for your effort of running it on an lcd screen! Yes, I had the wiring wrong and did not connect 5 to ground. Also, the != was correct as you said. Very cool: it works! I am so proud of this community 