Arduino UNO R3 using two outputs dependent to different inputs

I am running a program code in my Arduino with two inputs and on/delay cyclic timers. I like to add one more output independent with three different inputs, tried the below written code, but only my 1st loop is working, 2nd one with inputs pin-5, 6, 7, output pin-9 is not working. Can someone make correction and identify the mistake to me. Thanks.
For 2nd loop input pin5 must be HIGH, input pin6 & Pin8 must be low for Output "ON"at pin 9.
Input pin5 must be HIGH, input pin6 & pin7 must be high for out "OFF" at pin 9, in any condition if pin5 input is LOW output at pin 9 must be remain OFF. (1st loop with input pins 3,4 output pin 8 and timers is still working perfectly)

// Pin numbers aren't negative, right? So no `int´ but unsigned int's
const uint8_t relayPin = 8;
const uint8_t sensorPin = 3;
const uint8_t switchPin = 4;
const uint8_t tankempty = 5;
const uint8_t ohtanklow = 6;
const uint8_t ohtankfull = 7;
const uint8_t motorUpPin = 9;
bool flip = false;
int motorState = 2;
uint32_t previousMillis = 0;
const uint32_t offTime = 60000;
const uint32_t onTime = 90000;

void setup()
{
// All pins are set to inputs by default
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
Serial.begin(9600);
pinMode(sensorPin, INPUT_PULLUP);
pinMode(switchPin, INPUT_PULLUP);
pinMode(motorUpPin, OUTPUT);
digitalWrite(motorUpPin, LOW);
pinMode(tankempty, INPUT_PULLUP);
pinMode(ohtanklow, INPUT_PULLUP);
pinMode(ohtankfull, INPUT_PULLUP);
}

void loop()
{
// Always update the ezButton object states
// Remember the current motor state as well (defaults to false)
// Turn the motor OFF if the float switch is active (LOW)
int Switch = digitalRead(switchPin);
int Sensor = digitalRead(sensorPin);
int tankempty = digitalRead(tankempty);
int ohtanklow = digitalRead(ohtanklow);
int ohtankfull = digitalRead(ohtankfull);
Serial.print("Switchpin 4 is:...");
Serial.println(Switch);
Serial.print("Sensorpin 3 is:...");
Serial.println(Switch);
if (Switch == HIGH && Sensor == HIGH) {
motorState = 2; // not pressed.
counterFunction(motorState);
} else if (Switch == HIGH && Sensor == LOW) {
motorState = 0;//turns on.
counterFunction(motorState);
} else if (Switch == LOW && Sensor == HIGH) {
motorState = 1;//turns on.
counterFunction(motorState);
}
if (Switch == LOW && Sensor == LOW) {
motorState = 0;//turns the motor off when the tank fill's up but water is comming
}
Serial.println(motorState);
if (tankempty == LOW && ohtankfull == HIGH) {
motorState = 3;//turns off.
counterFunction(motorState);
}
if (tankempty == LOW && ohtanklow == LOW) {
motorState = 4;//turns on.
counterFunction(motorState);
}
if (tankempty == HIGH && ohtanklow == LOW ){
motorState = 3;// not pressed.
counterFunction(motorState);

}
Serial.println(motorState);
}

void counterFunction(int overide) {
//on loop.
if (overide == 2) {
if (millis() >= (previousMillis + onTime) && flip == true) {
previousMillis = millis();
flip = false;
overide = 0; //off
}
if (millis() >= (previousMillis + offTime) && flip == false) {
previousMillis = millis();
flip = true;
overide = 1; //on
}
}
if (overide == 0) {
digitalWrite(relayPin, LOW);
Serial.println("Motor is LOW:...");
} else if ( overide == 1) {
digitalWrite(relayPin, HIGH);
Serial.println("Motor is HIGH:...");
}
if (overide == 3) {
digitalWrite(motorUpPin, LOW);
Serial.println("Motor is LOW:...");
} else if ( overide == 4) {
digitalWrite(motorUpPin, HIGH);
Serial.println("Motor is HIGH:...");
}
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You actually have the pin numbers declared as byte, which is fine, but a byte is an unsigned integer not an unsigned int. One day the difference may matter in a sketch so it pays to be precise

can you explain what your trying to do, not in terms of pin states?

there are 2 sets if inputs read in loop() each setting "motorState" and calling "counterFunction()" with a different argument.

it's confusing what counterFuntion() does. looks like it's argument can have values from 0-5, but if the value is 2 it can be changed to 0 or 1 based on the timer.

the logic in counterFunction() is also confusing because the processing for argument values 0 & 1 is also a result of argument 2.

it would be clearer if there were descriptive symbols for what the arguments to counterFunction() are, for example value 3 & 4 affect the motor and 0&1 the relay

use of a switch statement would also make the code more readable

@Syed
I have removed your email address from your post as I do not think it is appropriate to put email addresses in the forum.

This topic seems to be closely related to your other topic https://forum.arduino.cc/t/arduino-with-multiple-inputs-and-multiple-outputs/906581 if not exactly the same. Are they the same?

Thanks,

The 1st function is one on/off timer is running, during "ON" cycle output at pin # 8 (Digital output) is on and "OFF" cycle, output at pin#8 is off. If input at pin# 3 becomes "HIGH" it will stop the timer and let the output at pin #8 remains "ON" until input at pin # 4 becomes "HIGH" that will stop the output at pin # 8, once pin #4 input goes "LOW" cyclic timer will start.(This output at pin #8 is a filling pump which is checking the presence of water depending upon timer cycle(Pin#3 input is water presence digital input which allow the pump to run continuously) and stopping with storage full input (Pin#4 digital input).
While 2nd function is to run another pump to shift this storage water to an overhead tank (Output Pin #9) it will see the water presence in storage (Input Pin # 5) if pin #5 is "HIGH" and overhead tank is not full (Pin# 7 input is "LOW"), it will start the 2nd pump (Output at Pin#9 "HIGH", if Pin#7 input becomes "HIGH" means overhead tank full, it will stop the 2nd pump, if water presence is of Pin# 5 input "HIGH" and overhead "LOW LEVEL" input Pin #6 becomes "LOW" it will start the pump again until water presence input Pin#5 goes "LOW" and/or Tank full input Pin#7 goes "HIGH" which is stop the output at pin#9.
Please be noted that 1st function which is more complicated with timer is running well, only 2nd function, filling the overhead tank is no working at all.

Well that certainly clears everything right up. Not.

A diagram of how these pumps, tanks and sensors are arranged would help.

A timing diagram of the several conditions and responses you want would be useful.

Perhaps if you made the desired function of the whole system a little better described it would help.

Part of the my problem is trying to see what is possible from the sensors - what combinations are ruled out by physical circumstances.

a7

Arduino UNO use for operating two different pumps, 1. Cyclic timer 30 minutes off-1 minute on(searching water in line) if on digital input get signal of water flow, that input should keep the Pump running until 2nd digital input of" tank full" goes high, then pump should stop as well as cyclic timer also, until tank full input goes off. Again cycle start.
2. Another pump for overhead tank filling should run if underground tank low level digital input is "off", overhead tank low level digital input "high" and pump stops at overhead tank high level digital input is"high" I'm very new beginner for Arduino program, can someone send me the codes/ program.

This is not a programming service.

You are right, as a biginner I just ask for help favor. Please don't take it seriously

How else can we be of assistance?

It doesn't sound too difficult however some things have to be known. If we look at only the Arduino and nothing else in the system, what are the inputs and outputs.

i.e.

Pin7 output controls pump if 1 pump is on if 0 pump is off
Pin8 input Tank full 1 = tank full 0 = tank not full

etc.

Next you have to create a flow chart of what the Arduino needs to do. Experienced programmers can sometime do this in their head but you should draw it out.

Flowchart example:

Keypad Flowchart.pdf (12.5 KB)

Thanks a lot JohnRob, I will draw the flow chart. Actually I am quite new in programming but I like to learn.

Good.
I suggest this:

  1. Get an Arduino.
  2. Write a sketch that blinks the onboard led.
  3. Write a sketch that reads a button.
  4. Write a sketch that turns the onboard led on/off as a button is pressed.
  5. Get your pump and appropriate driver/relay for it.
  6. Replace led with pump & driver. Write sketch to test if you can turn it on / off.
  7. Get tank fluid level sensor. Connect to Arduino, write sketch with sensor's library, or better yet: use example sketch that comes with library. Test if you can reliably sense if the tank is full.
  8. Get liquid flow sensor. Follow same steps as in (7).
  9. Combine code for both sensors and pumps into the system as you see it. Use the flowchart @JohnRob suggested you make to design your code.
  10. Profit.
1 Like

@Syed
I have merged your 2 topics as, so far as I can tell, they are on the same subject. Please do not post multiple questions on the same subject as it just wastes the time of the volunteers trying to help you.

Thanks,

void setup()
{
  Serial.begin(9600);

  pinMode(sensorPin, INPUT_PULLUP);    // #3
  pinMode(switchPin, INPUT_PULLUP);    // #4

  pinMode(tankempty, INPUT_PULLUP);    // #5

  pinMode(ohtanklow, INPUT_PULLUP);    // #6
  pinMode(ohtankfull, INPUT_PULLUP);   // #7

  pinMode(relayPin, OUTPUT);           // #8
  pinMode(motorUpPin, OUTPUT);         // #9

  digitalWrite(relayPin, LOW);
  digitalWrite(motorUpPin, LOW);
}

The setup() function reimagined.

@syed please describe how each of the 5 inputs is used, included what means HIGH and LOW readings from them.

Please describe what the two outputs are controlling.

I can't look away from the train wreck that is your code and explanation thereof. I challenge myself to make any sense of it. It should be a very simple program, even with the switch between timed running of the (a?) pump and water-level controlled.

Free game to play!

a7

Here's the 2nd pump thing.

It seems like you want to pump into the tank until it is full, and not start pumping again until it is empty.

And pump only if #5 sensor says there is any point.

Use the slide fader to simulate tank water. Empty on the left, full on the right.

See the empty and full LEDs that would be the output of the sensors.

See the LED up top turning on and off the pump.

a7

1 Like

Yes, you are right, I need pump should not start if underground storage is empty (#5 Sensor), but this 2nd pump logic should work with my 1st pump logic, which is storage tank filling pump.

Thanks, alto777, here is the clarification of pins and input/output you asked.
Input pin#3 is a flow sensor (Which is a digital input, High=5V, Low=oV).
Input pin #4 is storage tank full signal (Which is a digital input, High=5V, Low=oV).
Above inputs are used to control the 1st pump relay, output pin#8
input pin # 5 is storage tank empty signal (Which is a digital input, High=5V, Low=oV).
Input pin #6 is overhead tank level low signal (Which is a digital input, High=5V, Low=oV).
Input pin #7 is overhead tank level full signal (Which is a digital input, High=5V, Low=oV).
These three inputs are used to control the 2nd pump, output pin#9
Both pin#8 &9 outputs are controlling relays only.
digitalWrite(relayPin, LOW); means 1st pump relay 0v signal, (OFF) output pin#8 is 0v
digitalWrite(motorUpPin, LOW); means 2nd pump relay 0v (OFF) output pin#9 is 0v

Yes, thank you.

When you have a minute... For all inputs, what does HIGH mean? What does LOW mean?

Like:

input pin # 5 is storage tank empty signal: HIGH = tank empty

and what logic does the flow sensor assist in? What flow is it sensing, and what should flowing vs. not flowing mean to your program? And like the others, HIGH = flowing? or not flowing?

Can you just draw with a pencil the arrangement of the tanks, the position of the sensors? Take a picture of it w/ your cellphone and post it!

I think we have a "flow" diagram here, we all taking bets to see how close we came.

a7