#include <Arduino.h>
// Initial constants for the linear model
float slope = 0.5; // Slope (fixed for simplicity)
float intercept = 10; // Initial intercept
// Pin assignments
const int tempSensorPin = A0; // Analog pin connected to the temperature sensor
const int ledPinAdjust = 9; // Pin connected to an LED indicating adjustment
// Learning rate
const float learningRate = 0.01;
// Function to predict the next temperature
float predictTemperature(float currentTemp) {
return slope * currentTemp + intercept;
}
// Function to update the model based on the actual next temperature measured
void updateModel(float currentTemp, float actualNextTemp) {
float error = actualNextTemp - predictTemperature(currentTemp);
intercept += learningRate * error; // Simple update rule for intercept
}
void setup() {
pinMode(ledPinAdjust, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(tempSensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
float currentTemp = (voltage - 0.5) * 100.0; // Convert voltage to temperature
// Simulate getting the actual next temperature (this would be another sensor reading in reality)
float actualNextTemp = currentTemp + random(-2, 3); // Random fluctuation for demonstration
float predictedTemp = predictTemperature(currentTemp);
updateModel(currentTemp, actualNextTemp);
Serial.print("Current Temperature: ");
Serial.print(currentTemp);
Serial.print("°C, Predicted Next Temperature: ");
Serial.print(predictedTemp);
Serial.print("°C, Actual Next Temperature: ");
Serial.println(actualNextTemp);
Serial.print("Updated Intercept: ");
Serial.println(intercept);
digitalWrite(ledPinAdjust, HIGH);
delay(200); // LED on briefly to indicate processing
digitalWrite(ledPinAdjust, LOW);
delay(1000); // Wait for a second before the next reading
}