Hi all,
I am very new to Arduino. I recently bought the Beginner Kit for Arduino from DFRobot (SKU:DFR0100) and am trying to code a servo to rotate based on temperature. Essentially I would like the servo to be at 179 degrees when under 25 celsius and at 0 degrees when it is over 25 celsius. However my code is sadly not working. I don't have a good understanding of code, so please explain as much as you can!
#include <Servo.h>
Servo myservo; // create servo object to control a servo
unsigned long tepTimer ;
int threshold = 25; // Threshold for temperature
int servocold = 0; // Angle in which servo will go to
int servohot = 179; // Angle in which servo will go to
void setup(){
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop(){
int val;
double data;
val=analogRead(0);
data = (double) val * (5/10.24); // convert the voltage to temperture
if(data < threshold){ // If the temperture is over 27 degree, buzzer will alarm.
myservo.write(servocold);
}
if else (data > threshold); // If temperature is above the threshold, activate sequence
{
myservo.write(servohot);
}
if(millis() - tepTimer > 500){ // output the temperture value per 500ms
tepTimer = millis();
Serial.print("temperature: ");
Serial.print(data);
Serial.println("C");
}
}
I apologize in advance if it's a complete mess but I really need help!