GustySpace10
Coder
For a project I am doing at the moment, I have to have a servo motor turn 180° and have a piezo sound an alarm when the temperature goes above 18°C. I also have to display the temperature on an LCD.
I have gotten the piezo to sound the alarm when the temperature goes above 18°C and I have gotten the LCD to display the current temperature but when I connect the motor and upload my code to the Arduino board the thermistor seems to start to fluctuate and display temperatures above 18°C then below 18°C on the LCD and in turn that turns on the piezo and gets the motor to turn 180°.
This is the code that I am using:
Could it be a problem with my code or a wiring issue?
Any help is much appriecated!!
I have gotten the piezo to sound the alarm when the temperature goes above 18°C and I have gotten the LCD to display the current temperature but when I connect the motor and upload my code to the Arduino board the thermistor seems to start to fluctuate and display temperatures above 18°C then below 18°C on the LCD and in turn that turns on the piezo and gets the motor to turn 180°.
This is the code that I am using:
C++:
/* Set the sensorPin variable to pin A0
* Make a variable to store the value of the sensor
* Make a variable to store the calculated voltage
* Make a variable to store the calculated temperature
*/
#include <Servo.h>
Servo CoelsServo;
int sensorPin = A0;
int sensorValue = 0;
int voltageValue = 0;
int temperatureValue = 0;
int note = 330;
#include <LiquidCrystal.h> // Include the library code
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the library with the numbers of the interface pins
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2); // Set up the LCD's number of columns and rows:
lcd.print("Temperature = "); // Print a message to the LCD to tell you what the temperature is
CoelsServo.attach(9);
delay(100);
CoelsServo.write(0);
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue = analogRead(sensorPin); // Read the value coming from the sensor - values are between 0 and 1023
voltageValue = (sensorValue) * (5000/1024); // Convert the value of the sensor to a Voltage value
temperatureValue = ((voltageValue)-500)/10; // Convert the Voltage Value to a Temperature in Degrees Celcius
lcd.setCursor(0, 1); // set the cursor to column 0, line (note: line 1 is the second row, since counting begins with 0)
lcd.print(temperatureValue); // Print what the temperature is on the LCD screen
lcd.print(" C");
if(temperatureValue > 18){
tone(7, 330);
}
if(temperatureValue > 18){
CoelsServo.write(180);
}
else{
CoelsServo.write(0);
delay(100);
noTone(7);
}
delay(1000); // Delay of 1 second
}
Any help is much appriecated!!
Last edited by a moderator: