I’ve started project with my daughter in building a model of for simple Flood Management using floodgates and pumps.
I’ve manage to combine code for different components but somehow the I can’t get the second servo and the output pin for Pump 2 to work.
I’ve checked with the servo sweep tutorial and both servos are working. I’m powering them using a breadboard power supply module connected to a 9V battery.
I’m also using the Arduino Excel by Roberto Valgolio to write values on an excel spreadsheet.
These are the conditions the project is supposed to work:
Floodway HIGH : CLOSE both floodgates and start both pumps
Floodway LOW : OPEN both floodgates and stop both pumps
Floodway HIGH & River 1 LOW :Stop Pump 1.
Floodway HIGH & River 1 HIGH :Start Pump 1.
Floodway HIGH & River 2 LOW :Stop Pump 2.
Floodway HIGH & River 2 HIGH :Start Pump 2.
The pump pins are used to start/stop the Pumps. See attached image for pump connections i got online. Currently I have to Pumps connected I am only reading the PIN state to indicate pump start/stop.
See attached image for layout.
See code:
/*
------------------------------------------------------------------------------------------------
Arduino Excel 2.1
created June 20th 2016 by Roberto Valgolio
last modified 25/07/2016
history 25/07/2016 improved protocol with acknowledgement for each message
notes
this example code is in the public domain
any feedback will be appreciated, please write to roberto.valgolio@gmail.com
This sketch exchanges over serial csv messages with ArduinoExcel_xx.xls app by the same author
-------------------------------------------------------------------------------------------------
Modified sketech to include 3 Ultrasonic Sensor for water level measurement, 2 SG90 servos for floodgate control and 2 mini pumps (5v).
*/
#include <rExcel.h> // Arduino Excel 2.1 by Roberto Valgolio
#include "NewPing.h" // Include NewPing Library
#include <Servo.h> // Include Servo Library
#define trigPin1 13
#define echoPin1 12
#define trigPin2 11
#define echoPin2 10
#define trigPin3 9
#define echoPin3 8
#define MAX_DISTANCE 100
long idx = 0; // index
int outputTiming = 1000; // packet sending timing in ms important: this dermines the output timing
Servo floodgate1; // create servo object to control a servo. Servo for Floodgate No.1
Servo floodgate2; // create servo object to control a servo. Servo for Floodgate No.2
//NewPing library code
NewPing sonar1(trigPin1, echoPin1, MAX_DISTANCE);
NewPing sonar2(trigPin2, echoPin2, MAX_DISTANCE);
NewPing sonar3(trigPin3, echoPin3, MAX_DISTANCE);
float duration1, duration2, duration3, distance1, distance2, distance3;
float levelFloodway, levelRiver1, levelRiver2;
int iterations = 5;
int pump1_Pin = 2, pump1_Status; //Set Pump 1 output pin to 2 and status variable.
int pump2_Pin = 4, pump2_Status; //Set Pump 2 output pin to 3 and status variable.
int gate1, gate2; //Set variable for floodgate pin status
int pos = 179; // variable to store the initial servo position (Floodgates = OPEN)
rExcel myExcel; // class for Excel data exchanging
void setup(){
int pos = 179; // variable to store the initial servo position (Floodgates = OPEN)
floodgate1.write(pos);
floodgate2.write(pos);
floodgate1.attach(14); // attaches the Floodgate No.1 servo on pin 5 to the servo object
floodgate2.attach(16); // attaches the Floodgate No.1 servo on pin 6 to the servo object
Serial.begin(115200);
delay(100);
pinMode(pump1_Pin, OUTPUT); // Sets pumpPin as digital output
pinMode(pump2_Pin, OUTPUT); // Sets pumpPin as digital output
// rx buffer clearing
myExcel.clearInput();
}
void loop() {
static unsigned long loopTime = 0;
static unsigned long time1 = 0;
loopTime = millis();
// Output Task
// instructions performed each outputTiming ms
if ((loopTime - time1) >= outputTiming) {
time1 = loopTime;
//NewPing code with iterations
duration1 = sonar1.ping_median(iterations);
duration2 = sonar2.ping_median(iterations);
duration3 = sonar3.ping_median(iterations);
// Determine distance from duration
// Use 343 metres per second as speed of sound
distance1 = (duration1 / 2) * 0.0343;
distance2 = (duration2 / 2) * 0.0343;
distance3 = (duration3 / 2) * 0.0343;
// Determine water level
levelFloodway = 10 - distance1;
levelRiver1 = 10 - distance2;
levelRiver2 = 10 - distance3;
//Logic loop to check Floodway & River levels and CLOSE Floodgates/START Pumps
if (levelFloodway > 5 && levelRiver1 > 4) {
floodgate1.write(1); // close Floodgate 1
delay(15);
digitalWrite(pump1_Pin, HIGH);
pump1_Status = digitalRead(pump1_Pin); //start Pump 1
} else if (levelFloodway > 5 && levelRiver2 > 4) {
floodgate2.write(1); //close Floodgate 2
delay(15);
digitalWrite(pump2_Pin, HIGH);
pump2_Status = digitalRead(pump2_Pin); //start Pump 2
} else if (levelFloodway < 4 && levelRiver1 < 4) {
floodgate1.write(179); //open Floodgate 1
delay(15);
digitalWrite(pump1_Pin, LOW);
pump1_Status = digitalRead(pump1_Pin); //stop Pump 1
} else if (levelFloodway < 4 && levelRiver2 < 4) {
floodgate2.write(179); //open Floodgate 2
delay(15);
digitalWrite(pump2_Pin, LOW);
pump2_Status = digitalRead(pump2_Pin); //stop Pump 2
} else if (levelFloodway > 5 && levelRiver1 < 4) {
digitalWrite(pump1_Pin, LOW); //stop Pump 1
pump1_Status = digitalRead(pump1_Pin);
} else if (levelFloodway > 5 && levelRiver2 < 4) {
digitalWrite(pump2_Pin, LOW); //stop Pump 2
pump2_Status = digitalRead(pump2_Pin);
}
delay(100);
gate1 = floodgate1.read();
gate2 = floodgate2.read();
// Write data values to Excel spreadsheet.
myExcel.write("DISPLAY", "B2", levelFloodway, 2); // write the value of "distance" to worksheet 'DISPLAY' cell 'B2' with two digits as decimals
myExcel.write("DISPLAY", "B3", levelRiver1, 2); // write the value of "distance" to worksheet 'DISPLAY' cell 'B2' with two digits as decimals
myExcel.write("DISPLAY", "B4", levelRiver2, 2); // write the value of "distance" to worksheet 'DISPLAY' cell 'B2' with two digits as decimals
myExcel.write("DISPLAY", "D3", pump1_Status); // write the pin 2 value "pump1_Status" to worksheet 'DISPLAY' cell 'D3'
myExcel.write("DISPLAY", "D4", pump2_Status); // write the pin 3 value "pump2_Status" to worksheet 'DISPLAY' cell 'D4'
myExcel.write("DISPLAY", "G3", gate1); // write the pin 3 value Floodgate 1 status to worksheet 'DISPLAY' cell 'G3'
myExcel.write("DISPLAY", "G4", gate2); // write the pin 4 value Floodgate 2 status to worksheet 'DISPLAY' cell 'G4'
}
} // end of loop