int MTV = 512; // motor trip voltage
int PTV = 512; // power supply trip voltage
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT); // HIGH = PWR ENABLE
pinMode(3, OUTPUT); // MOTOR RELAY, HIGH = RUN MODE, LOW = VOUT MODE
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
void loop() {
while (analogRead(A0) < MTV) { // while the motor voltage is < motor trip voltage,
} // read the motor voltage, until it is >= motor trip voltage
Serial.print(" You are pulling the hose ");
delay(1000); // Delay 2 seconds to check if hose has been yanked
if (analogRead(A0) < MTV) { // after 2 sec, if motor voltage is still < motor trip voltage,
Retract(); // call the Retract subroutine
return; // return to top
}
while (analogRead(A0) >= MTV) { // while the motor voltage is >= motor trip voltage
// empty loop
} // read motor voltage until it is < motor trip voltage
unsigned long startTime = millis(); // start the timer
bool hosePulling = true; // initialize as true to handle the first button release correctly
while (true) {
if (analogRead(A0) >= MTV) {
if (!hosePulling) {
Serial.print(" You are pulling the hose ");
hosePulling = true;
}
startTime = millis(); // reset the timer if the motor voltage is >= motor trip voltage
} else {
if (hosePulling) {
Serial.print(" You stopped pulling the hose ");
hosePulling = false;
}
}
if (millis() - startTime >= 2500) {
if (analogRead(A0) < MTV) {
Slack(); // call the Slack subroutine
return; // return to top
}
}
delay(1); // small delay to prevent tight loop
}
}
void Retract() {
Serial.print(" It was a yank, I'm Retracting");
digitalWrite(3, LOW); // turn relay on
delay(100); // wait for relay to transition
digitalWrite(2, LOW); // turn pwr supply on
unsigned long startTime = millis(); // start the timer
while (true) {
if (analogRead(A1) >= PTV) {
startTime = millis(); // reset the timer if the power supply voltage is >= power supply trip voltage
}
if (millis() - startTime >= 2500) {
if (analogRead(A1) < PTV) {
Serial.print(" I'm Stalled waiting for next hose pull ");
digitalWrite(2, HIGH); // turn off power supply
delay(100); // wait for power supply to turn off
digitalWrite(3, HIGH); // turn relay off
return;
}
}
delay(1); // small delay to prevent tight loop
}
}
void Slack() {
Serial.print(" You stopped for more than 5 secs, you are parked, so I'm retracting a foot of hose in case ALL the hose was pulled out, so I can detect the next hose pull");
digitalWrite(3, LOW); // turn relay on
delay(100); // wait for relay to transition
digitalWrite(2, LOW); // turn pwr supply on
delay(2000); // run motor 2 sec for slack
digitalWrite(2, HIGH); // turn pwr supply off
delay(100); // wait for relay to transition
digitalWrite(3, HIGH); // turn relay off
Serial.print(" I finished minor retraction, Waiting for next hose pull");
}