#include <Arduino.h>
#include <Arduino_FreeRTOS.h>
#include <Servo.h>
// Definições de Pinos
const int sensorPin = A0; // Pino para o sensor de temperatura
const int servoPin = 11; // Pino para o servo motor
Servo myServo; // Cria um objeto servo
int digital_output ; // This will read the digital value
int analog_output ; // This will read the analog value
int revised_output; // variable to store the corrected value
float temp_C ; // Variable for storing the temperature
float temp_f ;
double Thermistor ( int RawADC )
{
double Temp ;
Temp = log (10240000 / RawADC - 10000);
Temp = 1 / ( 0.001129148 + ( 0.000234125 * Temp ) + ( 0.0000000876741 * Temp * Temp * Temp ) ) ;
Temp = Temp - 273.15 ; // This will Convert Kelvin to Celcius
return Temp ;
}
// Função para ler e converter a temperatura
float readTemperature() {
int sensorValue = analogRead(sensorPin);
revised_output= map (sensorValue, 0, 1023, 1023, 0 ) ;
temp_C = Thermistor ( revised_output ) ;
return temp_C;
}
// Tarefa para controlar servo motor
void TaskServoControl(void *pvParameters) {
(void) pvParameters;
myServo.attach(servoPin);
while (1) {
float temperature = readTemperature();
// Serial.println(temperature);
Serial.println(temperature);
if (temperature < 25.0) {
myServo.write(0); // Rotação em uma direção
} else {
myServo.write(180); // Rotação na direção oposta
}
vTaskDelay(1000 / portTICK_PERIOD_MS); // Atualiza a cada segundo
}
}
void setup() {
Serial.begin(115200);
// Criação das tarefas
xTaskCreate(TaskServoControl, "ServoControl", 128, NULL, 1, NULL);
}
void loop() {
// Nada aqui, as tarefas são gerenciadas pelo FreeRTOS
}