// Watch to assure the water level swings above and below 2.5 volts every so often
// Code From: Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
unsigned long time; //Initialize HiVolts, HighTime, LowVolts, LowTime,
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin VoltIn
void setup(){
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); //Open a Serial Port
}
void loop(){ //start a loop
Serial.print(“Time: “); //Test Print Current time
time = millis();
Serial.println(time); //prints time since program started
VoltIn = analogRead(potpin); //input voltage (value between 0 and 1023)
VoltIn = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
if VoltIn > HiVolts //if voltage is higher than old high, and higher than 2.6
if VoltIn > 2.6
HiVolts = VoltIn // set HiVolts to voltage
HighTime = time // set HighTime to now,
LowVolts = 2.5 // set LowVolts to 2.5
if VoltIn < LoVolts //if voltage is lower than old low, and lower than 2.4
if VoltIn < 2.4
LoVolts = VoltIn // set LowVolts, to voltage
LoTime = time // set LowTime to now
LowVolts = 2.5 // set HiVolts to 2.5
if abs(LoTime – HiTime) > 10000 //if ABS(LowTime – HighTime) is too long
Serial.print(“Time of failure: “); //send error
Serial.println(time);
delay(2000); // wait so as not to send massive amounts of data
} //end loop