I'm trying to use 3 timers in a project I'm doing. The first turns on a sensor, and the second turns it off. I have this section working but the third timer is the issue.
If the sensor is within a certain value range an output goes HIGH. The third timer is supposed to then set it LOW. Much like the first 2 timers
any help would be appreciated
const unsigned long DELAY_TIME = 10000;
const unsigned long DELAY_TIME1 = 1000;
const unsigned long DELAY_TIME2 = 2000;
unsigned long moisture_timer, moisture_on, solnoid_on = 0;
void setup() {
Serial.begin(9600);
for (int i = 5; i > 0; i--) { // just to give a little time before it all starts
delay(1000);
Serial.print(i); Serial.print(' ');
}
Serial.println();
pinMode(2, OUTPUT);// the power source for the moisture sensor
pinMode(3, OUTPUT); // signal for the latch relay
digitalWrite(2, LOW);// sets the pin to 0V
digitalWrite(3, LOW); //sets the pin to 0V
moisture_timer = moisture_on = solnoid_on = millis();
}
void loop() {
Serial.println(analogRead(A0));
if (millis() - moisture_timer > DELAY_TIME) {
if (digitalRead(2) == LOW) {
moisture_on = millis();
}
digitalWrite(2, HIGH);
if (millis() - moisture_on > DELAY_TIME1) {
digitalWrite(2, LOW);
moisture_timer = millis();
}
if (analogRead(A0) <= 500 && analogRead(A0) >= 1) {
digitalWrite(3, HIGH);
}
if (millis() - solnoid_on > DELAY_TIME2) {
digitalWrite(3, LOW);
solnoid_on = millis();
}
}
}
Read a bit about state machines. You should check first in which state your device actually is. Only then check the appropriate delay whether to take an action.
Hello gitpeke
I could be easy when some comments for the functionality of the sketch would be added.
Provide functional names for the port pins in use.