Help on program conditional loop with servo control

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

dc_motor_connections.jpg

dc_motor_connections.jpg

You don't have any Serial.print() statements to let you see the values that are used in your IF statements - perhaps the conditions for operating the floodway and pump2 are not being met?

If your 9v battery is one of smoke alarm PP3 types then it is totally unsuitable as it can't provide enough current. Use a pack of 6 x AA alkaline cells.

You have not mentioned how your servos are powered so I suspect they are drawing power from the Arduino 5v pin. That is also a bad idea as the Arduino cannot provide the current needed by servos. Give the servos their own power supply with the GND connected to the Arduino GND.

...R

PS ... I don't have Windows or Excel so you may find it easier to get help if you start with a program that is not using the Excel library. Just print values to the Serial Monitor while developing the program.

Thanks Robin2 for you post. I am doing the Serial.print() to debug the if statements. As for the servo power, I'm using your standard 9V battery connected to the breadboard power supply module. I will also try powering as you mentioned 6 AA alkaline cells. Will post again the results.

Finally got it working. Your Serial.print() suggestion helped a lot during debugging. It enabled me to see that the certain conditions are always TRUE preventing the other conditions to be considered. Restructured the if statements with a couple of imbedded if statements.

See revised if statements below;

 //Logic loop to check Floodway & River levels and CLOSE Floodgates/START Pumps
  if (levelFloodway > 5) {
    floodgate1.write(1);    // CLOSE Floodgate 1
    floodgate2.write(1);    // CLOSE Floodgate 2
    delay(15);


    //Check River 1 levels  
    if (levelRiver1 > 4) {
      digitalWrite(pump1_Pin, HIGH);
      pump1_Status = digitalRead(pump1_Pin);    //start Pump 1


    }
    else {
      digitalWrite(pump1_Pin, LOW);   //Do not start Pump 1. 
      pump1_Status = digitalRead(pump1_Pin);

    }

    //Check River 2 levels
    if (levelRiver2 > 4) {
      digitalWrite(pump2_Pin, HIGH);
      pump2_Status = digitalRead(pump2_Pin);    //start Pump 1


    }
    else {
      digitalWrite(pump2_Pin, LOW);   //Do not start Pump 1.  
      pump2_Status = digitalRead(pump2_Pin);  

    }


  } else if (levelFloodway < 4) {
    floodgate1.write(179);    // OPEN Floodgate 1
    floodgate2.write(179);    // OPEN Floodgate 2
    delay(15);
    digitalWrite(pump1_Pin, LOW);   // Pump 1 STOP
    digitalWrite(pump2_Pin, LOW);   // Pump 2 STOP
    pump1_Status = digitalRead(pump1_Pin);    
    pump2_Status = digitalRead(pump2_Pin);    
  
  }

As for the servo, its nothing to do with the power. Its due also to statements not being considered by the logic loop because some statements are always TRUE.

Thanks…

raffneck:
As for the servo, its nothing to do with the power.

It is still not a good idea to draw servo power (or power for any motor) through the Arduino board.

And good to see you have it working.

...R

Your right that is why I am powering the servos on a separate breadboard with its own power supply module connected to a 9V battery.

Anyway thanks for the feedback. Next I will try to control the servos from the Excel spreadsheet. :slight_smile: