Hello, I got some great help when I posted here the last time so I'm trying again as I'm slightly stuck.
I will start as always by saying I'm not a developer. I've been playing around with the Arduino to learn about it for the past two weeks. What I'm doing may not be at all accurate so please feel free to point out my mistakes.
Problem 1:
When the servo moves to 180 degrees, it stops but it sounds like it's still trying to move.
Problem 2:
When the servo is running the temperature sensor readings are way off.
These problems were described on this forum in the post that I've linked to below but the solution there didn't work for me.
http://forum.arduino.cc/index.php?topic=50523.0
Question: are the problems linked in some way?
The code that I've written is for testing. It has no practical use at the moment. It does the following.
- Moves the servo from 0 degrees to 180 degrees and back.
- Checks the temperature. If the temperature is below 16 degrees C, it leaves the LED turned on.
- Flashes the light for 3 seconds and leaves it off for 7. The temperature reading will override this.
Any suggestions you could give would be very appreciated.
#include <passive_timer.h>
#include <Servo.h>
#define LED_PIN 13
Servo myservo; // create servo object to control the servo.
int pos = 0; // variable to store the servo position.
int last_pos = 0; // Variable to store the last position.
int sensorPin = 0; // the analog pin the TMP36's Vout (sense) pin is connected to
PassiveTimer led_timer; // Tracks the Led time.
PassiveTimer servo_timer; // tracks the servo time
PassiveTimer temperature_timer; // tracks the temperature test time
boolean servo_has_moved; // Used for servo timer logic.
boolean led_is_on; // Used for LED timer logic.
boolean room_is_cold; // Indicates if temperature levels have reached cold. .
void led_setup (){
led_is_on = false;
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void servo_setup (){
servo_has_moved = false;
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void temperature_loop (){
if (temperature_timer.time_millis() >= 3000) {
int reading = analogRead(sensorPin); // Getting the voltage reading from the temperature sensor
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
if (temperatureC <=16) {
digitalWrite(LED_PIN, HIGH);
room_is_cold = true;
}else{
digitalWrite(LED_PIN, LOW);
room_is_cold = false;
}
Serial.print(temperatureC); Serial.println(" degrees C");
temperature_timer.restart();
}
}
void led_loop() {
// 300 ms on time
// (Intentionally using non 50% blinking to demonstrate the state handling. )
if (led_is_on) {
if (led_timer.time_millis() >= 300) {
if (!room_is_cold) {
led_is_on = false;
digitalWrite(LED_PIN, LOW);
}
led_timer.restart();
}
return;
}
// 700 ms off time.
if (led_timer.time_millis() >= 700) {
led_is_on = true;
digitalWrite(LED_PIN, HIGH);
led_timer.restart();
}
}
void servo_loop() {
if (servo_has_moved) {
if (servo_timer.time_millis() >= 1500) {
servo_has_moved = false;
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{ // in steps of 1 degree
if (last_pos != pos) {
myservo.write(pos); // tell servo to go to position in variable 'pos'
last_pos = pos;
}
}
servo_timer.restart();
}
return;
}
if (servo_timer.time_millis() >= 1500) {
servo_has_moved = true;
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
if (last_pos != pos) {
myservo.write(pos); // tell servo to go to position in variable 'pos'
last_pos = pos;
}
}
servo_timer.restart();
}
}
void setup() {
led_setup();
servo_setup();
Serial.begin(9600); //Start the serial connection with the computer
}
void loop() {
led_loop();
servo_loop();
temperature_loop();
}