Reading Sensor. Do condition with Solenoid and Pump

I am using Arduino to: read analog Pressure Sensor. then control Pump and Valve if its more than 800 stop Pump and Valve... start timer for 5 seconds then turn on Valve. if its less than 200 stop Pump and Valve... start timer for 3 seconds then turn on Pump. if its between 800 and 200 before timer do the job... stop / reset timer and continue old job (if pump is on or valve is on)

this project use millis()

I tried it many times but i don't know how do it.

Could someone please help me.

Welcome to the forum

Start by posting your best attempt an explaining what was wrong

HI, @gitex
Welcome to the forum.

It show you how to insert code with a scrolling window.

Tom... :smiley: :+1: :coffee: :australia:

so you're waiting for the pressure to > 800 before opening the value and closing the valve when the pressure drops below 200?

can you explain "what" you're trying to control, not "how" you think you need to do it?

Your thought process seems to start in the future and time runs backward until the present.
Start your thought process here:
You have just turned power ON and pressure is less than 100.

I found it difficult to understand your process, so is this you are trying to implement?

Start -> Check pressure (analog read)

if pressure < 200, wait 3 seconds then close valve and start pump

else if pressure > 800, stop pump, wait 5 seconds then open valve

loop to Start

Kindly advise.

this isn't very clear

what does this mean? which is it: valve or pump

????

can you please describe what the system is rather than what how you think it needs to be solved?

The XY problem is a communication problem encountered in help desk, technical support, software engineering, or customer service situations where the question is about an end user's attempted solution (Y ) rather than the root problem itself (X ).[1]

Recommendations,

  • Not to change the state of the Valve/Pump before the timer-on.
  • Included deadband in the alarm.

Please take a look at the below code for your reference,
Click here -> Wokwi Simulator

/*
  Pressure: 0.0-100.0%
  Low SP:   20.0%
  High SP:  80.0%
  Deadband:  3.0%
*/

#define commonTripPin     4
#define hiAlarmPin        5
#define loAlarmPin        6
#define normalPin         7

#define pumpOutputPin     2
#define valveOutputPin    3
#define pressureInputPin  A0

#define EU_MAX        100.0F
#define SETPOINT_HIGH 80.0F
#define SETPOINT_LOW  20.0F
#define DEADBAND      3.0F
#define PUMP_DELAY    3000UL
#define VALVE_DELAY   5000UL

int rawValue;
float pressure;
bool lowPressure;
bool highPressure;
bool commonAlarm;
const float deadband = EU_MAX * DEADBAND * 0.01;

bool pumpState;
bool valveState;

bool input;
bool done;
unsigned long delayTime;
unsigned long startTime;

void setup() {
  // Serial.begin(115200);

  pinMode(normalPin, OUTPUT);       // Normal Pin Mode
  pinMode(loAlarmPin, OUTPUT);      // Low Alarm Pin Mode
  pinMode(hiAlarmPin, OUTPUT);      // High Alarm Pin Mode
  pinMode(commonTripPin, OUTPUT);   // Common Trip Pin Mode

  pinMode(valveOutputPin, OUTPUT);  // Valve Pin Mode
  pinMode(pumpOutputPin, OUTPUT);   // Pump Pin Mode
}

void loop() {
  AnalogCondition();
  OutputCondition();
  AlarmStatus();
}

void AnalogCondition() {
  rawValue = analogRead(pressureInputPin);  // RAW Input Value
  pressure = rawValue * 100.0F / 1023.0F;   // Analog Scaling

  lowPressure = (lowPressure | pressure <= SETPOINT_LOW) & !(pressure >= SETPOINT_LOW + deadband);      // Low Alarm With DB
  highPressure = (highPressure | pressure >= SETPOINT_HIGH) & !(pressure <= SETPOINT_HIGH - deadband);  // High Alarm With DB
  commonAlarm = lowPressure | highPressure;
}

void OutputCondition() {
  if (commonAlarm) {
    delayTime = lowPressure ? PUMP_DELAY : VALVE_DELAY;     // Set Delay Time
  }

  TimerOn(commonAlarm);                                     // Timer On
  bool pumpStart = lowPressure & done;                      // Pump Start
  bool valveStart = highPressure & done;                    // Valve Start

  pumpState = (pumpState | pumpStart) & !valveStart;        // Pump State
  valveState = (valveState | valveStart) & !pumpStart;      // Valve State

  digitalWrite(pumpOutputPin, pumpState & !highPressure);   // Pump Output
  digitalWrite(valveOutputPin, valveState & !lowPressure);  // Valve Output
}

void AlarmStatus() {
  digitalWrite(normalPin, !commonAlarm);    // Normal Status
  digitalWrite(loAlarmPin, lowPressure);    // Low-Alarm Status
  digitalWrite(hiAlarmPin, highPressure);   // High-Alarm Status
  digitalWrite(commonTripPin, input);       // Common-Trip Status
}

bool TimerOn(bool enable) {
  if (enable) {
    if (!done) {
      if (!input) startTime = millis();
      if (millis() - startTime >= delayTime) done = true;
    }
  } else {
    done = false;
  }
  input = enable;
  return done;
}
2 Likes

Great Answer :clap: :clap: :clap:

How i can do it ? because pressure sensor will break. i need to turn pump off in pressure more than 80% and turn off valve in pressure less than 20% ...

your coding was great :heart_eyes_cat:

Refer to the example. It will be answered,

  • After High pressure for 3 seconds, turn the Valve On and stop the Pump.
  • After Low Pressure for 3 seconds, turn the Valve Off and start the Pump.

It must be like this:

after low pressure first turn off the valve... then start timer ... then turn on pump.
after high pressure first turn off the pump... then start timer ... then turn on valve.

not sure how do that in your code.

I have changed the Timer from function to structure. Please try to declare a timer for each device or a single timer. And run a timer,

  pumpTimer.timerOn(lowTrip & !pumpState);                  // Pump Start Condition
  valveTimer.timerOn(highTrip & !valveState);               // Valve On Condition

  pumpState = (pumpState | pumpTimer.done) & !highTrip;     // Pump State
  valveState = (valveState | valveTimer.done) & !lowTrip;   // Valve State
1 Like

i am not sure why edited code don't work.
it seems something is wrong

Please post a code/capture of what you did so that I/we can help.

I have run the last code you edited. not sure whats happening
didn't turn off pump in the more than 80% pressure before timer.
didn't turn off valve in the less than 20% pressure before timer.
valve is on in pressure less than 20% and pump is on in more than 80 before timer. please let me know how to turn it off before timer enabled.
i am so confused :sob:

sorry for my bad english.

i run it in simulator that you have posted.

Set up a couple variables such as previous milli and current milli.. Before the the loop, set millis() as the "previous millis". If you then set millis() as the "current millis" you now have a time difference. Then all you need to do is have a variable set point defined and something along the lines oif if Current Millis - Previous Millis >= X, then do something. Much easier to control than a delay since that is no matter what going to delay for X amount of time whether the value of the sensor goes lower than threshold or not.

Is this a pump that could be controlled with a PLC? If so try an open source like Open PLC that works with arduino. I'd try and stick with discreet I/O if you can. Look at www.a2btransportsystems.com for some sensors that may work.

Please look at the latest code post #10.

1 Like

Man, You Are My Hero :clap: :clap: :clap: :clap: :clap: :clap: :clap:
Great Code.
Great Solution.

Thank you :pray: :pray: :pray:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.