Go Down

### Topic: Alternate operation of two motors depending on state of pressure switch. (Read 1 time)previous topic - next topic

#### deshmane

#15
##### Dec 16, 2016, 01:51 pm

@ 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?

Code: [Select]
`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:)

#### UKHeliBob

#16
##### Dec 16, 2016, 05:20 pm
Quote
Its showing error of 'alt was not declared in this scope'
Quote
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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### outsider

#17
##### Dec 16, 2016, 05:59 pm
Give this a try:
Code: [Select]
`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;   }   }`

#### outsider

#18
##### Dec 16, 2016, 07:05 pmLast Edit: Dec 16, 2016, 07:24 pm by outsider
Final final edit (I hope)  , added interlock:
Code: [Select]
`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;  }}`

#### deshmane

#19
##### Dec 19, 2016, 08:29 am
@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:

Code: [Select]
`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.

#### outsider

#20
##### Dec 19, 2016, 10:14 amLast Edit: Dec 19, 2016, 10:39 am by outsider
In your OP you said you were going to exchange the toggle sw for a pressure sw,  so I envisioned your setup like this:
The pressure switch starts a pump when liquid gets to high level;
As the level falls the pressure switch will release and the pump would stop if not held on by the interlock;
When the level reaches the float and releases it, the pump would stop, flip the alternator bit and release the interlock, then wait for the pressure sw to engage again;
Try turning the toggle sw ON then right back OFF and see how it works.
BTW, I'm not sure how the toggle sw is wired, you have internal pullup turned on but your code switches the pump on with a HIGH, so it's ON when the toggle sw is OFF, is that right?
Also, you may need some switch debounce.

#### deshmane

#21
##### Dec 19, 2016, 11:36 amLast Edit: Dec 21, 2016, 06:05 am by deshmane
In your OP you said you were going to exchange the toggle sw for a pressure sw,  so I envisioned your setup like this:
The pressure switch starts a pump when liquid gets to high level;
As the level falls the pressure switch will release and the pump would stop if not held on by the interlock;
When the level reaches the float and releases it, the pump would stop, flip the alternator bit and release the interlock, then wait for the pressure sw to engage again;
Try turning the toggle sw ON then right back OFF and see how it works.
BTW, I'm not sure how the toggle sw is wired, you have internal pullup turned on but your code switches the pump on with a HIGH, so it's ON when the toggle sw is OFF, is that right?
Also, you may need some switch debounce.

Hello,

Sorry actually logic behind motor operation is different.  motors are going to get ON depending on pressure not on liquid level.  Here motors used for pressurized system, Pressure switch will be there for checking water pressure in system(here for trial I have used toggel switch as pressure switch, as I shown in posted circuit).

Thus for FLOAT switch is HIGH (i.e. water has good or max level), then when toggel switch is HIGH(pressure reduced), only motor A should start and when toggel switch become LOW(pressure increased or restore), motor A should stop.  Similarly, again next time when toggel switch become HIGH, only motor B should start and when toggel switch goes to LOW, motor B should stop and so on.

Toggle switch wiring: Between pin 2 of Arduino and ground of Arduino.  I have used internal pull up, and logic used is at HIGH of switch motors ON and at LOW motors OFF.
and is meaning of 'so it's ON when the toggle sw is OFF' is like when toggel switch is at HIGH position motor will get ON signal by relay as signal goes to relay as digitalWrite(7, LOW) as relay get ON signal for LOW.

Have you got logic I want? Debounce thing I will check adding. whatelse need to do?

Thanks:)

#### outsider

#22
##### Dec 19, 2016, 06:46 pmLast Edit: Dec 19, 2016, 06:46 pm by outsider
OK, the fog is slowly lifting,    You don't need the interlock then, delete my second program and try the first, does it do what you want?

#### outsider

#23
##### Dec 19, 2016, 09:02 pmLast Edit: Dec 19, 2016, 09:52 pm by outsider
OK, final final edit # 2, I put in a debounce for the pressure sw, compiles but ? Try it and let me know.
Code: [Select]
`unsigned long dbStart;        //debounce pressure switchbyte dbTime = 100;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, swState, pressSwState;void setup() {  pinMode(PRESSURE_SWITCH_PIN, INPUT_PULLUP);  pinMode(FLOAT_SWITCH_PIN, INPUT_PULLUP);  pinMode(PUMP_A_PIN, OUTPUT);  pinMode(PUMP_B_PIN, OUTPUT);  Serial.begin(9600);  swState = digitalRead(PRESSURE_SWITCH_PIN);}void loop() {  if (digitalRead(PRESSURE_SWITCH_PIN) != swState)  {    dbStart = millis();    swState ^= 1;  }  if (millis() - dbStart > dbTime)  {    pressSwState = swState;  }  //when float switch at low, both motors will OFF:  if (digitalRead(FLOAT_SWITCH_PIN) == LOW || pressSwState == LOW) // ???  {    digitalWrite(PUMP_A_PIN, HIGH);    digitalWrite(PUMP_B_PIN, HIGH);  }  if (digitalRead(FLOAT_SWITCH_PIN) == HIGH && pressSwState == HIGH      && digitalRead(PUMP_A_PIN) == HIGH && digitalRead(PUMP_B_PIN) == HIGH)  {    alt = ! alt;    //motor at pin 7 is ON and at 8 is OFF when pressed pressure switch HIGH 1sttime:    if (alt == true)    {      digitalWrite(PUMP_A_PIN, LOW);      digitalWrite(PUMP_B_PIN, HIGH);    }    else    {      //when second time pressure switch HIGH, motor at 8 will ON and at 7 will OFF:      digitalWrite(PUMP_A_PIN, HIGH);      digitalWrite(PUMP_B_PIN, LOW);    }  }}`

#### deshmane

#24
##### Dec 20, 2016, 10:55 am
Hello everyone,
Finally circuit and its alternate logic is working with adding debounce in Interlock program.
Thanks everyone  for helping me.  Thanks @ Outsider, your logic helped lot.
Thanks:)

Go Up