Alternate operation of two motors depending on state of pressure switch.

Hello everyone,

I have a pump system, in which I want to run motor alternately depending on state of pressure switch and not with delay program. I want logic as below,

  1. When switch ON system and if pressure switch is at HIGH state, PUMP A should run and once pressure switch is LOW pump will stop.
  2. When next time again pressure switch become HIGH, PUMP B should run as PUMP A was running earlier.
    In such way I want to run pumps alternately. I have made program for this as below, but its not working with hardware. All other program and circuit is working good only alternate operation is not happening.

#define FlOAT_SWITCH_PIN 2
#define Pressure_SWITCH_PIN 12
//Motors pins:
#define PUMPA_PIN 7
#define PUMPB_PIN 8

include <EEPROM.h>

void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
Serial.begin(9600);}

void loop() {
//when pressure switch(12) HIGH, first motor 8, should start,
if (digitalRead(12)==HIGH && digitalRead(2)==HIGH && EEPROM.read(8, HIGH)
{digitalWrite(8,LOW);
digitalWrite(7,HIGH);
EEPROM.write(8,LOW);}
//when next time pressure switch will become HIGH that time second motor 7, should start and in such way motors should run alternatly,
else if (digitalRead(12)==HIGH && digitalRead(2)==HIGH && EEPROM.read(8, LOW)
{digitalWrite(8,HIGH);
digitalWrite(7,LOW);
EEPROM.write(8,HIGH);}
}

Please, can someone help me with this?

Please, can someone help me with this?

Not until you post your code properly. Unless, of course, it really has smileys in it, which I doubt.

Please read this before posting a programming question and follow the instructions for posting code.

You meant to post:

const int FLOAT_SWITCH_PIN = 2;
const int PRESSURE_SWITCH_PIN = 12;
//Motors pins:
const int PUMP_A_PIN = 7;
const int PUMP_B_PIN = 8;

#include <EEPROM.h>

void setup() {
  pinMode(FLOAT_SWITCH_PIN, INPUT_PULLUP);
  pinMode(PRESSURE_SWITCH_PIN, INPUT_PULLUP);
  pinMode(PUMP_B_PIN, OUTPUT);
  pinMode(PUMP_A_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  //when pressure switch(PRESSURE_SWITCH_PIN) HIGH, first motor 8, should start,
  if (digitalRead(PRESSURE_SWITCH_PIN) == HIGH &&
      digitalRead(FLOAT_SWITCH_PIN) == HIGH &&
      EEPROM.read(8) == HIGH) {
    digitalWrite(PUMP_B_PIN, LOW);
    digitalWrite(PUMP_A_PIN, HIGH);
    EEPROM.write(8, LOW);
  }
  //when next time pressure switch will become HIGH that time second motor 7, should start and in such way motors should run alternately,
  else if (digitalRead(PRESSURE_SWITCH_PIN) == HIGH &&
           digitalRead(FLOAT_SWITCH_PIN) == HIGH &&
           EEPROM.read(8) == LOW) {
    digitalWrite(PUMP_B_PIN, HIGH);
    digitalWrite(PUMP_A_PIN, LOW);
    EEPROM.write(8, HIGH);
  }
}

You said you want Pump A to always be first on power-up but you are using EEPROM to keep track of which pump was last used. Did you mean to say you want them to alternate, even if power fails?

Hi UKHeliBob and Johnwasser,

Sorrry for delay I was not able to find replies of post.

@ UKHeliBob: I am new to Arduino and have read the link sent by you, I got it. Thanks.

I have attached my code and also posted below.

Circuit Hardware: I have connected float switch and SPDT switch as pressure switch to INPUT of Arduino and two motor through 5V relay to OUTPUT of Arduino and all connections have made properly only not getting logic which I want.

Logic: I wants to run two motor(A & B) alternately according to status of pressure switch as shown in program [independent of motor ON time and main switch ON-OFF status(i e. ON-OFF main power supply or power failure and restore) it should sense only status of pressure switch and motors for alternate operation]. And for motors here logic is LOW, i.e. when said digitalWrite(8, LOw), motor at 8 will ON and for digitalWrite(8, HIGH), motor will OFF.

Current Working: I have made code for this using EEPROM concept to check and update status of motors as attached and also pasted below. But with this motors are not running alternately, sometimes only one motor keeps running or both runs randomly. And I think reason behind this is Arduino is not able to understand or sense pressure switch and motor status and give signal accordingly as my program is lacking somewhere.

@Johnswasser: Yes I wants to run motor alternately even if power fails, that means if now motor A was running and power fails and whole system stopped then when power restored and float switch as well as pressure switch HIGH, motor B should start as motor A was running earlier.

What is mistake in this program? And if anyone know getting this logic in someother way, post please.

CODE:
const int FLOAT_SWITCH_PIN = 2;
const int PRESSURE_SWITCH_PIN = 12;
//Motors pins:
const int PUMP_A_PIN = 7;
const int PUMP_B_PIN = 8;

#include <EEPROM.h>

void setup() {
pinMode(FLOAT_SWITCH_PIN, INPUT_PULLUP);
pinMode(PRESSURE_SWITCH_PIN, INPUT_PULLUP);
pinMode(PUMP_B_PIN, OUTPUT);
pinMode(PUMP_A_PIN, OUTPUT);
Serial.begin(9600);
}

void loop() {
//when pressure switch(PRESSURE_SWITCH_PIN) HIGH, first motor 8, should start,
if (digitalRead(PRESSURE_SWITCH_PIN) == HIGH &&
digitalRead(FLOAT_SWITCH_PIN) == HIGH &&
EEPROM.read(8) == HIGH) {
digitalWrite(PUMP_B_PIN, LOW);
digitalWrite(PUMP_A_PIN, HIGH);
EEPROM.write(8, LOW);
}
//when next time pressure switch will become HIGH that time second motor 7, should start and in such way motors should run alternately,
else if (digitalRead(PRESSURE_SWITCH_PIN) == HIGH &&
digitalRead(FLOAT_SWITCH_PIN) == HIGH &&
EEPROM.read(8) == LOW) {
digitalWrite(PUMP_B_PIN, HIGH);
digitalWrite(PUMP_A_PIN, LOW);
EEPROM.write(8, HIGH);
}
else{
digitalWrite(PUMP_B_PIN, HIGH);
digitalWrite(PUMP_A_PIN, HIGH);
}
}

Alternate_operation_of_motors_Arduino.ino (1.11 KB)

How are your switches wired up? Are they normally closed? Your use of input pullup and testing whether switches are high implies it.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

If you get your switches wired properly this version of the sketch is going to switch between the two motors hundreds of times per second as long as the two switches are open. Probably not what you want to do. You need to detect the moment when it goes from "don't run a pump" to "DO run a pump" and only then turn on a pump.

johnwasser:
If you get your switches wired properly this version of the sketch is going to switch between the two motors hundreds of times per second as long as the two switches are open. Probably not what you want to do. You need to detect the moment when it goes from "don't run a pump" to "DO run a pump" and only then turn on a pump.

Is that mean when we switch pressure switch to HIGH or ON, motors becoming ON-OFF more number of times. As you said 'detect the moment when it goes from "don't run a pump" to "DO run a pump" and only then turn on a pump', How can I get this logic?

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

I have posted circuit with Arduino Uno, see it and tell me what can I do for alternate operation.

Thanks :slight_smile:

wildbill:
How are your switches wired up? Are they normally closed? Your use of input pullup and testing whether switches are high implies it.

Switches are normally open, when SPDT switch(used as pressure switch) will HIGH then motor will run.

According to your drawing, the toggle (pressure) switch will be LOW when CLOSED, the float switch will be CLOSED and LOW when liquid is higher than float switch. Is that correct?

outsider:
According to your drawing, the toggle (pressure) switch will be LOW when CLOSED, the float switch will be CLOSED and LOW when liquid is higher than float switch. Is that correct?

When float switch closed(LOW), water level will be low and so both motors will be OFF. When float switch Open(HIGH), water level will be OK and so both motors will become ON alternately according to status to SPDT(pressure)switch.

For SPDT(single pole double throw), when switch at 1, no signal will go. When at 2, motor will go signal of ON. If you see program as well as circuit I have attached above, you will get clearly what logic I want.

when SPDT at 2(mean SPDT or pressure switch at HIGH) motor A will start, when SPDT goes to state 1 motor will OFF. Again next time when SPDT at 2 motor B should start instead of A.

Built a control for 2 diesel pumps feeding a generator at the local hospital.... No micros... See if I can find it

bluejets:
Built a control for 2 diesel pumps feeding a generator at the local hospital.... No micros... See if I can find it

Okay. I want alternate operation of two pumps with programming language, please see if you can find it.

If float switch is closed when level is below it should be drawn like this:
flSw.png
You need to create a boolean variable named "alt" and invert it's state (true or false) when the pump cuts off, next time a pump is to start, check the state of the boolean to see which one to start.

  // PSEUDO CODE

if(RUN_PUMP == HIGH)
  {
    if(alt == true)
      RUN_PUMP1;
  else
      RUN_PUMP2;
  }
else
  {
    STOP_PUMP1;
    STOP_PUMP2;
    alt = ! alt;  // invert alt
  }

@ Outsider
Hi, I used logic you said and changed my program by referring your alt boolean logic. And have attached program below as well as pasted below. Its showing error of 'alt was not declared in this scope', I am not getting program flow and its logic properly Can you please tell me how to define it?

int FLOAT_SWITCH_PIN = 2;
 int PRESSURE_SWITCH_PIN = 12;
//Motors pins:
 int PUMP_A_PIN = 7;
 int PUMP_B_PIN = 8;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  Serial.begin(9600);
}

void loop() {
//when float switch at low, both motors will OFF:
  if (digitalRead(2)== LOW)
  {
   digitalWrite(7, HIGH);
   digitalWrite(8, HIGH); 
   }

   
 if ( digitalRead(8)== HIGH && digitalRead(12) == HIGH && digitalRead(2) == HIGH)
     {
//motor at pin 7 is ON and at 8 is OFF when pressed pressure switch HIGH 1st time:
      if(alt == true)
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    else
//when second time pressure switch HIGH, motor at 8 will ON and at 7 will OFF:
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
     }
  else
  {
   digitalWrite(7, HIGH);
   digitalWrite(8, HIGH);
   alt =! alt;
   }   
}

Thanks:)

_161216_alt_boolean_logic_S_S_alternate.ino (710 Bytes)

Its showing error of 'alt was not declared in this scope'

Can you please tell me how to define it?

The easiest way would be to declare alt as a global boolean variable as suggested in reply #14

Give this a try:

int FLOAT_SWITCH_PIN = 2;
 int PRESSURE_SWITCH_PIN = 12;
//Motors pins:
 int PUMP_A_PIN = 7;
 int PUMP_B_PIN = 8;
 boolean alt = true;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  Serial.begin(9600);
}

void loop() {
//when float switch at low, both motors will OFF:
  if (digitalRead(2)== LOW)
  {
   digitalWrite(7, HIGH);
   digitalWrite(8, HIGH);
   }

   
 if ( digitalRead(8)== HIGH && digitalRead(12) == HIGH && digitalRead(2) == HIGH)
     {
//motor at pin 7 is ON and at 8 is OFF when pressed pressure switch HIGH 1st time:
      if(alt == true)
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    else
//when second time pressure switch HIGH, motor at 8 will ON and at 7 will OFF:
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
     }
  else
  {
   digitalWrite(7, HIGH);
   digitalWrite(8, HIGH);
   alt =! alt;
   }   
}

Final final edit (I hope) :slight_smile: , added interlock:

int FLOAT_SWITCH_PIN = 2;
int PRESSURE_SWITCH_PIN = 12;
//Motors pins:
int PUMP_A_PIN = 7;
int PUMP_B_PIN = 8;
boolean alt = true, interLock = false;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  //when float switch at low, both motors will OFF:
  /*
    if (digitalRead(2)== LOW)
    {
     digitalWrite(7, HIGH);
     digitalWrite(8, HIGH);
    }
  */

  if ((digitalRead(2) == LOW) && (digitalRead(12) == HIGH || interLock == true))
  {
    //motor at pin 7 is ON and at 8 is OFF when pressed pressure switch HIGH 1st time:
    if (alt == true)
    {
      digitalWrite(8, HIGH);
      digitalWrite(7, LOW);
      interLock = true;
    }
    else
    {
      //when second time pressure switch HIGH, motor at 8 will ON and at 7 will OFF:
      digitalWrite(7, HIGH);
      digitalWrite(8, LOW);
      interLock = true;
    }
  }
  //when float switch at low, both motors will OFF:
  if (digitalRead(2) == LOW)
  {
    interLock = false;
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    alt = ! alt;
  }
}

@Outsider thanks for code, I checked it. But second code which you posted is not giving exact result so I changed it but that is also not working. Results by hardware circuit by your original second program and after making some changes in it are as below. Please see it. what improvement or changes are required in program?

1)Results by original second program:
Operation is changing automatically. At starting, at HIGH position of pressure switch motors were running alternately and at LOW of pressure switch one motor keeps running even at LOW both motor should have stopped. And now with same program(without any change in program) sometimes only one motor is running and sometime both running.

2)Results after making some changes in it:
At Float switch HIGH and changing pressure switch position in HIGH and LOW,
HIGH-both run
LOW-8 run(motor B)
HIGH-both run
LOW-8 run
HIGH-both run
LOW-7 run(motor A)
HIGH-both run
LOW-8 run ....& so on. sometimes at every LOW of switch, 8 keeps running five times as shown above it runs two times and then at next low of switch 7 run.
here also, at LOW no motor should run and at HIGH 8 and 7 should run alternately i.e if at first HIGH, 8 run then at second HIGH, 7 should run.

That changed code is as below:

int FLOAT_SWITCH_PIN = 2;
int PRESSURE_SWITCH_PIN = 12;
//Motors pins:
int PUMP_A_PIN = 7;
int PUMP_B_PIN = 8;
boolean alt = true, interLock = false;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if ((digitalRead(2) == HIGH) && (digitalRead(12) == HIGH || interLock == false))
  {
    //motor at pin 7 is ON and at 8 is OFF when pressed pressure switch HIGH 1st time:
    if (alt == true)
    {
      digitalWrite(8, HIGH);
      digitalWrite(7, LOW);
      interLock = true;
      alt = ! alt;
    }
    else 
    {
      //when second time pressure switch HIGH, motor at 8 will ON and at 7 will OFF:
      digitalWrite(7, HIGH);
      digitalWrite(8, LOW);
        interLock = true;
      alt = ! alt;
    }
  }   
    
  
  //when float switch at low, both motors will OFF:
  if (digitalRead(2) == LOW)
  {
    interLock = false;
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    alt = ! alt;
  }
 else if (digitalRead(12) == LOW )
  {
    interLock = false;
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    alt = ! alt;
  }
}

what changes need in program?

Thanks.