I have ran out of talent with automated filtration system

What I am trying to do: I want create a system with arduino that does two basic things that revolve around a whole house filter that is bringing used as the first filter in the water filtration system in a house on well water. Most houses I know of have a sediment filter with a ball valve for flushing out sand, then a 10" whole house filter with a wound polypropylene filter. I want to have a LED light come on with the pressure drop on the whole house filter is greater than a threshold (a yellow LED, I will call it the "time to order filter light"). Then once a a greater threshold is reached between the difference in pre and post sensors, I want the yellow LED to turn off, and a red LED to come on. I could probably write the code to make this happen, although I threw a wrench at myself when I decided that the sediment filter needs to have a valve that opens to flush out sand once a set psi is seen at the pre-filter sensor. I was using delay() lines until I discovered that I should have been using millis(), and I essentially need the code to being checking multiple things at once, and not get "stuck" on a delay() code.

No this is not for work and no this is not for an assignment. It is just a pain point that my neighbors and I have here in SW Idaho with our crappy well water.

The code I was working before I realized that I needed to use millis() commands:

 float preVoltage;   //declaring pre-filter sensor voltage
  float postVoltage;  //declaring of post-filter sensor voltage
  
  float prePsi;   //declaring pre-filter sensor psi
  float postPsi;  //declaring post-filter sensor psi


  int preSensorValue = A0;   //assigning pre-filter sensor to A0
  int postSensorValue = A1;  //assigning pre-filter sensor to A1
  int readPreSensorValue;   //declaring voltage of pre-filter sensor
  int readPostSensorValue;  //declaring voltage of post-filter senor
  
  int yellowLED = A2;                   //assigning pin number to yellow LED
  int yellowLEDThresholdPsi = 5;  //assigning a pressure threshold for yellow LED to illuminate
  int redLED = A3;                      //assigning pin number to red LED
  int redLEDThresholdPsi = 10;    //assigning a pressure threshold for red LED to illuminate
  
  int flushValveSolenoid = A6;       //assigning pin number to flush valve
  int flushValveThresholdPsi = 50;   //assinging a pressure threshold for flush out valve to open
  int flushValveDelay = 30000;       //assigning a delay for flush out valve

void setup() {
    
  Serial.begin(9600);   //initialize serial communication at 9600 bits per second
    
  pinMode(preSensorValue, INPUT);        //sets pre-filter sensor value as input
  pinMode(postSensorValue, INPUT);       //sets post-filter sensor value as input
  pinMode(yellowLED, OUTPUT);            //sets yellow LED as output
  pinMode(redLED, OUTPUT);               //sets red LED as output
  pinMode(flushValveSolenoid, OUTPUT);   //sets flush valve solenoid as output
 
    
}

void loop() {
 
 readPreSensorValue = analogRead(preSensorValue);                  //read raw voltage from pre-filter sensor
 preVoltage = (5./1023.) * preSensorValue;                         //calculating voltage from pre-filter sensor
 prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);              //caculating psi from pre-filter sensor
 Serial.println("Pre-filter sensor psi is currentlty: " prePsi);   //print out pre-filter sensor psi
 delay(2000);                                                      //delay in between reads for stability
 
 readPostSensorValue = analogRead(postSensorValue);                 //read raw voltage from post-filter sensor
 postVoltage = (5./1023.) * postSensorValue;                        //calculating voltage from post-filter sensor
 postPsi = (postVoltage - 0.5) * (100.0) / (4.5 - 0.5);             //caculating psi from post-filter sensor
 Serial.Println("Post-filter sensor psi is currently: " postPsi);   //print out post-filer sensor psi
 
 
 if (prePsi < flushValveThreshold) {          //if the psi is lower than treshold
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
    delay(flushValveDelay);                   //delay for flush out valve
    digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
  } 
 
  if (postPsi - prePsi > yellowLEDThresholdPsi) {   //if the pressure drop is greater than threshold
    digitalWrite(yelowLED, HIGH);                     //turn on the yellow LED
  
    
    
    
    
    
  }
  
  
  
  
  
  
  
  
    
}

What's wrong with just opening the flush valve for a preset time interval when you hit that pressure limit? If you're not checking for the yellow or red conditions during that time I don't see what the problem is, since the valve should not be open for very long.

People (many of whom should know better) crap on using delay() but it's there for a reason.

Are you looking for help with code or for someone to write the code for you from start to finish?

I've replaced the delay() with a millis() timer so you can see how it could work (untested).

float preVoltage;   //declaring pre-filter sensor voltage
float postVoltage;  //declaring of post-filter sensor voltage

float prePsi;   //declaring pre-filter sensor psi
float postPsi;  //declaring post-filter sensor psi


int preSensorValue = A0;   //assigning pre-filter sensor to A0
int postSensorValue = A1;  //assigning pre-filter sensor to A1
int readPreSensorValue;   //declaring voltage of pre-filter sensor
int readPostSensorValue;  //declaring voltage of post-filter senor

int yellowLED = A2;                   //assigning pin number to yellow LED
int yellowLEDThresholdPsi = 5;  //assigning a pressure threshold for yellow LED to illuminate
int redLED = A3;                      //assigning pin number to red LED
int redLEDThresholdPsi = 10;    //assigning a pressure threshold for red LED to illuminate

int flushValveSolenoid = A6;       //assigning pin number to flush valve
int flushValveThresholdPsi = 50;   //assinging a pressure threshold for flush out valve to open

uint32_t flushValveDelay = 30000UL;       //assigning a delay for flush out valve  ***int would be on the limit for this value

uint32_t flushValveOnAtMs = 0 ;    // flush timer
bool inFlush = false ;             // flush timer status

void setup() {

  Serial.begin(9600);   //initialize serial communication at 9600 bits per second

  pinMode(preSensorValue, INPUT);        //sets pre-filter sensor value as input
  pinMode(postSensorValue, INPUT);       //sets post-filter sensor value as input
  pinMode(yellowLED, OUTPUT);            //sets yellow LED as output
  pinMode(redLED, OUTPUT);               //sets red LED as output
  pinMode(flushValveSolenoid, OUTPUT);   //sets flush valve solenoid as output


}

void loop() {

  readPreSensorValue = analogRead(preSensorValue);                  //read raw voltage from pre-filter sensor
  preVoltage = (5. / 1023.) * preSensorValue;                       //calculating voltage from pre-filter sensor
  prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);              //caculating psi from pre-filter sensor
  Serial.println("Pre-filter sensor psi is currentlty: " prePsi);   //print out pre-filter sensor psi
  delay(2000);                                                      //delay in between reads for stability

  readPostSensorValue = analogRead(postSensorValue);                 //read raw voltage from post-filter sensor
  postVoltage = (5. / 1023.) * postSensorValue;                      //calculating voltage from post-filter sensor
  postPsi = (postVoltage - 0.5) * (100.0) / (4.5 - 0.5);             //caculating psi from post-filter sensor
  Serial.Println("Post-filter sensor psi is currently: " postPsi);   //print out post-filer sensor psi



  if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  }
  else {
    digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
    inFlush = false ;
  }

  if (prePsi < flushValveThreshold) {          //if the psi is lower than treshold
    if ( inflush == false ) {
      inFlush = true ;                     
      flushValveOnAtMs = millis() ;    // start flush timer
    }
  }

  if (postPsi - prePsi > yellowLEDThresholdPsi) {   //if the pressure drop is greater than threshold
    digitalWrite(yelowLED, HIGH);                     //turn on the yellow LED

  }

}

cedarlakeinstruments:
What's wrong with just opening the flush valve for a preset time interval when you hit that pressure limit? If you're not checking for the yellow or red conditions during that time I don't see what the problem is, since the valve should not be open for very long.

People (many of whom should know better) crap on using delay() but it's there for a reason.

Are you looking for help with code or for someone to write the code for you from start to finish?

So based on what I have described I need to do, the delay function should work just fine? To go into detail, if the pressure drop over the two sensors is seen, and it throws on the yellow light, and stays on until the board is reset(filter changed), during the time the yellow light is on, the arduino could still be checking the pre-filter and opening the flush out valve as necessary?

6v6gt:
I've replaced the delay() with a millis() timer so you can see how it could work (untested).

float preVoltage;   //declaring pre-filter sensor voltage

float postVoltage;  //declaring of post-filter sensor voltage

float prePsi;   //declaring pre-filter sensor psi
float postPsi;  //declaring post-filter sensor psi

int preSensorValue = A0;   //assigning pre-filter sensor to A0
int postSensorValue = A1;  //assigning pre-filter sensor to A1
int readPreSensorValue;   //declaring voltage of pre-filter sensor
int readPostSensorValue;  //declaring voltage of post-filter senor

int yellowLED = A2;                   //assigning pin number to yellow LED
int yellowLEDThresholdPsi = 5;  //assigning a pressure threshold for yellow LED to illuminate
int redLED = A3;                      //assigning pin number to red LED
int redLEDThresholdPsi = 10;    //assigning a pressure threshold for red LED to illuminate

int flushValveSolenoid = A6;       //assigning pin number to flush valve
int flushValveThresholdPsi = 50;   //assinging a pressure threshold for flush out valve to open

uint32_t flushValveDelay = 30000UL;       //assigning a delay for flush out valve  ***int would be on the limit for this value

uint32_t flushValveOnAtMs = 0 ;    // flush timer
bool inFlush = false ;             // flush timer status

void setup() {

Serial.begin(9600);   //initialize serial communication at 9600 bits per second

pinMode(preSensorValue, INPUT);        //sets pre-filter sensor value as input
 pinMode(postSensorValue, INPUT);       //sets post-filter sensor value as input
 pinMode(yellowLED, OUTPUT);            //sets yellow LED as output
 pinMode(redLED, OUTPUT);               //sets red LED as output
 pinMode(flushValveSolenoid, OUTPUT);   //sets flush valve solenoid as output

}

void loop() {

readPreSensorValue = analogRead(preSensorValue);                  //read raw voltage from pre-filter sensor
 preVoltage = (5. / 1023.) * preSensorValue;                       //calculating voltage from pre-filter sensor
 prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);              //caculating psi from pre-filter sensor
 Serial.println("Pre-filter sensor psi is currentlty: " prePsi);   //print out pre-filter sensor psi
 delay(2000);                                                      //delay in between reads for stability

readPostSensorValue = analogRead(postSensorValue);                 //read raw voltage from post-filter sensor
 postVoltage = (5. / 1023.) * postSensorValue;                      //calculating voltage from post-filter sensor
 postPsi = (postVoltage - 0.5) * (100.0) / (4.5 - 0.5);             //caculating psi from post-filter sensor
 Serial.Println("Post-filter sensor psi is currently: " postPsi);   //print out post-filer sensor psi

if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired
   digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
 }
 else {
   digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
   inFlush = false ;
 }

if (prePsi < flushValveThreshold) {          //if the psi is lower than treshold
   if ( inflush == false ) {
     inFlush = true ;                    
     flushValveOnAtMs = millis() ;    // start flush timer
   }
 }

if (postPsi - prePsi > yellowLEDThresholdPsi) {   //if the pressure drop is greater than threshold
   digitalWrite(yelowLED, HIGH);                     //turn on the yellow LED

}

}

Thank you for the code! If I Venmo you some cash, would you be able to use Google Hangouts to show me what and why you did what you did? I would like to build on my knowledge of code a bit, not just have the code done for me.

float preVoltage;   //declaring pre-filter sensor voltage
float postVoltage;  //declaring of post-filter sensor voltage

float prePsi;   //declaring pre-filter sensor psi
float postPsi;  //declaring post-filter sensor psi


int preSensorValue = A0;   //assigning pre-filter sensor to A0
int postSensorValue = A1;  //assigning pre-filter sensor to A1
int readPreSensorValue;   //declaring voltage of pre-filter sensor
int readPostSensorValue;  //declaring voltage of post-filter senor

int yellowLED = A2;                   //assigning pin number to yellow LED
int yellowLEDThresholdPsi = 5;  //assigning a pressure threshold for yellow LED to illuminate
int redLED = A3;                      //assigning pin number to red LED
int redLEDThresholdPsi = 10;    //assigning a pressure threshold for red LED to illuminate

int flushValveSolenoid = A6;       //assigning pin number to flush valve
int flushValveThresholdPsi = 50;   //assinging a pressure threshold for flush out valve to open

uint32_t flushValveDelay = 30000UL;       //assigning a delay for flush out valve  

uint32_t flushValveOnAtMs = 0 ;    // flush timer
bool inFlush = false ;             // flush timer status

void setup() {

  Serial.begin(9600);   //initialize serial communication at 9600 bits per second

  pinMode(preSensorValue, INPUT);        //sets pre-filter sensor value as input
  pinMode(postSensorValue, INPUT);       //sets post-filter sensor value as input
  pinMode(yellowLED, OUTPUT);            //sets yellow LED as output
  pinMode(redLED, OUTPUT);               //sets red LED as output
  pinMode(flushValveSolenoid, OUTPUT);   //sets flush valve solenoid as output


}

void loop() {

  readPreSensorValue = analogRead(preSensorValue);                  //read raw voltage from pre-filter sensor
  preVoltage = (5. / 1023.) * preSensorValue;                       //calculating voltage from pre-filter sensor
  prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);              //caculating psi from pre-filter sensor
  Serial.println("Pre-filter sensor psi is currentlty: " prePsi);   //print out pre-filter sensor psi
  delay(2000);                                                      //delay in between reads for stability

  readPostSensorValue = analogRead(postSensorValue);                 //read raw voltage from post-filter sensor
  postVoltage = (5. / 1023.) * postSensorValue;                      //calculating voltage from post-filter sensor
  postPsi = (postVoltage - 0.5) * (100.0) / (4.5 - 0.5);             //caculating psi from post-filter sensor
  Serial.Println("Post-filter sensor psi is currently: " postPsi);   //print out post-filer sensor psi



  if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  }
  else {
    digitalWrite(flushValveSolenoid, LOW);         //close flush out valve
    inFlush = false ;
  }

  if (prePsi < flushValveThreshold) {              //if the psi is lower than treshold
    if ( inflush == false ) {
      inFlush = true ;                     
      flushValveOnAtMs = millis() ;                 // start flush timer
    }
  }

  if (postPsi - prePsi > yellowLEDThresholdPsi) {   //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, HIGH);                  //turn on the yellow LED
  }
  
  if (postPsi - prePsi > redLEDThresholdPsi) {      //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, LOW);                   //turn yellow LED off
    digitalWrite(redLED, HIGH);                     //turn red LED on  
  }

}

What I did was the classic use of millis() to replace a delay() by setting a simple timer to the current value of millis() then just testing on every loop() iteration (but without blocking other activities) until it expired.
There is quite a comprehensive description on the forum here aimed at people exploring this area:
http://forum.arduino.cc/index.php?topic=503368.0

Important for you is to get to the situation where you can test such code (maybe changing some of the values for demonstration purposes) so you can confirm it works as you require (by lighting leds, writing to the serial console etc.). I'd even recommend simulating the inputs of the pressure sensors using potentiometers and the map() function so you can verify the whole design in isolation before implementing it. When it gets to that stage, that is where the real fun starts because you can see instantly the effects of any changes you make.

If you have concrete questions, simply ask here. There are also other people who will do coding and tuition for some remuneration if you need that.

Edit:
There is a typing error originating from my code changes. In one case inFlush has been mistyped as inflush. Such errors will be apparent as soon as you do a test compilation.

Hey all,

I finally got some hardware and I am doing bench testing to verify the code. I am using potentiometers in place of the pressure sensors and just using a blue LED in place of the solenoid that will be controlling the flush out valve.

The problem I am having is with the flush out valve(blue LED). Once the threshold pressure is seen(I have it set at 85 for testing) instead of dwell(to symbolize the time the solenoid will have power to open valve), the LED just blinks a slow blink.

Any ideas what could be wrong?

Thanks in advance

int preSensorValue = A0;   //assigning pre-filter sensor to A0
int postSensorValue = A1;  //assigning pre-filter sensor to A1
int readPreSensorValue;    //declaring voltage of pre-filter sensor
int readPostSensorValue;   //declaring voltage of post-filter senor

const int yellowLED = 2;            //assigning pin number to yellow LED
int yellowLEDThresholdPsi = 5;  //assigning a pressure threshold for yellow LED to illuminate
const int redLED = 4;               //assigning pin number to red LED
int redLEDThresholdPsi = 10;    //assigning a pressure threshold for red LED to illuminate

const int flushValveSolenoid = 7;      //assigning pin number to flush valve
int flushValveThresholdPsi = 85;   //assinging a pressure threshold for flush out valve to open
const int buttonPin = A2;           //assigning a pin number to button
int buttonState = 0;               // variable for reading the pushbutton status

uint32_t flushValveDelay = 30000UL;  //assigning a delay for flush out valve  

uint32_t flushValveOnAtMs = 0 ;  // flush timer
bool inFlush = false ;           // flush timer status

void setup() {

  Serial.begin(9600);   //initialize serial communication at 9600 bits per second

  pinMode(preSensorValue, INPUT);        //sets pre-filter sensor value as input
  pinMode(postSensorValue, INPUT);       //sets post-filter sensor value as input
  pinMode(yellowLED, OUTPUT);            //sets yellow LED as output
  pinMode(redLED, OUTPUT);               //sets red LED as output
  pinMode(flushValveSolenoid, OUTPUT);   //sets flush valve solenoid as output
  pinMode(buttonPin, INPUT);             //sets button as input
}

void loop() {
  
  
  buttonState = digitalRead(buttonPin);   // read the state of the pushbutton value

  
  if (buttonState == HIGH) {      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  } else { 
    digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
  }  
  
  readPreSensorValue = analogRead(preSensorValue);                  //read raw voltage from pre-filter sensor
  preVoltage = (5.0 / 1023.0) * readPreSensorValue;                 //calculating voltage from pre-filter sensor
  prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);              //caculating psi from pre-filter sensor
  Serial.print("Pre-filter sensor psi is currentlty: ");            //print out pre-filter sensor psi
  Serial.println(prePsi);                                           //posting current pre-filter psi
  delay(2000);                                                      //delay in between reads for stability

  

  if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  }
  else {
    digitalWrite(flushValveSolenoid, LOW);         //close flush out valve
    inFlush = false ;
  }

  if (prePsi < flushValveThresholdPsi) {              //if the psi is lower than treshold
    if ( inFlush == false ) {
      inFlush = true ;                     
      flushValveOnAtMs = millis() ;                 // start flush timer
    }
  }

  if (postPsi - prePsi > yellowLEDThresholdPsi) {   //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, HIGH);                  //turn on the yellow LED
  }
  
  if (postPsi - prePsi > redLEDThresholdPsi) {      //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, LOW);                   //turn yellow LED off
    digitalWrite(redLED, HIGH);                     //turn red LED on  
  }

}

There are number of undeclared variables, so that sketch doesn't compile.

I apologize, I did not select all the code when I brought it over. Here is the full code with the declared variables.

float preVoltage;   //declaring pre-filter sensor voltage
float postVoltage;  //declaring of post-filter sensor voltage

float prePsi;      //declaring pre-filter sensor psi
const int postPsi = 75;     //declaring post-filter sensor psi


int preSensorValue = A0;   //assigning pre-filter sensor to A0
int postSensorValue = A1;  //assigning pre-filter sensor to A1
int readPreSensorValue;    //declaring voltage of pre-filter sensor
int readPostSensorValue;   //declaring voltage of post-filter senor

const int yellowLED = 2;            //assigning pin number to yellow LED
int yellowLEDThresholdPsi = 5;  //assigning a pressure threshold for yellow LED to illuminate
const int redLED = 4;               //assigning pin number to red LED
int redLEDThresholdPsi = 10;    //assigning a pressure threshold for red LED to illuminate

const int flushValveSolenoid = 7;      //assigning pin number to flush valve
int flushValveThresholdPsi = 85;   //assinging a pressure threshold for flush out valve to open
const int buttonPin = A2;           //assigning a pin number to button
int buttonState = 0;               // variable for reading the pushbutton status

uint32_t flushValveDelay = 30000UL;  //assigning a delay for flush out valve  

uint32_t flushValveOnAtMs = 0 ;  // flush timer
bool inFlush = false ;           // flush timer status

void setup() {

  Serial.begin(9600);   //initialize serial communication at 9600 bits per second

  pinMode(preSensorValue, INPUT);        //sets pre-filter sensor value as input
  pinMode(postSensorValue, INPUT);       //sets post-filter sensor value as input
  pinMode(yellowLED, OUTPUT);            //sets yellow LED as output
  pinMode(redLED, OUTPUT);               //sets red LED as output
  pinMode(flushValveSolenoid, OUTPUT);   //sets flush valve solenoid as output
  pinMode(buttonPin, INPUT);             //sets button as input
}

void loop() {
  
  
  buttonState = digitalRead(buttonPin);   // read the state of the pushbutton value

  
  if (buttonState == HIGH) {      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  } else { 
    digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
  }  
  
  readPreSensorValue = analogRead(preSensorValue);                  //read raw voltage from pre-filter sensor
  preVoltage = (5.0 / 1023.0) * readPreSensorValue;                 //calculating voltage from pre-filter sensor
  prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);              //caculating psi from pre-filter sensor
  Serial.print("Pre-filter sensor psi is currentlty: ");            //print out pre-filter sensor psi
  Serial.println(prePsi);                                           //posting current pre-filter psi
  delay(2000);                                                      //delay in between reads for stability

  

  if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  }
  else {
    digitalWrite(flushValveSolenoid, LOW);         //close flush out valve
    inFlush = false ;
  }

  if (prePsi < flushValveThresholdPsi) {              //if the psi is lower than treshold
    if ( inFlush == false ) {
      inFlush = true ;                     
      flushValveOnAtMs = millis() ;                 // start flush timer
    }
  }

  if (postPsi - prePsi > yellowLEDThresholdPsi) {   //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, HIGH);                  //turn on the yellow LED
  }
  
  if (postPsi - prePsi > redLEDThresholdPsi) {      //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, LOW);                   //turn yellow LED off
    digitalWrite(redLED, HIGH);                     //turn red LED on  
  }

}
  1. Have you an external pull down resistor (say 10k ) between the buttonPin and ground ? If not, this will introduce some instability.
    It is actually better to use the pin's inbuilt pull up resistor that is available by using pinMode( myPin, INPUT_PULLUP) but then you have rewire the button and invert some of the logic in your sketch, so when the button is pressed, digitalRead() gives LOW instead of HIGH.

  2. When you simulated the pressure rising ( preSensorValue connected to A0 ) with the potentiometer to force the flush valve to open (blue led on), did you the wind the potentiometer back to simulate the lower pressure to bring it back below the threshold ?

  3. At what frequency was the led flashing, about once every 30 seconds or every 2 seconds or what ?

It does indeed look like a floating input - I can reproduce the symptoms that way.

You have another issue though, if the flush button is not pressed, the flush solenoid will be turned off. This overrides the automatic flush functionality. When you're in flush mode, you will need to ignore the button.

6v6gt:

  1. Have you an external pull down resistor (say 10k ) between the buttonPin and ground ? If not, this will introduce some instability.
    It is actually better to use the pin's inbuilt pull up resistor that is available by using pinMode( myPin, INPUT_PULLUP) but then you have rewire the button and invert some of the logic in your sketch, so when the button is pressed, digitalRead() gives LOW instead of HIGH.

  2. When you simulated the pressure rising ( preSensorValue connected to A0 ) with the potentiometer to force the flush valve to open (blue led on), did you the wind the potentiometer back to simulate the lower pressure to bring it back below the threshold ?

  3. At what frequency was the led flashing, about once every 30 seconds or every 2 seconds or what ?

  1. Yes there is a 10K resistor between ground and button.

  2. Actually I have simulated the pressure dropping (preSensorValue connected to A0, below flushValveThresholdPsi of 85), and when preSensorValue is below 85, the blue LED blinks at the same time/rate as the TX light on the Arduino board, only when a value less than 85 is seen.

  3. Answered above...

wildbill:
It does indeed look like a floating input - I can reproduce the symptoms that way.

You have another issue though, if the flush button is not pressed, the flush solenoid will be turned off. This overrides the automatic flush functionality. When you're in flush mode, you will need to ignore the button.

Not sure how to fix the floating input situation, maybe by ditching the 10K resistor on ground leg of button and using what 6V6GT described?

Also, you saying I need to add code in the automatic flush functionality code to ignore the operation of the button somehow?

What Wildbill said is correct. There is a conflict between the manual operation of the flush valve and the automatic (pressure related) operation.

Pressing (and holding) the button switches the blue led on. 2 seconds later, after the delay, the automatic part switches it off again because the timer is no longer running.

One solution is this:

  if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  }
  else {
    if ( buttonState== LOW ) {  // automatic termination only if button not pressed.
       digitalWrite(flushValveSolenoid, LOW);         //close flush out valve
       inFlush = false ;
    }
  }

However, this means that the automatic flush will not work without a further code modification.

Another is this, that the button press initiates a one time automatic (30 second) flush without regard to the pressure measured.

 if (buttonState == HIGH) {      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    // digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
    inFlush = true ;                     
    flushValveOnAtMs = millis() ;       
  } else {
    // digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
  }

If you have already got the pull down resistor, then you should not have stability problems so there is no need to change this unelss you want to reduce the component count on the board.

6v6gt:
What Wildbill said is correct. There is a conflict between the manual operation of the flush valve and the automatic (pressure related) operation.

Pressing (and holding) the button switches the blue led on. 2 seconds later, after the delay, the automatic part switches it off again because the timer is no longer running.

One solution is this:

  if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired

digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
 }
 else {
   if ( buttonState== LOW ) {  // automatic termination only if button not pressed.
      digitalWrite(flushValveSolenoid, LOW);         //close flush out valve
      inFlush = false ;
   }
 }




However, this means that the automatic flush will not work without a further code modification.

Another is this, that the button press initiates a one time automatic (30 second) flush without regard to the pressure measured.



if (buttonState == HIGH) {      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
   // digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
   inFlush = true ;                    
   flushValveOnAtMs = millis() ;      
 } else {
   // digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
 }





If you have already got the pull down resistor, then you should not have stability problems so there is no need to change this unelss you want to reduce the component count on the board.

If I copy your code how you posted it, (with "//" in front of your digitalWrite's) the blue LED works as it is supposed to off the pressure threshold but not with button, obviously. Once I delete the //'s in front of the digitalWrite's. both the button and the LED function based on pressure threshold doesn't work.

Also, another thought came to mind with the code. There needs to be a limit on how much the flush out valve opens. Something like 6 times an hour maybe. If the sediment filter screen is pretty well clogged, and the user is out of town or something and still needs irrigation, I can't have the flush valve just cycling open.

float preVoltage;   //declaring pre-filter sensor voltage
float postVoltage;  //declaring of post-filter sensor voltage

int prePsi;      //declaring pre-filter sensor psi
const int postPsi = 75;     //declaring post-filter sensor psi


int preSensorValue = A0;   //assigning pre-filter sensor to A0
int postSensorValue = A1;  //assigning pre-filter sensor to A1
int readPreSensorValue;    //declaring voltage of pre-filter sensor
int readPostSensorValue;   //declaring voltage of post-filter senor

const int yellowLED = 2;            //assigning pin number to yellow LED
int yellowLEDThresholdPsi = 5;  //assigning a pressure threshold for yellow LED to illuminate
const int redLED = 4;               //assigning pin number to red LED
int redLEDThresholdPsi = 10;    //assigning a pressure threshold for red LED to illuminate

const int flushValveSolenoid = 7;      //assigning pin number to flush valve
int flushValveThresholdPsi = 85;   //assinging a pressure threshold for flush out valve to open
const int buttonPin = A2;           //assigning a pin number to button
int buttonState = 0;               // variable for reading the pushbutton status

uint32_t flushValveDelay = 10000UL;  //assigning a delay for flush out valve  

uint32_t flushValveOnAtMs = 0 ;  // flush timer
bool inFlush = false ;           // flush timer status

void setup() {

  Serial.begin(9600);   //initialize serial communication at 9600 bits per second

  pinMode(preSensorValue, INPUT);        //sets pre-filter sensor value as input
  pinMode(postSensorValue, INPUT);       //sets post-filter sensor value as input
  pinMode(yellowLED, OUTPUT);            //sets yellow LED as output
  pinMode(redLED, OUTPUT);               //sets red LED as output
  pinMode(flushValveSolenoid, OUTPUT);   //sets flush valve solenoid as output
  pinMode(buttonPin, INPUT);             //sets button as input
}


void loop() {
 
 

 if (buttonState == HIGH) {      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    //digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
    inFlush = true ;                     
    flushValveOnAtMs = millis() ;       
  } else {
    //digitalWrite(flushValveSolenoid, LOW);    //close flush out valve
  } 

 
 readPreSensorValue = analogRead(preSensorValue);                  //read raw voltage from pre-filter sensor
  preVoltage = (5.0 / 1023.0) * readPreSensorValue;                 //calculating voltage from pre-filter sensor
  prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);              //caculating psi from pre-filter sensor
  Serial.print("Pre-filter sensor psi is currentlty: ");            //print out pre-filter sensor psi
  Serial.println(prePsi);                                           //posting current pre-filter psi
  delay(2000);                                                      //delay in between reads for stability


  if ( inFlush == true && millis() - flushValveOnAtMs < flushValveDelay ) {   // if flush timer not expired
    digitalWrite(flushValveSolenoid, HIGH);   //open flush out valve
  }
  else {
    digitalWrite(flushValveSolenoid, LOW);         //close flush out valve
    inFlush = false ;
  }

  if (prePsi < flushValveThresholdPsi) {              //if the psi is lower than treshold
    if ( inFlush == false ) {
      inFlush = true ;                     
      flushValveOnAtMs = millis() ;                 // start flush timer
    }
  }

  if (postPsi - prePsi > yellowLEDThresholdPsi) {   //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, HIGH);                  //turn on the yellow LED
  }
  
  if (postPsi - prePsi > redLEDThresholdPsi) {      //if the pressure drop is greater than threshold
    digitalWrite(yellowLED, LOW);                   //turn yellow LED off
    digitalWrite(redLED, HIGH);                     //turn red LED on  
  }
    
}

I like the idea of having the manual flush simply invoke the same mechanism as the automatic. It also means that you can police the permissible frequency of the flush in the same place. Below is the code I adapted to my test rig of leds & pots. NOTE THAT some of the pins changed to suit my setup. Note also that some of the intervals/delays are smaller to facilitate testing.

float prePsi;      //declaring pre-filter sensor psi
float postPsi;     //declaring post-filter sensor psi

byte preSensorPin = A0;   //assigning pre-filter sensor to A0
byte postSensorPin = A1;  //assigning pre-filter sensor to A1

const byte yellowLEDPin = 2;            //assigning pin number to yellow LED
const byte redLEDPin = 5;               //assigning pin number to red LED
const byte flushValveSolenoidPin = 7;   //assigning pin number to flush valve
const byte buttonPin = 12;//A2;         //assigning a pin number to button

int redLEDThresholdPsi = 10;       //assigning a pressure threshold for red LED to illuminate
int yellowLEDThresholdPsi = 5;     //assigning a pressure threshold for yellow LED to illuminate
int flushValveThresholdPsi = 85;   //assinging a pressure threshold for flush out valve to open

const uint32_t flushValveDelay = 15000UL;  //assigning a delay for flush out valve
const uint32_t minFlushInterval = 2UL * 60UL * 1000UL; // Minimum time between flushes, however invoked

uint32_t flushValveOnAtMs = 0 ;  // flush timer
bool inFlush = false ;           // flush timer status

void setup()
{
  Serial.begin(115200);
  Serial.println("\nStarting...");
  pinMode(preSensorPin, INPUT);        //sets pre-filter sensor value as input
  pinMode(postSensorPin, INPUT);       //sets post-filter sensor value as input
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(flushValveSolenoidPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  if (!inFlush)
  {
    ManageManualFlush();
  }
  prePsi = GetPsi(preSensorPin);
  Serial.print("Pre-filter psi: ");            
  Serial.print(prePsi);                                           
  postPsi = GetPsi(postSensorPin);
  Serial.print("  Post-filter psi: ");         
  Serial.println(postPsi);                                        
  delay(2000);                                                      //delay in between reads for stability
  ManageAutoFlush();
  ManageLeds();
}

float GetPsi(byte AnalogPin)
{
  int SensorValue = analogRead(AnalogPin);                       //read raw reading from sensor
  float Voltage = (5.0 / 1023.0) * SensorValue;                  //calculating voltage from raw reading
  return (Voltage - 0.5) * (100.0) / (4.5 - 0.5);                //calculating psi from voltage
}

void ManageLeds()
{
  if (postPsi - prePsi > redLEDThresholdPsi)        //if the pressure drop is greater than red threshold
  {
    //Serial.println("Show red - threshold exceeded");
    digitalWrite(yellowLEDPin, LOW);                         //turn yellow LED off
    digitalWrite(redLEDPin, HIGH);                           //turn red LED on
  }
  else if (postPsi - prePsi > yellowLEDThresholdPsi)      //if the pressure drop is greater than yellow threshold
  {
    //Serial.println("Show yellow - threshold exceeded");
    digitalWrite(yellowLEDPin, HIGH);                        //turn on the yellow LED
    digitalWrite(redLEDPin, LOW);                            //turn red LED off
  }
  else
  {
    //Serial.println("Show nothing - within tolerance");
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(redLEDPin, LOW);
  }
}

void ManageManualFlush()
{
  if (digitalRead(buttonPin) == HIGH)        // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  {
    Serial.println(F("Manual flush"));
    StartFlush();
  }
}

void ManageAutoFlush()
{
  if ( inFlush && millis() - flushValveOnAtMs >= flushValveDelay )  // if flush timer expired stop flushing
  {
    digitalWrite(flushValveSolenoidPin, LOW);         //close flush out valve
    inFlush = false;
    Serial.println(F("Stop flush timer"));
  }
  if (!inFlush && prePsi < flushValveThresholdPsi)    //if the psi is lower than threshold start auto flush
  {
    Serial.println(F("Auto flush"));
    StartFlush();
  }
}

void StartFlush()
{
  if ((millis() - flushValveOnAtMs > minFlushInterval) || flushValveOnAtMs == 0)  // Honour an initial request for flush - who knows when we did it last
  {
    inFlush = true;
    digitalWrite(flushValveSolenoidPin, HIGH);   // open flush out valve
    flushValveOnAtMs = millis();                 // start flush timer
    Serial.println(F("Start flush timer"));
  }
  else
  {
    Serial.print(F("Flush request denied - did one too recently. Next opportunity in: "));
    Serial.print((minFlushInterval-(millis() - flushValveOnAtMs))/1000UL);
    Serial.println(F(" seconds."));
  }
}

Do as you will with it.

Note also, that useful comments are encouraged, but adding comments for the sake of it, especially those that simply echo the code is best avoided.

Thank you for the advice on comments. I was erroring on the side of caution, for myself mostly I guess.

Also a big thank you for the code. It seems to work great, with the only exception being that the yellow and red LED's don't stay on once illuminated. This is important because once a the water starts flowing, and a pressure drop is seen, it will trip the condition, but once the water stops flowing, the pressures will equalize, and the condition will no longer be tripped, but the light needs to stay on to alert the user. Does that make sense? Also, I only have one potentiometer until Monday when my other order arrives, so not sure if I had control of both pre and post "sensors" the code would in fact work.

Thanks again for your help, and like I said earlier, I would be happy to Venmo you money. If your good at something, never do it for free.

If the red and yellow leds must "latch" on, you'll probably need to add a reset button (not the arduino reset button), which the user operates, to unlatch them and/or a timeout.

Here's the latching version of ManageLeds:

void ManageLeds()
{
  if (postPsi - prePsi > redLEDThresholdPsi)        //if the pressure drop is greater than red threshold
  {
    //Serial.println("Show red - threshold exceeded");
    digitalWrite(redLEDPin, HIGH);                           //turn red LED on
  }
  else if (postPsi - prePsi > yellowLEDThresholdPsi)      //if the pressure drop is greater than yellow threshold
  {
    //Serial.println("Show yellow - threshold exceeded");
    digitalWrite(yellowLEDPin, HIGH);                        //turn on the yellow LED
  }
  else
  {
    //Serial.println("Show nothing - within tolerance");
  }
}

Compiled, not tested.

Also, it occurs to me that the manual flush control may be wrong. As written, it won't flush if the auto flush happened recently. I rather suspect that the user would prefer that it flushes when it's told to, perhaps with the exception of when it's actually flushing.