Stop the servo, and interference with temperature sensor

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();
}

Sounds like power issues and asking the servo to do something it can't.

Not all servos can manage the full range of movement from 0 to 180. It sounds like yours can't, reduce the number of degrees you want it to move through and find out by experiment how far it can go without it sounding like it's trying to break the end stop.

Servos draw a fair bit of power - allow an amp. This means that the arduino can't power one, it needs its own power supply but make sure you connect the grounds. It sounds as though you're trying to power the servo directly from the arduino and although you may get away with it for a while, you may damage the arduino. That current draw may also mean that there are other electrical side effects like analogRead giving bad results, as you observe.

Interesting. Thanks.

The servo that I have came in a kit from ardx.org. It's very small. I doubt it would move much at all. All I was going to use it for was moving the Raspberry Pi camera board. Although it's in a loop at the moment, I would eventually have it moving very occasionally. Do you still think it would need a power supply?

I have a few PC's and servers here. I'd like to be able to monitor the temperature in the room and point the camera toward different areas. Again, mainly to learn about the Arduino. It's nothing difficult I'm sure. If I need to power the RP, the Arduino and a servo, the power cables start to become a little more obvious.

The servo will draw current even if it isn't moving, unless you detach it. Once detached, you'll be relying on friction in the drivetrain to prevent it from moving. Maybe that'll be ok, try it and see.

If the servo really is tiny and draws less than 200 mA under load (measure it!), then in theory, you don't need a separate power supply. Finding a supply should not be hard - I just searched through wall warts for long abandoned electrical gear until I found one with the right voltage and sufficient power - who knew it was worth keeping that old link sys power supply?