#define nexSerial Serial2
// Slider update functions
void nexEndCmd() {
nexSerial.write(0xff);
nexSerial.write(0xff);
nexSerial.write(0xff);
}
void updateSlider(const char* label, int value) {
nexSerial.print(label);
nexSerial.print(".val=");
nexSerial.print(value);
nexEndCmd();
delay(10);
}
// Interpolation function
float interpolate(float x, const float* xTable, const float* yTable, int size) {
if (x <= xTable[0]) return yTable[0];
if (x >= xTable[size - 1]) return yTable[size - 1];
for (int i = 0; i < size - 1; i++) {
if (x >= xTable[i] && x <= xTable[i + 1]) {
return yTable[i] + (yTable[i + 1] - yTable[i]) * (x - xTable[i]) / (xTable[i + 1] - xTable[i]);
}
}
return yTable[0]; // Fallback (shouldn't occur)
}
// Temperature mapping for J0
const float voltageTableTemp[] = {0.725, 1.043, 1.429, 1.847, 2.299, 2.726, 3.133, 3.482, 3.625, 3.779, 3.897, 4.023, 4.115, 4.212};
const float tempTable[] = {0, 10, 20, 30, 40, 50, 60, 70, 75, 80, 85, 90, 95, 100};
// PSI mapping for J1
const float voltageTablePSI[] = {0.5, 0.9, 1.3, 1.6, 1.9, 2.2, 2.5, 2.8, 3.1, 3.4, 4.5};
const float psiTable[] = {0, 14.503, 29.075, 43.51, 58.01, 72.51, 87.02, 101.526, 116.03, 130.534, 145.038};
// Oil temperature mapping for J2
const float voltageTableOilTemp[] = {0.737, 1.055, 1.437, 1.864, 2.307, 2.741, 3.137, 3.302, 3.485, 3.625, 3.778, 3.895, 4.019, 4.114, 4.214, 4.369};
const float oilTempTable[] = {0, 10, 20, 30, 40, 50, 60, 65, 70, 75, 80, 85, 90, 95, 100, 110};
// Helper to read analog pin and convert to voltage
float readVoltage(int pin) {
return analogRead(pin) * (5.0 / 1023.0);
}
void setup() {
Serial.begin(9600);
nexSerial.begin(9600); // Nextion serial communication
}
void loop() {
// Read voltages
float voltageA0 = readVoltage(A0);
float voltageA1 = readVoltage(A1);
float voltageA2 = readVoltage(A2);
// Interpolate values
int tempValue = interpolate(voltageA0, voltageTableTemp, tempTable, sizeof(tempTable) / sizeof(tempTable[0]));
int psiValue = interpolate(voltageA1, voltageTablePSI, psiTable, sizeof(psiTable) / sizeof(psiTable[0]));
int oilTempValue = interpolate(voltageA2, voltageTableOilTemp, oilTempTable, sizeof(oilTempTable) / sizeof(oilTempTable[0]));
Serial.print("Voltage A0:");
Serial.print(voltageA0);
Serial.print(", Temp Value:");
Serial.println(tempValue);
Serial.print("Voltage A1:");
Serial.print(voltageA1);
Serial.print(", PSI Value:");
Serial.println(psiValue);
Serial.print("Voltage A2:");
Serial.print(voltageA2);
Serial.print(", Oil Temperature:");
Serial.println(oilTempValue);
// Update sliders
updateSlider("j0", tempValue); // Update temperature slider
delay(50); // Wait 50 milliseconds after sending a value
updateSlider("j1", psiValue); // Update PSI slider
delay(50); // Wait 50 milliseconds after sending a value
updateSlider("j2", oilTempValue); // Update oil temperature slider
delay(1000); // Update every 1000 ms
}