Flickering digital and analog inputs

So i’m trying to turn off a machine when it gets to certain pressure, the problem with this is that the pressure values are jumping so it’ll keep turning on and off the machine. Im looking for a good way to turn it off without it turning back on. i tried using a boolean value so it runs that piece of code only once but i figured that wouldn;t work for my application.

Also I’m having flickering digital inputs, I have a 1 megaohm pull down resistor so i dont know why they keep flickering. if i use the internal pull up resistors on the arduino they do not flicker they just stay high as they should.

I have attached my code below the digital input is D_ESTOP_SWITCH and the analog input is dischargeP

Thanks in advance.

code.PNG

Internal pull-up resistors are 20K-30K. You are using 1M (1000K). See the difference?

For the other part you need to build in some hysteresis. I’m not going to try to decipher a picture of part of the code so that’s the best I can do until you work out how to post code properly. Try reading https://forum.arduino.cc/index.php?topic=148850.0 particularly #7.

Steve

slipstick:
Internal pull-up resistors are 20K-30K. You are using 1M (1000K). See the difference?

For the other part you need to build in some hysteresis. I'm not going to try to decipher a picture of part of the code so that's the best I can do until you work out how to post code properly. Try reading https://forum.arduino.cc/index.php?topic=148850.0 particularly #7.

Steve

What difference does it make? I'm genuinely just asking cause i dont know much about pull down resistors. the external pull down resistors are 1mega ohm is that a real problem? will that make my inputs to keep bouncing around?

So i'm trying to turn off a machine when it gets to certain pressure, the problem with this is that the pressure values are jumping so it'll keep turning on and off the machine.

Beyond code and adding hysteresis what pressure, what is the medium and what sensor are you using. You may want to just try using a buffer or commonly called a pressure snubber. The effect of a pressure bouncing around is sometimes called "hammering" and the way it is removed is using a snubber. You are not the first to experience this sort of problem in a pressure line. You want to dampen things and you may want to try a snubber on your plumbing or other form of dampening. From an electrical point you also may want to condition your signal in using an RC Low Pass filter. Just figure a time constant you can live with.

Ron

heres my code:

if( millis() - millisPrint > 1000 ){
            Serial.println(counterEstop);  
        }
        //If estop OK and vals OK, actionStroke()
        if(digitalRead(D_ESTOP_SWITCH)){
          //static uint32_t timerESTOP = 0;
          uint32_t debounceDelay  = 100;
          if(millis() - timerESTOP > debounceDelay){
            counterEstop++;
            timerESTOP = millis() + debounceDelay;
            if(dischargeP < MAX_DISCHARGEPRESS){
              digitalWrite(O_MAIN_RELAY_ON, HIGH);
              digitalWrite(O_PUMP_ON, HIGH);
              digitalWrite(O_INPUT_VALVE, HIGH);
              digitalWrite(O_CHILLER_ON, HIGH);
            }else{
              if(dischargeP > MAX_DISCHARGEPRESS - 100 && dischargeP < MAX_DISCHARGEPRESS + 100){
                digitalWrite(O_MAIN_RELAY_ON, LOW);
                digitalWrite(O_PUMP_ON, LOW);
                digitalWrite(O_INPUT_VALVE, LOW);
                digitalWrite(O_CHILLER_ON, LOW);
              }
            }
          }
        }else{
          uint32_t debounceDelay  = 100;
          if( millis() > timerESTOP + debounceDelay) {
            digitalWrite(O_MAIN_RELAY_ON, LOW);
            digitalWrite(O_PUMP_ON, LOW);
            digitalWrite(O_INPUT_VALVE, LOW);
            digitalWrite(O_CHILLER_ON, LOW);
          }
        }

Ron_Blain:
Beyond code and adding hysteresis what pressure, what is the medium and what sensor are you using. You may want to just try using a buffer or commonly called a pressure snubber. The effect of a pressure bouncing around is sometimes called "hammering" and the way it is removed is using a snubber. You are not the first to experience this sort of problem in a pressure line. You want to dampen things and you may want to try a snubber on your plumbing or other form of dampening. From an electrical point you also may want to condition your signal in using an RC Low Pass filter. Just figure a time constant you can live with.

Ron

How do i add hysteresis, i don't really understand it, I'm using a 4-20 mA pressure transducer with maximum pressure of 15000 psi.

slipstick:
Internal pull-up resistors are 20K-30K. You are using 1M (1000K). See the difference?

For the other part you need to build in some hysteresis. I'm not going to try to decipher a picture of part of the code so that's the best I can do until you work out how to post code properly. Try reading https://forum.arduino.cc/index.php?topic=148850.0 particularly #7.

Steve

I posted my code correctly, how do i implement a hysteresis for this??

How do i add hysteresis, i don't really understand it, I'm using a 4-20 mA pressure transducer with maximum pressure of 15000 psi.

OK, there are a few ways but with a focus on a software solution this old thread is a very good read and an example. Another software solution would be using a running average code solution and I did not look through your code so sorry if you already tried that. Another name is smoothing.

OK, so you are using a 4 - 20 mA loop so 0 to 15000 PSI = 4 to 20 mA and you likely run the 4 to 20 mA through a 250 Ohm precision resistor to get 1 to 5 volts to an analog in pin. I will tell you right now that while a good software solution may work I am a little more fond of using a pressure snubber. I had to keep track of a 3,500 PSI N2 compressor and pressure hammering was a problem. I started with a mechanical pressure sensor and that worked. This wasn't an Arduino system but same idea. I also added a low pass RC filter since time was not a big factor, I wanted shutoff at about 3,500 PSI.

Anyway hopefully the links will help get you there and if not start looking at a snubber like I linked to earlier.

Ron

alfredfoby:
I posted my code correctly, how do i implement a hysteresis for this??

Well that’s good start but that’s definitely not a complete program. There’s no way it will compile. And with such a lot of unknown variables I have no idea what it’s supposed to be doing.

Hysteresis basically means that if you switch on when the pressure gets up to a certain value you want the pressure to get down to a lower value before you switch off. So that avoids too much switching if the value fluctuates slightly.

Steve

Yeah I know what it means I’m just not sure how to implement it.

I can’t really post all my code on here because there’s confidential information like my adafruit account on the code because I’m using adafruit to publish data.

If you could make a simple example of reading an analog input and turning off an output when it gets to point using hysteresis that would be awesome

This is an example I used to apply one type of hysteresis using analog in A0. There are a few ways to use hysteresis in a software solution. You should be able to work from this as the remarks sort of follow what is happening. I forget what I even developed from this but I tried it and it does compile so now you at least have a start. The idea is to look at the code and get a good handle on what is going on.

// The baud rate is the number of bits per second transmitted over the serial port. #define BAUD_RATE 9600

// This assumes a photoresistor is pulling A0 up and a resistor is pulling A0
// down. When the input is bright, the voltage increases, when dark, the
// voltage decreases.
#define INPUT_PIN A0

// Some version of the Arduino IDE don’t correctly define this symbol for an
// Arduino Uno.
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif

/****************************************************************/
// Global variables.

// The state of the system can be captured with only two values, e.g., it is
// represented as a single bit. The following statement defines two symbolic
// values, one for each possible state.
enum state_t { IS_DARK, IS_LIGHT };

// Declare the state variable as a symbolic value.
enum state_t state = IS_DARK;

// The hysteretic response is defined by using two thresholds.
const int light_threshold = 700;
const int dark_threshold = 300;

/**************/
/
Standard entry points for Arduino system /
/
/

// Standard Arduino initialization function to configure the system.

void setup()
{
// initialize the Serial port
Serial.begin( BAUD_RATE );

// configure our trivial I/O
pinMode( LED_BUILTIN, OUTPUT );

// the LED start out ON to match the initial state
digitalWrite(LED_BUILTIN, HIGH);
}

/****************************************************************/
// Standard Arduino polling function.

void loop()
{
// Read the ambient light level.
int input = analogRead(A0);

if (state == IS_LIGHT) {
if (input < dark_threshold) {
Serial.print("Dark observed at input level ");
Serial.println(input);
Serial.println(“Transitioning to the IS_DARK state.”);

state = IS_DARK;
digitalWrite(LED_BUILTIN, HIGH);
}

} else { // state must be IS_DARK
if (input > light_threshold) {
Serial.print("Light observed at input level ");
Serial.println(input);
Serial.println(“Transitioning to the IS_LIGHT state.”);

state = IS_LIGHT;
digitalWrite(LED_BUILTIN, LOW);
}
}
}

/****************************************************************/

Keep a few things in mind. You have your pressure transducer providing your 4 to 20 mA loop. As I mentioned earlier that is likely 1 to 5 volts so you have a 4 volt span for your pressure range.

The above is merely an example and you will have to tailor it to your needs. The idea of the code using this method is to take a state and represent that state using two values. These become your high and low. Matter of fact you can change the LIGHT and DARK to HIGH and LOW.

I am very new to these forums and my take is that the objective is to help people but they learn and do the work part. So I don’t want to upset the powers to be because I am getting to like the place. :slight_smile:

Does the code sample make sense now?

Ron

Ron_Blain:
This is an example I used to apply one type of hysteresis using analog in A0. There are a few ways to use hysteresis in a software solution. You should be able to work from this as the remarks sort of follow what is happening. I forget what I even developed from this but I tried it and it does compile so now you at least have a start. The idea is to look at the code and get a good handle on what is going on.

// The baud rate is the number of bits per second transmitted over the serial port. #define BAUD_RATE 9600

// This assumes a photoresistor is pulling A0 up and a resistor is pulling A0
// down. When the input is bright, the voltage increases, when dark, the
// voltage decreases.
#define INPUT_PIN A0

// Some version of the Arduino IDE don’t correctly define this symbol for an
// Arduino Uno.
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif

/****************************************************************/
// Global variables.

// The state of the system can be captured with only two values, e.g., it is
// represented as a single bit. The following statement defines two symbolic
// values, one for each possible state.
enum state_t { IS_DARK, IS_LIGHT };

// Declare the state variable as a symbolic value.
enum state_t state = IS_DARK;

// The hysteretic response is defined by using two thresholds.
const int light_threshold = 700;
const int dark_threshold = 300;

/**************/
/
Standard entry points for Arduino system /
/
/

// Standard Arduino initialization function to configure the system.

void setup()
{
// initialize the Serial port
Serial.begin( BAUD_RATE );

// configure our trivial I/O
pinMode( LED_BUILTIN, OUTPUT );

// the LED start out ON to match the initial state
digitalWrite(LED_BUILTIN, HIGH);
}

/****************************************************************/
// Standard Arduino polling function.

void loop()
{
// Read the ambient light level.
int input = analogRead(A0);

if (state == IS_LIGHT) {
if (input < dark_threshold) {
Serial.print("Dark observed at input level ");
Serial.println(input);
Serial.println(“Transitioning to the IS_DARK state.”);

state = IS_DARK;
digitalWrite(LED_BUILTIN, HIGH);
}

} else { // state must be IS_DARK
if (input > light_threshold) {
Serial.print("Light observed at input level ");
Serial.println(input);
Serial.println(“Transitioning to the IS_LIGHT state.”);

state = IS_LIGHT;
digitalWrite(LED_BUILTIN, LOW);
}
}
}

/****************************************************************/

Keep a few things in mind. You have your pressure transducer providing your 4 to 20 mA loop. As I mentioned earlier that is likely 1 to 5 volts so you have a 4 volt span for your pressure range.

The above is merely an example and you will have to tailor it to your needs. The idea of the code using this method is to take a state and represent that state using two values. These become your high and low. Matter of fact you can change the LIGHT and DARK to HIGH and LOW.

I am very new to these forums and my take is that the objective is to help people but they learn and do the work part. So I don’t want to upset the powers to be because I am getting to like the place. :slight_smile:

Does the code sample make sense now?

Ron

Yes it makes sense, Thank you, I did it in a similar way

alfredfoby:
How do i add hysteresis, i don't really understand it, I'm using a 4-20 mA pressure transducer with maximum pressure of 15000 psi.

You do understand hysteresis really, its when you switch the lights on when it gets dark, but switch them off only once its much lighter. If you switched the lights according to just one threshold, every passing cloud would cause you to toggle the lightswitch... Same thing here.

So two thresholds, only change state when dropping below the lower threshold, or rising above the upper threshold. Rising above the lower threshold or dropping below the upper threshold does nothing.