#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
const int ledPin1 = 27;
const int ledPin2 = 26;
const int tempSensor = 33;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
//Temperature Threshold
const int temp_threshold = 25;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 19, 18, 5, 17);
LiquidCrystal_I2C lcd( 0x27 , 16, 2);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// LCD Set Up
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("The IoT Thermal System!!");
}
void loop(){
//lcd.clear();
//lcd.setCursor(0,1);
//lcd.print("The IoT Thermal System!!");
//int temp_value = analogRead(tempSensor);
float temp_value = getTemperature();
delay(1000);
if(temp_value > temp_threshold){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Temperature ABOVE OPTM!!");
motorStart();
blinkLed(ledPin1);
blinkLed(ledPin2);
}
else if(temp_value == temp_threshold){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Temperature AT OPTM!!");
}
else{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Temperature AT OPTM!!");
}
}
float getTemperature() {
int analogValue = analogRead(tempSensor);
float celsius = 1 / (log(1 / (4096.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
return celsius;
//delay(1000);
}
void motorStart() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
void blinkLed(int pin){
digitalWrite(pin , HIGH);
delay(200);
digitalWrite(pin , LOW);
delay(200);
}