#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
// Data wire is connected to Arduino digital pin 2
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// LCD pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
// Stepper motor pins
#define STEP_PIN 8
#define DIR_PIN 9
#define ENABLE_PIN 10
const int MIN_SPEED = 1000; // Minimum motor speed (microseconds)
const int MAX_SPEED = 500; // Maximum motor speed (microseconds)
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Start LCD
lcd.begin(16, 2);
// Start DS18B20 temperature sensor
sensors.begin();
// Set stepper motor pins as output
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
// Enable the motor driver
digitalWrite(ENABLE_PIN, LOW);
}
void loop() {
// Request temperature from DS18B20
sensors.requestTemperatures();
float temperatureC = getManualTemperature(); // Get manual temperature input
// Print temperature to serial monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Display temperature on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print(" C");
// Vary motor speed based on temperature
int speed = map(temperatureC, 0, 100, MAX_SPEED, MIN_SPEED); // Assuming temperature range from 0 to 100
rotateMotor(speed);
// Print motor speed
Serial.print("Motor Speed: ");
Serial.println(speed);
delay(1000); // Delay before next reading
}
float getManualTemperature() {
Serial.println("Enter temperature (0-100 °C): ");
while (!Serial.available()) {} // Wait for user input
float temperature = Serial.parseFloat(); // Read user input
return temperature;
}
void rotateMotor(int speed) {
// Set direction of motor rotation (clockwise or counterclockwise)
digitalWrite(DIR_PIN, HIGH); // Set HIGH for clockwise, LOW for counterclockwise
// Generate a step pulse to move the motor
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(speed); // Adjust this delay based on desired speed
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(speed); // Adjust this delay based on desired speed
}