#include <DHT.h>
#include <Adafruit_Sensor.h>
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(10, 11);
Servo servo1;
DHT dht(2, DHT11);
int currentAngle = 90;
String input = "";
// Настройка диапазона температур для шкалы
float minTemp = 0; // при 0°C стрелка на 0°
float maxTemp = 40; // при 40°C стрелка на 180°
void setup() {
dht.begin();
Serial.begin(9600);
bluetooth.begin(9600);
servo1.attach(9);
servo1.write(90);
delay(1000);
bluetooth.println("Термометр готов");
}
void loop() {
float t = dht.readTemperature();
if (!isnan(t)) {
// Преобразуем температуру в угол
currentAngle = map(t, minTemp, maxTemp, 0, 180);
currentAngle = constrain(currentAngle, 0, 180);
servo1.write(currentAngle);
}
// Обработка Bluetooth команд
while (bluetooth.available()) {
char c = bluetooth.read();
if (c == '\n') {
input.trim();
if (input == "temp") {
bluetooth.print("Температура: ");
bluetooth.print(t);
bluetooth.println("°C");
} else if (input == "angle") {
bluetooth.println(currentAngle);
} else if (input.startsWith("SET:")) {
int manualAngle = input.substring(4).toInt();
if (manualAngle >= 0 && manualAngle <= 180) {
currentAngle = manualAngle;
servo1.write(currentAngle);
bluetooth.println("ОК");
} else {
bluetooth.println("ERR");
}
}
input = "";
} else {
input += c;
}
}
delay(100);
}