LCD Display using NodeMCU ESP8266

I’m having trouble with my project. I use ACS712 and NodeMCU ESP8266 for my project, but how do I make the 16x2 LCD display LOAD OFF when the AC Power is below 10 Watts, and LOAD ON when the Power is above 10 Watt. thank you
here is my code.

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int sensorIn = A0;
int mVperAmp = 66;
int Watt = 0;

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void setup() {
Serial.begin (9600);
Serial.println (“ACS712 current sensor”);
lcd.begin();
lcd.backlight();
lcd.print(“Robojax ACS712”);
lcd.setCursor(0,1);
lcd.print(“Demo”);
delay(2000);

}

void loop() {

Serial.println ("");

Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;

Serial.print(AmpsRMS);
Serial.print(" Amps RMS — “);
Watt = (AmpsRMS*240/1.3);
Serial.print(Watt);
Serial.println(” W");
lcd.clear();
lcd.setCursor (0,0);
lcd.print("PLN ON: ");
lcd.print(Watt);
lcd.print(“W”);

}

float getVPP()
{
float result;
int readValue;
int maxValue = 0;
int minValue = 1024;
int LIMIT = 10;

uint32_t start_time = millis();
while((millis()-start_time) < 1000)
{
readValue = analogRead(sensorIn);
if (readValue > maxValue)
{
maxValue = readValue;
}
if (readValue < minValue)
{
minValue = readValue;
}
}

result = ((maxValue - minValue) * 5.0)/1024.0;

return result;
}

void lcd_control (){
lcd. setCursor (0, 1);
lcd.print (Watt);
lcd.print (" watt ");
delay (100);
}

Maybe change this line to:
lcd.print(Watt < 10 ? "LOAD OFF: " : "LOAD ON: ");

The 'A ? B : C' operator is the 'ternary' operator. If A is true it evaluates to B. If A is false it evaluates to C.

Edit: Fixed a typo. I failed to put in the ':' part of the operator!

@johnwasser
lcd.print(Watt < 10 ? "LOAD OFF: " "LOAD ON: ");
If I am not mistaken there could be a small typo in your post. ( : is missing)
lcd.print(Watt < 10 ? "LOAD OFF: " : "LOAD ON: ");
Br,
Johi.

Correct. I will fix it now.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.