Arduino controlling fluid pump and solenoids [solved]

Hello,

I am currently working on a project using an Arduino Mega to run a fluid pump and several solenoids. I currently have the system working however, I could use some help with a question. I am pretty new to Arduino and coding so please bare with me.

Currently, the program turns the pump on and actuates the different solenoids to allow fluid to flow to different parts of the hydraulic circuit for a set amount of time. After some testing I found that the pressure can spike higher than desired due to some of the orifice sizes. I currently have a pressure sensor wired into the circuit that cuts the signal to the relay but, I was wondering if there is away i can set my code so that it will interrupt the IF loop, if pressure sensor trips. i was wondering if there is a way to code it in. Below is an example of what I have for part of the circuit.

      Operation =  Circuit1 + Circuit2;
      Serial.println(Operation);
      if ( Operation == 2) {
        digitalWrite(41, HIGH);
        delay(500);
        digitalWrite(41, LOW);
        digitalWrite(43, HIGH);
        delay(1500);
        digitalWrite(43, LOW);
        digitalWrite(45, HIGH);
        delay(500);
        digitalWrite(45, LOW);
        digitalWrite(47, HIGH);
        delay(500);
        digitalWrite(47, LOW);
      }
      else if ( Operation == 1) {
        if (Circuit1 == 1) {
          digitalWrite(41, HIGH);
          digitalWrite(43, LOW);
          digitalWrite(45, LOW);
          digitalWrite(47, LOW);
        }
        if (Circuit2 == 1) {
          digitalWrite(41, LOW);
          digitalWrite(43, HIGH);
          digitalWrite(45, HIGH);
          digitalWrite(47, HIGH);
          delay(1500);
          digitalWrite(41, HIGH);
          digitalWrite(43, LOW);
          digitalWrite(45, LOW);
          digitalWrite(47, LOW);
        }
      }
      else if ( Operation == 0) {
        digitalWrite(41, LOW);
        digitalWrite(43, LOW);
        digitalWrite(45, LOW);
        digitalWrite(47, LOW);
      }

I did try and enclose this in a while statement, but it would not exit the IF loop. like this:

      Operation =  Circuit1 + Circuit2;
      Serial.println(Operation);
      while(digitalRead(38) == HIGH){
        if ( Operation == 2) {      
        digitalWrite(41, HIGH);
        delay(500);
        digitalWrite(41, LOW);
        digitalWrite(43, HIGH);
        delay(1500);
        digitalWrite(43, LOW);
        digitalWrite(45, HIGH);
        delay(500);
        digitalWrite(45, LOW);
        digitalWrite(47, HIGH);
        delay(500);
        digitalWrite(47, LOW);
      }
      else if ( Operation == 1) {
        if (Circuit1 == 1) {
          digitalWrite(41, HIGH);
          digitalWrite(43, LOW);
          digitalWrite(45, LOW);
          digitalWrite(47, LOW);
        }
        if (Circuit2 == 1) {
          digitalWrite(41, LOW);
          digitalWrite(43, HIGH);
          digitalWrite(45, HIGH);
          digitalWrite(47, HIGH);
          delay(1500);
          digitalWrite(41, HIGH);
          digitalWrite(43, LOW);
          digitalWrite(45, LOW);
          digitalWrite(47, LOW);
        }
      }
      else if ( Operation == 0) {
        digitalWrite(41, LOW);
        digitalWrite(43, LOW);
        digitalWrite(45, LOW);
        digitalWrite(47, LOW);
      }
    }
    }

Again, I apologize for my ignorance, but I am unsure if this is even possible. I am sure there are easier ways to code this as well, but this was the only way I knew how.

Would it not be so much easier to have a hydraulic system that included it's own pressure regulation?

Paul

Hello,

You are correct it would be easier, and is most likely the approach I will take. It just made me wonder if it was possible to force an IF loop to stop, based off of an input, and if so how? Thank you for taking the time to give your input.

mbuchanan84:
Hello,

You are correct it would be easier, and is most likely the approach I will take. It just made me wonder if it was possible to force an IF loop to stop, based off of an input, and if so how? Thank you for taking the time to give your input.

Google "Arduino exit".

Paul

mbuchanan84:
Hello,

You are correct it would be easier, and is most likely the approach I will take. It just made me wonder if it was possible to force an IF loop to stop, based off of an input, and if so how? Thank you for taking the time to give your input.

An "if" only runs once when all the conditions you code are true.

Paul

mbuchanan84:
Hello,

You are correct it would be easier, and is most likely the approach I will take. It just made me wonder if it was possible to force an IF loop to stop, based off of an input, and if so how? Thank you for taking the time to give your input.

You need to switch over to using non-blocking code - no calls to delay().

Checkout the BlinkWithoutDelay example sketch to see the way this is done. In general you code
the actions as a statemachine and then loop() gets to run repeatedly all the time and can check for
all the things you need to check for on a timely basis.

Thank you very much for the advice! I was able to find an example in the general programming forum page called do multiple things at once. I was able to understand and modify my code to integrate your exact solution of a “state machine”. Thank you all for your advice and patience with my lack of experience.