#include <HCSR04.h>
// Digital pin definitions
int ledRightPin = 2;
int ledLeftPin = 3;
int pushButtonLeftPin = 4;
int pushButtonRightPin = 5;
int sensorTriggerPin = 6;
int sensorRightEchoPin = 7;
int sensorLeftEchoPin = 8;
int pumpPin = 9;
int valveRightPin = 10;
int valveLeftPin = 11;
int buttonPin = 12;
int servoAngle;
// The maximum distance that an object must be from the sensor to trigger (in cm).
const int maxObjectDistance = 5;
// The duration of the motor pumping water in seconds.
const int motorPumpDuration = 1;
// The gap in seconds the the water pump will start before opening the gap, and will stop before closing the valve.
const int valvePumpGapDuration = 1;
// Initialisation class HCSR04 (trig pin , echo pin, number of sensors).
HCSR04 sensor(sensorTriggerPin, new int[2] {sensorLeftEchoPin, sensorRightEchoPin}, 2);
void setup()
{
Serial.begin(9600);
// Define pin modes.
pinMode(ledLeftPin, OUTPUT);
pinMode(ledRightPin, OUTPUT);
pinMode(valveRightPin, OUTPUT);
pinMode(valveLeftPin, OUTPUT);
pinMode(pumpPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(pushButtonRightPin, INPUT_PULLUP); // Enable the resistor embbeded on arduino's digital pin so we don't need to use a resistor.
pinMode(pushButtonLeftPin, INPUT_PULLUP); // Enable the resistor embbeded on arduino's digital pin so we don't need to use a resistor.
}
void loop()
{
// The time interval to wait within loops.
int delayTime;
if (digitalRead(buttonPin) == LOW) {
// The switch button is off. We can perform test operartions.
// Check is the right push button has been pressed.
if (digitalRead(pushButtonRightPin) == LOW) {
// Pump the water through the right valve.
pumpWater(valveRightPin);
} else if (digitalRead(pushButtonLeftPin) == LOW) {
// Pump the water through the left valve.
pumpWater(valveLeftPin);
}
// Read somewhere that a small delay is good for circuit stability.
delayTime = 1;
} else {
// The switch button is on. Verify if a nearby object is at the range of the sensor.
// Look for objects in the left sensor.
checkSensor(false);
// Look for objects in the right sensor.
checkSensor(true);
// Wait every 2 seconds to read again..
delayTime = 2000;
}
delay(delayTime);
}
void checkSensor(bool isSensorRight) {
int sensorIndex, ledPin, valvePin;
// Sensor left is represented at position 0 in HCSR04 the array. Sensor right at position 1.
if (isSensorRight) {
sensorIndex = 1;
ledPin = ledRightPin;
valvePin = valveRightPin;
} else {
sensorIndex = 0;
ledPin = ledLeftPin;
valvePin = valveLeftPin;
}
// The distance of an object in cm.
int objectDistance = sensor.dist(sensorIndex);
// It is recommended to wait over 60ms in order to prevent trigger signal to the echo signal.
delay(60);
// This conditional prevents pumping water when the sensor is disconected.
if (objectDistance > 0) {
if (objectDistance < maxObjectDistance) {
// An object is near the sensor, turn on the led.
turnOnLed(ledPin, 1);
// Pump the water.
pumpWater(valvePin);
} else {
// No objects found nearby, turn off the led.
turnOffLed(ledPin);
}
}
}
void turnOffLeds() {
digitalWrite(ledLeftPin, LOW);
digitalWrite(ledRightPin, LOW);
}
void turnOffLed(int ledPin) {
digitalWrite(ledPin, LOW);
}
// Turns on the led represented by ledPin for the given duration in seconds.
void turnOnLed(int ledPin, int duration) {
digitalWrite(ledPin, HIGH);
delay(duration * 1000);
digitalWrite(ledPin, LOW);
}
void turnOnLeds() {
digitalWrite(ledLeftPin, HIGH);
digitalWrite(ledRightPin, HIGH);
}
// Turns on the water pump for 1 second.
void pumpWater(int valvePin) {
// First we must open the water valve before pumping the water.
digitalWrite(valvePin, HIGH);
// Let's wait a bit for the valve to open before pumping the water.
delay(valvePumpGapDuration * 1000);
// Pump water for 1 sec.
digitalWrite(pumpPin, HIGH);
delay(motorPumpDuration * 1000);
// Switch off pump
digitalWrite(pumpPin, LOW);
// Wait a bit for the water pump to switchOff before closing the valve.
delay(valvePumpGapDuration * 1000);
// Once we swich off the water pump, we can now close the valve.
digitalWrite(valvePin, LOW);
}