#define dripDetectionPin 2
#define simulatedDrip 3
#define DIR 4
#define STEP 5
unsigned long timeNow;
unsigned long stepInterval;
unsigned long lastDetectedDrip;
unsigned long timeBetweenDrips;
unsigned long lastSimulatedDrip;
signed int valvePosition;
void drip()
{
timeNow = millis();
if (timeNow - lastDetectedDrip > 50)
{
timeBetweenDrips = timeNow - lastDetectedDrip;
lastDetectedDrip = timeNow;
}
}
void setup()
{
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(dripDetectionPin, INPUT);
attachInterrupt(digitalPinToInterrupt(dripDetectionPin), drip, RISING);
}
void loop()
{
timeNow = millis();
// vvv For simulation purposes only. vvv
int simulatedDripInterval = analogRead(A0) * 2;
if (timeNow - lastSimulatedDrip > simulatedDripInterval)
{
digitalWrite(simulatedDrip, HIGH);
lastSimulatedDrip = timeNow;
}
// ^^^ For simulation purposes only. ^^^
if (timeNow - stepInterval >= 1000)
{
if (timeBetweenDrips > 1100 && valvePosition < 46)
{
valvePosition++;
digitalWrite(DIR, HIGH);
digitalWrite(STEP, HIGH);
digitalWrite(STEP, LOW);
}
if (timeBetweenDrips < 900 && valvePosition > -46)
{
valvePosition--;
digitalWrite(DIR, LOW);
digitalWrite(STEP, HIGH);
digitalWrite(STEP, LOW);
}
stepInterval = timeNow;
}
digitalWrite(simulatedDrip, LOW); // For simulation purposes only.
}
Adjusts simulated drip rate.
Blinks to indicate water droplet.