Hello guys.
I need to control the damper of the smokehouse and thereby control the temperature. The damper position is driven by a servo motor according to the temperature. 0° - 90°. In the code it is currently 180°, but in practice it will only be 90°. The code works, but I haven't tried it in practice yet. So far only a microservo. I don't know how to display the variable temperature Min and Max on the LCD and I also need to change these values preferably with two potentiometers.
if (tempCelsius >= 10 and tempCelsius <= 60)
Thank you very much for any advice.
/* Atmega 328 (Nano)
sensor type DS18B20
22/01/2024
*/
#include <Servo.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 5
const int SERVO_PIN = 5; // Arduino pin connected to Servo Motor's pin
const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin
const int i2c_addr = 0x27;
const float TEMPERATURE_THRESHOLD = 30; // °C 20
//const float TOLERANCE = 0.5; // °C
Servo servo; // create servo object to control a servo
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library
LiquidCrystal_I2C lcd(0x27, 20, 4);
float tempCelsius; // temperature in Celsius
float servoposraw;
float servopos;
void setup() {
Serial.begin(9600); // initialize serial
servo.attach(SERVO_PIN); // attaches the servo on pin 5 to the servo object, pin 5? servo.write(0);
servo.write(0);
sensor.begin(); // initialize the sensor
lcd.init(); // initialize the lcd
lcd.begin(20, 4);
lcd.backlight(); // open the backlight
}
void loop() {
delay(1000);
sensor.requestTemperatures(); // send the command to get temperatures
tempCelsius = sensor.getTempCByIndex(0); // read temperature in Celsius
if (tempCelsius >= 10 and tempCelsius <= 60) {
servoposraw = (180. / 60.) * tempCelsius;
//přidat mapování teploty a uhlu serva https://forum.arduino.cc/t/controlling-a-servo-position-using-a-temperature-sensor/374543/8
servopos = 180 - servoposraw;
servo.write(servopos);
Serial.println(servopos);
Serial.print("Temperature: ");
Serial.print(tempCelsius);
//Serial.print(tempC,1); // The 1 behind the tempC (second argument) defines the numbers of digits required
Serial.print("°C => servo angle: ");
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Temperature: ");
lcd.print(tempCelsius,0); // print the temperature in Celsius
lcd.print((char)223); // print ° character
lcd.print("C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print("Angle : "); // print the temperature in Fahrenheit
lcd.print(servopos,0);
lcd.print((char)223); // print ° character
//lcd.print("°");
lcd.setCursor(0, 2);
lcd.print("Min: ");
//lcd.print(???);
lcd.setCursor(0, 3);
lcd.print("Max: ");
// lcd.print(???);
}
//delay(150);
}