/*******************************************************
Yet another WMEVG, a UNO and 2 LCDs this time
Replace the pots with pressure sensors to balance carbs
Flip the switch to zoom into the "green" zone
********************************************/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize LCD at I2C address 0x27 (or 0x3F) for 20x4 display
LiquidCrystal_I2C lcd2(0x3F, 16, 2);
LiquidCrystal_I2C lcd1(0x27, 20, 4);
//const int analogPin = A0; // Pin reading the 0-1023 value
const int carbPin1 = A0; // Analog pin for sensor
const int carbPin2 = A1;
const int carbPin3 = A2;
const int carbPin4 = A3;
const int switchPin = 2; // Pin connected to the Zoom Switch
const int ledPin = 3;
int carbValue1 = 0;
int carbValue2 = 0;
int carbValue3 = 0;
int carbValue4 = 0;
// Define custom character blocks for smooth pixel-by-pixel bar drawing
byte barChars[5][8] = {
{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}, // 1 vertical line
{0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18}, // 2 vertical lines
{0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C}, // 3 vertical lines
{0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E}, // 4 vertical lines
{0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F} // 5 vertical lines (Solid block)
};
// State keeping variables to prevent screen flickering
bool lastSwitchState = true;
int lastcarbValue1 = -1;
int lastcarbValue2 = -1;
int lastcarbValue3 = -1;
int lastcarbValue4 = -1;
int switchState = 0;
// ----------------------------------------------------
// SMOOTH BAR RENDERER
// ----------------------------------------------------
void drawSmoothBar(int row, int val, long minVal, long maxVal) {
// Map value to 100 total possible increments (20 characters * 5 segments each)
int totalPixels = map(val, minVal, maxVal, 0, 100);
int fullChars = totalPixels / 5;
int remainderPixels = totalPixels % 5;
lcd1.setCursor(0, row);
// 1. Draw full solid blocks
for (int i = 0; i < fullChars; i++) {
lcd1.write(4); // Character 4 is the solid block
}
// 2. Draw the fractional block (if any)
if (remainderPixels > 0) {
lcd1.write(remainderPixels - 1); // Select proper custom character (0 to 3)
fullChars++; // Increment tracking to know how many spaces to clear
}
// 3. Clear the rest of the 20 character row
for (int i = fullChars; i < 20; i++) {
lcd1.write(' ');
}
}
//##############################################################################
void setup() {
pinMode(switchPin, INPUT); // LOW = Switch ON, HIGH = Switch OFF
pinMode(ledPin, OUTPUT);
Serial.begin(112500);
lcd1.init();
lcd2.init();
// Load custom characters into LCD memory (CGRAM slots 0 to 4)
for (int i = 0; i < 5; i++) {
lcd1.createChar(i, barChars[i]);
}
lcd1.clear();
lcd2.setCursor(0, 0);
lcd2.print("C1- ");
lcd2.setCursor(9, 0);
lcd2.print("C3- ");
lcd2.setCursor(0, 1);
lcd2.print("C2- ");
lcd2.setCursor(9, 1);
lcd2.print("C4- ");
}
//############################################################################
void loop() {
// Check if the toggle switch is flipped ON (HIGH)
if (digitalRead(switchPin) == HIGH) {
digitalWrite(ledPin, LOW); // Turn the LED indicator ON
switchState = 1;
// runContinuousFunction(); // Execute your code continuously
}
else {
digitalWrite(ledPin, HIGH); // Turn the LED indicator OFF
switchState = 0;
}
Serial.println(switchState);
int carbValue1 = analogRead(carbPin1);
int carbValue2 = analogRead(carbPin2);
int carbValue3 = analogRead(carbPin3);
int carbValue4 = analogRead(carbPin4);
if (carbValue1 <= 999) {
lcd2.setCursor(3, 0);
lcd2.print(carbValue1);
lcd2.setCursor(6, 0);
lcd2.print(" ");
} else {
lcd2.setCursor(3, 0);
lcd2.print(carbValue1);
}
if (carbValue2 <= 999) {
lcd2.setCursor(3, 1);
lcd2.print(carbValue2);
lcd2.setCursor(6, 1);
lcd2.print(" ");
} else {
lcd2.setCursor(3, 1);
lcd2.print(carbValue2);
}
if (carbValue3 <= 999) {
lcd2.setCursor(12, 0);
lcd2.print(carbValue3);
lcd2.setCursor(15, 0);
lcd2.print(" ");
} else {
lcd2.setCursor(12, 0);
lcd2.print(carbValue3);
}
if (carbValue4 <= 999) {
lcd2.setCursor(12, 1);
lcd2.print(carbValue4);
lcd2.setCursor(15, 1);
lcd2.print(" ");
} else {
lcd2.setCursor(12, 1);
lcd2.print(carbValue4);
}
delay(100);
bool switchState = digitalRead(switchPin); // Active LOW due to INPUT_PULLUP
Serial.print(switchState);
// Force redraw if switch toggles or if raw value changes significantly
// if (switchState != lastSwitchState || abs(rawValue - lastRawValue) > 2) {
// Clear lines to clean old artifacts if the switch just toggled
if (switchState != lastSwitchState) {
lcd1.clear();
lastSwitchState = switchState;
}
lastcarbValue1 = carbValue1;
lastcarbValue2 = carbValue2;
lastcarbValue3 = carbValue3;
lastcarbValue4 = carbValue4;
long minVal, maxVal;
// ----------------------------------------------------
// LOGIC CALCULATION & RANGE SWITCHING
// ----------------------------------------------------
if (switchState == LOW) {
// ZOOMED-IN MODE: 60% to 80% of 1023 = 0.2mb/div
minVal = 614; // 60% of 1023
maxVal = 818; // 80% of 1023
// Constrain raw value so it doesn't break the progress bar bounds
int constrainedValue1 = constrain(carbValue1, minVal, maxVal);
int constrainedValue2 = constrain(carbValue2, minVal, maxVal);
int constrainedValue3 = constrain(carbValue3, minVal, maxVal);
int constrainedValue4 = constrain(carbValue4, minVal, maxVal);
drawSmoothBar(0, constrainedValue1, minVal, maxVal);
drawSmoothBar(1, constrainedValue2, minVal, maxVal);
drawSmoothBar(2, constrainedValue3, minVal, maxVal);
drawSmoothBar(3, constrainedValue4, minVal, maxVal);
} else {
// NORMAL MODE: 0% to 100% of 1023
minVal = 0;
maxVal = 1023;
// drawSmoothBar(1, rawValue, minVal, maxVal);
drawSmoothBar(0, carbValue1, minVal, maxVal);
drawSmoothBar(1, carbValue2, minVal, maxVal);
drawSmoothBar(2, carbValue3, minVal, maxVal);
drawSmoothBar(3, carbValue4, minVal, maxVal);
}
delay(50); // Small delay to debounce and smooth performance
}