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?
//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;
}