Another working version with potentiometer manual override and thermistor control stops at closed, quarter open, half open, three quarters open and full open.
ThermistorServo 1_02
Code:
// Thermistor controlled servo to open a vent based on thermistor values, quarter stops, pot manual override.
// This version seems to work.
// Connect one or more servos with control pin to pin 2. Pos lead to 5V, neg lead to grd.
// Connect thermistor to analog pin 0: 5V - thermistor - apin 0 - 10k ohm - grd.
// Connect potentiometer sensor (middle pin) to analog pin 3. Connect one side to 5V and the other to grd.
// Adjust the min and max values for the thermistor and servo to adjust.
// There may be a need to "reverse" the servo or thermistor, use math or turn servo around.
// Configure servo throw and temperature cutin and cutout points
int refreshTime = 200; // ms, the time in between pulses, servo speed adjustment.
int thermMin = 970; // thermistor control voltage, ("Temp") at which vent will start to open.
int thermMax = 990; // thermistor control voltage ("Temp") at which vent will be 100% open.
int potMin = 100; // potentiometer control voltage at which vent will start to open.
int potMax = 1000; // potentiometer control voltage at which vent will be 100% open.
int pulseMin = 1000; // us, Start point for servo, between 250 and 2300 microseconds for my Futaba FP-52B.
int pulseMax = 2000; // us, End point for servo, must be higher than pulseMin.
// Beyond Here: Change with care
int servoPin = 2; // control pin for servo motor
int thermPin = 0; // thermistor analog input pin
int potPin = 3; // potentiometer analog input pin
// variables to set stops for vent opening
int thermInput = 0; // thermistor control voltage value
int thermRange = thermMax - thermMin;
int thermThird = (thermRange/3)+thermMin;
int thermTwoThirds = (2*(thermRange/3))+thermMin;
int pulseRange = pulseMax - pulseMin;
int pulse1 = pulseMin + (pulseRange/4);
int pulse2 = pulseMin + (2*(pulseRange/4));
int pulse3 = pulseMin + (3*(pulseRange/4));
// potentiometer variables
int potInput = 0; // potentiometer control voltage value
int potInRange = 0;
int potRange = potMax - potMin;
// somewhat silly method to avoid decimal points
int potRatio = 0;
long potPlace = 0;
long potPlace100 = 0;
long potPct = 0;
// pulse generation variables
long pulseIncrease = 0;
long lastPulse = 0;
long pulse = 0;
void setup() {
Serial.begin(9600);
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
}
void loop() {
// thermistor control
thermInput = analogRead(thermPin); // read the analog input, 0 to 1023
if(thermInput<thermMin){pulse=pulseMin;}
if(thermInput>=thermMin && thermInput<thermThird){pulse=pulse1;}
if(thermInput>=thermThird && thermInput<thermTwoThirds){pulse=pulse2;}
if(thermInput>=thermTwoThirds && thermInput<thermMax){pulse=pulse3;}
if(thermInput>thermMax){pulse=pulseMax;}
// potentiometer control
potInput = analogRead(potPin); // read the analog input, 0 to 1023
potInRange = potInput;
if(potInput<potMin){potInRange=potMin+1;}
if(potInput>potMax){potInRange=potMax;}
potPlace = potInRange - potMin;
potPlace100 = 100*potPlace;
potPct = potPlace100/potRange;
pulseIncrease = pulseRange*potPct;
// potentiometer only controls when it is set within the Min and Max range
// turn to either stop to let thermistor control
if (potInput>potMin && potInput<potMax){ pulse = pulseMin + (pulseIncrease/100);}
// pulse the servo again after refresh time
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
// \\//, skyl4rk 10-III-2009 ,\\//
// Print values to screen
Serial.print("thermInput: ");
Serial.print(thermInput);
Serial.print("; potInput: ");
Serial.print(potInput);
Serial.print("; pulse: ");
Serial.println(pulse);
}