I have a Adafruit ESP32 Feather Huzzah board on which I run Arduino code. I have attached an LCD screen (16X2) and a HC-SR04 ultrasound distance meter. The aim is to display the amount of water sitting in my rain water tanks. The display shows percentage, total volume and a graph bar. This all works flawlessly with the following code:
#include <HCSR04.h>
#include <Wire.h>
#include "rgb_lcd.h"
#include <SPI.h>
// HC-SR04
UltraSonicDistanceSensor distanceSensor(15, 14);
// 16x2 LCD
rgb_lcd lcd;
// Variables
int MaxHeight = 200; //height of water tank
int MaxVol = 20000; //max vol in L of both water tanks
int CurVol = 0;
// Progress Bar characters
byte zero[] = {B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000};
byte one[] = {B10000, B10000, B10000, B10000, B10000, B10000, B10000, B10000};
byte two[] = {B11000, B11000, B11000, B11000, B11000, B11000, B11000, B11000};
byte three[] = {B11100, B11100, B11100, B11100, B11100, B11100, B11100, B11100};
byte four[] = {B11110, B11110, B11110, B11110, B11110, B11110, B11110, B11110};
byte five[] = {B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111};
// Function for Progress Bar
void updateProgressBar(unsigned long count, unsigned long totalCount, int lineToPrintOn)
{
double factor = totalCount / 80.0; // 16 characters x 5
int percent = (count + 1) / factor;
int number = percent / 5;
int remainder = percent % 5;
if (number > 0)
{
for (int j = 0; j < number; j++)
{
lcd.setCursor(j, lineToPrintOn);
lcd.write(5);
}
}
lcd.setCursor(number, lineToPrintOn);
lcd.write(remainder);
if (number < 16)
{
for (int j = number + 1; j <= 16; j++)
{
lcd.setCursor(j, lineToPrintOn);
lcd.write((byte) 0x00);
}
}
}
// Function to setup LCD screen
void setupLcd () {
lcd.begin(16, 2);
lcd.createChar(0, zero);
lcd.createChar(1, one);
lcd.createChar(2, two);
lcd.createChar(3, three);
lcd.createChar(4, four);
lcd.createChar(5, five);
lcd.clear();
}
void setup () {
Serial.begin(9600); // Start the serial output
setupLcd (); // Start LCD
lcd.print("(re)Starting...");
lcd.clear();
}
void loop () {
lcd.setCursor(6, 1);
float inputReading = distanceSensor.measureDistanceCm();
int CurPercentage = (100 - (inputReading / MaxHeight * 100));
int CurVol = MaxVol / 100 * CurPercentage;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(CurVol);
lcd.print(" L - ");
lcd.print(CurPercentage);
lcd.print(" %");
// update Progress Bar
updateProgressBar(CurPercentage, 100, 1);
delay(3000);
}
As this display is going to be sitting in my garage outside, I also want the latest information to be available on a web page (json object, so I can display it on a monitor inside the house. I added an Adafruit Ethernet Featherwing to the setup. I added Ethernet and Json libraries, and adapted the code. However, when I do this, the LCD screen flickers annoyingly. I have no idea how this happens (I'm completely new to Arduino IDE coding. Is there something wrong in my loop ?
#include <HCSR04.h>
#include <Wire.h>
#include "rgb_lcd.h"
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
// HC-SR04
UltraSonicDistanceSensor distanceSensor(15, 14);
// 16x2 LCD
rgb_lcd lcd;
// Wired Ethernet
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 30); // static IP not working due to changed ESP32/server.h
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80);
// Variables
int MaxHeight = 200; //height of water tank
int MaxVol = 20000; //max vol in L of both water tanks
int CurVol = 0;
// Progress Bar characters
byte zero[] = {B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000};
byte one[] = {B10000, B10000, B10000, B10000, B10000, B10000, B10000, B10000};
byte two[] = {B11000, B11000, B11000, B11000, B11000, B11000, B11000, B11000};
byte three[] = {B11100, B11100, B11100, B11100, B11100, B11100, B11100, B11100};
byte four[] = {B11110, B11110, B11110, B11110, B11110, B11110, B11110, B11110};
byte five[] = {B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111};
// Function for Progress Bar
void updateProgressBar(unsigned long count, unsigned long totalCount, int lineToPrintOn)
{
double factor = totalCount / 80.0; // 16 characters x 5
int percent = (count + 1) / factor;
int number = percent / 5;
int remainder = percent % 5;
if (number > 0)
{
for (int j = 0; j < number; j++)
{
lcd.setCursor(j, lineToPrintOn);
lcd.write(5);
}
}
lcd.setCursor(number, lineToPrintOn);
lcd.write(remainder);
if (number < 16)
{
for (int j = number + 1; j <= 16; j++)
{
lcd.setCursor(j, lineToPrintOn);
lcd.write((byte) 0x00);
}
}
}
// Function to connect to Ethernet & start server
void setupEthernet () {
Ethernet.init(33);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
server.begin();
Serial.println(F("Server is ready."));
Serial.print(F("Please connect to http://"));
Serial.println(Ethernet.localIP());
}
// Function to setup LCD screen
void setupLcd () {
lcd.begin(16, 2);
lcd.createChar(0, zero);
lcd.createChar(1, one);
lcd.createChar(2, two);
lcd.createChar(3, three);
lcd.createChar(4, four);
lcd.createChar(5, five);
lcd.clear();
}
// ========== MAIN FUNCTIONS: SETUP & LOOP ==========
void setup () {
Serial.begin(9600); // Start the serial output
setupLcd (); // Start LCD
lcd.print("(re)Starting...");
setupEthernet(); // Connect to Ethernet
delay(10000);
lcd.clear();
}
void loop () {
lcd.setCursor(6, 1);
float inputReading = distanceSensor.measureDistanceCm();
int CurPercentage = (100 - (inputReading / MaxHeight * 100));
int CurVol = MaxVol / 100 * CurPercentage;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(CurVol);
lcd.print(" L - ");
lcd.print(CurPercentage);
lcd.print(" %");
// update Progress Bar
updateProgressBar(CurPercentage, 100, 1);
// Wait for an incoming connection
EthernetClient client = server.available();
if (!client)
return;
Serial.println(F("New client"));
while (client.available()) client.read();
StaticJsonDocument<100> doc;
doc["data"] = CurPercentage;
// Write response headers
client.println(F("HTTP/1.0 200 OK"));
client.println(F("Content-Type: application/json"));
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(measureJsonPretty(doc));
client.println();
serializeJsonPretty(doc, client);
client.stop();
delay(3000);
}