#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
RTC_DS3231 rtc; // Initialize the RTC module
// Define the LCD pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int analogPin = A0; // Analog input pin
// Define your input voltage range (0-5V) and the corresponding reference data.
const float voltageMin = 0.0;
const float voltageMax = 5.0;
const int tableSize = 5; // Number of data points in the table
const float voltageTable[tableSize] = {0, 2.5, 5}; // Voltage values in the table
const int dataTable[tableSize] = {10, 15, 20}; // Corresponding reference data
void setup() {
lcd.begin(16, 2); // Initialize the LCD: 16 columns and 2 rows
lcd.clear();
}
void loop() {
// Read the analog input and convert it to voltage
int rawValue = analogRead(analogPin);
float voltage = (rawValue / 1023.00) * voltageMax;
// Interpolate the input voltage to find the corresponding reference data
float interpolatedData = interpolateData(voltage);
// Display voltage and interpolated data on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage, 2); // Display voltage with 2 decimal places
lcd.setCursor(0, 1);
lcd.print("AFR: ");
lcd.print(interpolatedData, 2);
delay(1000); // Update the display every second
}
// Function to interpolate data based on the input voltage
float interpolateData(float voltage) {
if (voltage <= voltageTable[0]) {
return static_cast<float>(dataTable[0]);
}
for (int i = 1; i < tableSize; i++) {
if (voltage <= voltageTable[i]) {
// Linear interpolation
float slope = static_cast<float>(dataTable[i] - dataTable[i - 1]) / (voltageTable[i] - voltageTable[i - 1]);
float interpolatedValue = static_cast<float>(dataTable[i - 1]) + slope * (voltage - voltageTable[i - 1]);
return interpolatedValue;
}
}
return static_cast<float>(dataTable[tableSize - 1]); // If the voltage is out of range
}