LCD Display not responding in loop function

Hey everybody,

I often read posts in this forum for troubleshooting and normally I can find a solution. But now I neither find another posts with the same issue nor I can find a mistake in my code.

I am developing a “remote control” which contains 2 Buttons, 1 RF transmitter and 1 LCD Display. Everything is working except the LCD Display.

The lcd.print() is working properly in my setup function, but not in my loop function. I have obviously tried it with my own code and tried it with library examples. The examples are working fine in their sketches, but as soon as I copy the code into mine, the part in the loop function stops working.

I have checked the wiring, the setup…I really cannot find the mistake. Can anyone help please? :frowning:

//libraries
#include <VirtualWire.h>
#include <LiquidCrystal.h>
#include <Bounce2.h> 

#define PIN_LCD_RS 0                       // Pin für LCD-Pin RS  (Register Select)
#define PIN_LCD_E 1                        // Pin für LCD-Pin E   (Enable)
#define PIN_LCD_D4 8                       // Pin für LCD-Pin D4  (Datenbit 4)
#define PIN_LCD_D5 9                       // Pin für LCD-Pin D5  (Datenbit 5)
#define PIN_LCD_D6 10                      // Pin für LCD-Pin D6  (Datenbit 6)
#define PIN_LCD_D7 11                      // Pin für LCD-Pin D7  (Datenbit 7)
#define LCD_ROWS 2                         // Anzahl der Zeilen des Displays.
#define LCD_COLS 16                        // Anzahl der Spalten des Displays.

// binding PIN numbers to variables:
const int  buttonSceneOne = 4;    // the pin to control Scenario 1
const int  buttonSceneTwo = 6;    // the pin to control Scenario 2
const int led = 13;       // the pin that the LED is attached to
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
LiquidCrystal lcd(PIN_LCD_RS, PIN_LCD_E, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7);

// Variables will change:
boolean timerState = false;   // state of timer and trigger for the scenario
unsigned long start, finished, elapsed;
int buttonOneState = 0;         // current state of the button 1
int lastButtonOneState = 0;     // previous state of the button 1
int buttonTwoState = 0;         // current state of the button 2
int lastButtonTwoState = 0;     // previous state of the button 2
char *msg;// message between master and slave


void setup() {
  ///// DISPLAY ///////
  // set up the LCD’s number of columns and rows:
  lcd.begin(LCD_COLS, LCD_ROWS);      
  lcd.setCursor(0,0);
  lcd.print("System ready");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Szenario: ");
  lcd.setCursor(0,1);
  lcd.print("Zeit: ");
  
  
  ///////   BUTTON     ////////////
  // initialize the button pin as a input:
  pinMode(buttonSceneOne, INPUT);
  pinMode(buttonSceneTwo, INPUT);
  debouncer1.attach(buttonSceneOne);
  debouncer1.interval(5);
  debouncer2.attach(buttonSceneTwo);
  debouncer2.interval(5);

  //////  TRIGGER COMMUNICATION ////
  Serial.begin(38400);
  pinMode(13, OUTPUT);
  vw_set_ptt_inverted(true); //
  vw_set_tx_pin(12);
  vw_setup(4000);// speed of data transfer Kbps     
}

void loop() {
  //update debouncer
  debouncer1.update();
  debouncer2.update();
  // read the pushbutton input pin:
  buttonTwoState = debouncer2.read();
  buttonOneState = debouncer1.read();

  // compare the buttonState to its previous state
  if ((buttonOneState != lastButtonOneState) && (buttonTwoState == 0)) {
    // if the state has changed, increment the counter
    if (buttonOneState == 1) {

      //begin Timer
      if (!timerState) {
        //Serial.println("Starte Szenario 1 und stoppe Zeit.");
        lcd.clear();
        lcd.setCursor(11,0);
        lcd.write(1);
        delay(100);
        timerState = true;
        start = millis();

        //send msg "1"
        msg = "1";
        vw_send((uint8_t *)msg, strlen(msg));
        vw_wait_tx(); // Wait until the whole message is gone
        digitalWrite(13, 1);
        Serial.println("Nachricht gesendet: 1");
      } else {
        timerState = false;
        finished = millis();
        //stopped time
        int   s, ms;
        unsigned long over;
        elapsed = finished - start;
        over = elapsed % 3600000;
        over = over % 60000;
        s =  int  (over / 1000);
        ms = over % 1000;
        Serial.print(s);
        Serial.println("s ");
        Serial.print(ms);
        Serial.println("ms");

        //Serial.println("Szenario beendet.");
        lcd.setCursor(7,1);
        //lcd.print("%d s : %d ms", s, ms);
        //send msg "0"
        msg = "0";
        vw_send((uint8_t *)msg, strlen(msg));
        vw_wait_tx(); // Wait until the whole message is gone
        digitalWrite(13, 0);
        Serial.println("Nachricht gesendet: 0");
      }


    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }


  if ((buttonTwoState != lastButtonTwoState) && (buttonOneState == 0)) {
    // if the state has changed, increment the counter
    if (buttonTwoState == 1) {
      //begin Timer
      if (!timerState) {
        //Serial.println("Starte Szenario 2 und stoppe Zeit.");
        lcd.clear();
        lcd.setCursor(11,0);
        lcd.write(2);
        timerState = true;
        start = millis();

        //send msg "2"
        msg = "2";
        vw_send((uint8_t *)msg, strlen(msg));
        vw_wait_tx(); // Wait until the whole message is gone
        digitalWrite(13, 1);
        Serial.println("Nachricht gesendet: 2");
      } else {
        timerState = false;
        finished = millis();
        //stopped time
        int   s, ms;
        unsigned long over;
        elapsed = finished - start;
        over = elapsed % 3600000;
        over = over % 60000;
        s =  int  (over / 1000);
        ms = over % 1000;
        Serial.print(s);
        Serial.println("s ");
        Serial.print(ms);
        Serial.println("ms");

        //Serial.println("Szenario beendet.");
        lcd.setCursor(7,1);
        //lcd.print("%d s : %d ms", s, ms);
        //send msg "0"
        msg = "0";
        vw_send((uint8_t *)msg, strlen(msg));
        vw_wait_tx(); // Wait until the whole message is gone
        digitalWrite(13, 0);
        Serial.println("Nachricht gesendet: 0 - warum nur?!");
      }


    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }


  // save the current state as the last state, for next time through the loop
  lastButtonOneState = buttonOneState;
  lastButtonTwoState = buttonTwoState;

}

What happens if you use lcd.print() instead of lcd.write() ?

I don't know if this is it.

You use pins 0 and 1 for your LCD, lcd.print in setup() then do a serial.begin().

Doesn't serial use pins 0 and 1?

evanmars:
Doesn't serial use pins 0 and 1?

Yes it does and you can't use those pins for both things at the same time.

Hi,

many thanks for your fast response! I was not aware that Serial uses PIN 0 and 1. I just used it for debugging, so I deleted Serial.begin(), but it didn´t work either.

But it was a helpful clue because I haven´t thought about "invisible" Hardware setups. I found a conflict between the library VirtualWire and my LCD PIN setup.

Default settings of VirtualWire are blocking the Pins I wanted to use for LCD, so I had to redefine the pins although I am not using these parts in my code.

I found it in these posts:

  vw_set_ptt_inverted(true); //
  vw_set_tx_pin(12);
  vw_set_rx_pin(13);        //not in use but has to be defined to avoid conflicts with LCD Screen
  vw_set_ptt_pin(7);        // rx pin deault: PIN 11, ptt pin default: PIN 10
  vw_setup(4000);// speed of data transfer Kbps

Many thanks!!!

p.s. I tried using Serial.begin() after fixing the other problem.....and you are right. Doesn´t work :smiley: just had to check it by myself