• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ Having problems with a servo motor and thermistor.

GustySpace10

New 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:

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
}
Could it be a problem with my code or a wiring issue?
Any help is much appriecated!!
 
Last edited by a moderator:

Malcolm

Administrator
Administrator
Staff Team
Hi @GustySpace10, Welcome to Code Forum!

Please remember for next time that you need to place all your code in the </> editor option. It makes your code easier to read and understand.

Give me a bit to look over your code to see if I have an answer, however I’m not an expert in this field. We’ll try our best to get this answered for you.
 

GustySpace10

New Coder
Hi @GustySpace10, Welcome to Code Forum!

Please remember for next time that you need to place all your code in the </> editor option. It makes your code easier to read and understand.

Give me a bit to look over your code to see if I have an answer, however I’m not an expert in this field. We’ll try our best to get this answered for you.
Hi Malcolm, I noticed the alert on the top of my post so I re posted it using BBCode if that makes it easier!
 

LTomy

Active Coder
Staff Team
Guardian
I had a quick look at your code and I did not detect any error. I could have helped with C++ errors, but I know nothing about Arduino nor the library you use.
 

Krusty the Senile

Active Coder
Possible suggestion:
C:
int aboveEighteen = 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) {
    if (!aboveEighteen) {
      tone(7, 330);
      CoelsServo.write(180);
      aboveEighteen = 1;
    }
  } else {
    if (aboveEighteen) {
      CoelsServo.write(0);
      delay(100);
      noTone(7);
      aboveEighteen = 0;
    }
  }

  delay(1000); // Delay of 1 second
}
Not sure if this is the solution you'd be going for. Basically, only run the code in the "above 18" bit once with a flag to say that it's been run. Same with the else condition. I can't remember if arduino supports bool so I'm using int, but you can change this for a bool.
 

GustySpace10

New Coder
Possible suggestion:
C:
int aboveEighteen = 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) {
    if (!aboveEighteen) {
      tone(7, 330);
      CoelsServo.write(180);
      aboveEighteen = 1;
    }
  } else {
    if (aboveEighteen) {
      CoelsServo.write(0);
      delay(100);
      noTone(7);
      aboveEighteen = 0;
    }
  }

  delay(1000); // Delay of 1 second
}
Not sure if this is the solution you'd be going for. Basically, only run the code in the "above 18" bit once with a flag to say that it's been run. Same with the else condition. I can't remember if arduino supports bool so I'm using int, but you can change this for a bool.
Hi Krustie,

just tried your solution there and it didn’t solve the problem :(
 

Krusty the Senile

Active Coder
Would you know if the power supply to your Arduino and associated components is enough? Are you using a battery to power everything?
Could you try with just the motor connected and the LCD disconnected? Maybe with the piezo disconnected too?
Could you test the motor on its own with the Arduino? Can you create a program that actuates the motor independently of the other inputs? Maybe when the motor is activated, there's too much power draw. You can also try modifying your program such that the motor is activated independently of the LCD and piezo.
 
Top