Try that code, I'll post a little bit later with code to do a moving window average.Code:// Controlling a servo position using a temperature sensor
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// edited 5-12-2010 by Will Lyon to include base setting for servo if voltage not detected on pin 7
// edited again 7-4-2010 by crenn to simplify the code a little
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const char CONTROL = 7; // digital pin used to detect if the system is on or off
const char temps = 0; // analogue pin used to connect the temp sensor
char trigger = 0; // varible used to store the control pin value
int val; // variable to read the value from the analog pin
void setup()
{
pinMode (CONTROL, INPUT); // sets the control pin to input
myservo.attach(9); // attaches the servo on pin 9 to the servo object
digitalWrite(CONTROL, LOW); // ensure internal pullup resistor is disabled.
}
void loop()
{
trigger = digitalRead(CONTROL); // read input of pin CONTROL and store it
if (trigger == HIGH){ // reads if pin CONTROL, if true, do this:
val = analogRead(temps); // read the value of the temp sensor (value with range of 1024)
val = map(val, 350, 700, 75, 105); // scale it to use it with the servo (value between 75 and 105)
myservo.write(val); // sets the servo position according to the scaled value
}
else {
myservo.write(50); // sets servo position to 50 if above statment is false
}
delay(125); // wait 25ms for the servo to move to it's new position and also 100ms until it gets the new value
}