#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
RTC_DS3231 rtc;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int buttonPinForward = 2;
const int buttonPinBackward = 3;
const int buttonPinDateTime = 4; // New button for switching display modes
const int tableSize = 6;
const int sensorPins[] = {A0, A1, A2, A3};
const int numSensors = sizeof(sensorPins) / sizeof(sensorPins[0]);
const char* sensorNames[] = { "* LAMBDA", "* ETHANOL", "* OIL", "* BOOST"};
const char* sensorSymbols[] = { "afr", "% ", "psi", "psi"};
float sensorValues[numSensors][tableSize] = {
{0.0, 1.0, 2.0, 3.0, 4.0, 5.0}, //Input voltage A0
{0.0, 1.0, 2.0, 3.0, 4.0, 5.0}, //Input voltage A1
{0.5, 1.3, 2.1, 2.9, 3.7, 4.5}, //Input voltage A2
{0.0, 1.0, 2.0, 3.0, 4.0, 5.0}, //Input voltage A3
};
int tableData[numSensors][tableSize] = {
{10, 12, 14, 16, 18, 20}, //Interpolated output value A0
{0, 20, 40, 60, 80, 100}, //Interpolated output value A1
{0, 20, 40, 60, 80, 100}, //Interpolated output value A2
{-12, 0 , 12, 24, 36, 48}, //Interpolated output value A3
};
int currentInputIndex = 0;
bool buttonPressed = false;
bool displaySensorData = false; // Variable to track the display mode
void setup() {
pinMode(buttonPinForward, INPUT_PULLUP);
pinMode(buttonPinBackward, INPUT_PULLUP);
pinMode(buttonPinDateTime, INPUT_PULLUP); // Set the new button as input with pull-up resistor
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5, 15);
display.println("Modded by @jaymangsr");
display.display();
delay(1000);
display.clearDisplay();
if (!rtc.begin()) {
display.println("RTC not found");
display.display();
while (1);
}
}
void loop() {
int inputValue = analogRead(sensorPins[currentInputIndex]);
float voltage = float(inputValue) * 5.0 / 1023.0;
int buttonForwardState = digitalRead(buttonPinForward);
int buttonBackwardState = digitalRead(buttonPinBackward);
int buttonDateTimeState = digitalRead(buttonPinDateTime);
if (buttonForwardState == LOW || buttonBackwardState == LOW) {
if (!buttonPressed) {
buttonPressed = true;
if (buttonForwardState == LOW) {
currentInputIndex = (currentInputIndex + 1) % numSensors;
}
if (buttonBackwardState == LOW) {
currentInputIndex = (currentInputIndex - 1 + numSensors) % numSensors;
}
}
} else {
buttonPressed = false;
}
if (buttonDateTimeState == LOW && !buttonPressed) {
buttonPressed = true;
displaySensorData = !displaySensorData;
}
if (displaySensorData) {
float outputValue = interpolate(sensorValues[currentInputIndex], tableData[currentInputIndex], tableSize, voltage);
display.clearDisplay();
display.setTextSize(2); //Sensor name
display.setCursor(5, 5);
display.println(sensorNames[currentInputIndex]);
display.setTextSize(3); //Interpolated value
display.setCursor(18, 24);
display.print(outputValue, 1);
display.setTextSize(2); //Sensor symbol
display.setCursor(90, 45);
display.println(sensorSymbols[currentInputIndex]);
display.setTextSize(0); //Input voltage
display.setCursor(5, 52);
display.print(voltage, 2);
display.println(" v");
display.display();
} else {
// Display date
display.clearDisplay();
DateTime now = rtc.now();
display.setTextSize(2);
display.setCursor(5, 42);
display.print(now.month(), DEC);
display.print('.');
display.print(now.day(), DEC);
display.print('.');
display.println(now.year(), DEC);
// Convert and display time as 12-hour format
display.setTextSize(3);
display.setCursor(24, 8);
int hour = now.hour();
if (hour == 0) {
display.print("12:");
} else if (hour <= 12) {
display.print(hour);
display.print(':');
} else {
display.print(hour - 12);
display.print(':');
}
display.print(now.minute(), DEC);
// Display AM or PM
display.setTextSize(1);
display.setCursor(5, 10);
if (hour < 12) {
display.print("AM"); //Am
} else {
display.print("PM"); //Pm
}
display.display();
}
}
float interpolate(float xValues[], int yValues[], int size, float x) {
if (x <= xValues[0]) {
return yValues[0];
} else if (x >= xValues[size - 1]) {
return yValues[size - 1];
} else {
for (int i = 0; i < size - 1; i++) {
if (x >= xValues[i] && x <= xValues[i + 1]) {
float slope = float(yValues[i + 1] - yValues[i]) / float(xValues[i + 1] - xValues[i]);
return yValues[i] + slope * (x - xValues[i]);
}
}
}
return 0.0;
}