I set up a circuit with lcd to display a certain text when the light intensity changes. But the ldr seems not working as from the beginning, the lcd only shows the text that should only be displayed when I turned on the light, even when I turned off the light. Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
int ldrPin = 0;
String defText1 = "Good bye.";
String defText2 = "Hello.";
boolean newData = false;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
Serial.println("");
}
void loop() {
int ldrVal = analogRead(ldrPin);
delay(150);
if(ldrVal<20){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(defText1);
}
else if(ldrVal>20){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(defText2);
}
recvWithEndMarker();
showNewData();
lcd.setCursor(0,1);
lcd.print(receivedChars);
delay(1000);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.println(receivedChars);
newData = false;
}
}
