so I have a piece of code that senses a voltage using a resistor bridge and uses it if it passes a set trigger point to turn on or off an output, the problem is it only does it once and doesn't look like its looping. i am making a small solar setup to power a fountain in my garden, and going to use the output of the solar panels once high enough to trigger a relay to turn on the pump so its not running of the battery all night
int sensorPin = A1; // Voltage Sensor connected to A1
int sensorValue; // variable to store the value coming from the voltage sensor
int previousSensorValue;
int testLED = 9;
boolean timing = false;
unsigned long startTime;
unsigned long onTime = 2000;
void setup()
{
Serial.begin(9600); // initialize the serial port
pinMode(sensorPin, INPUT); // sets analog pin A1 to be an input
pinMode(testLED, OUTPUT); // sets digital pin 9 to be an output
}
void loop()
{
previousSensorValue = sensorValue;
sensorValue = analogRead(sensorPin); // read the value from the voltage sensor
Serial.println(sensorValue);
if (sensorValue >= 200 && previousSensorValue < 200)
{
timing = true;
startTime = millis();
digitalWrite(testLED, HIGH); //turns on led connected to digital pin 9
}
if (timing && millis() - startTime >= onTime)
{
digitalWrite(testLED, LOW);
timing = false;
}
}