using delay within the if statement

I am trying to include a delay before a function is run. The function looks like this:

/*********************Pressure Variables**********************************/
int incomingAnalog = 0;
int Pressure = 0;
int MaxPressure = 3000;
int sensorMax = 0;
bool cycleDone = false;
/*************************************************************************/
unsigned long currentMillis = 0;
const long interval = 1000;           // interval (milliseconds)
unsigned long previousMillis = 0; 

bool cyclestatus = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  currentMillis = millis();
  incomingAnalog = analogRead(A0);
  Pressure = map(incomingAnalog, 0, 1023, 0, MaxPressure);  
  sensorMax = 0;
  cyclestatus = false;

  while (Pressure > 50) {
    while (!cyclestatus){
    currentMillis = millis();
    delay(1000);
    incomingAnalog = analogRead(A0);
    Pressure = map(incomingAnalog, 0, 1023, 0, MaxPressure);
    
    Serial.print("Current Pressure: ");
    Serial.println(Pressure);

    if (Pressure > sensorMax) sensorMax = Pressure;
    else if (Pressure < sensorMax) sensorMax = sensorMax;

    Serial.print("Max Sensor Value: ");
    Serial.println(sensorMax);

   // while((millis() < currentMillis + interval) && Pressure < 50) {cycleMessage(); Serial.println("Hello Me hu");}
    if (Pressure < 50) cycleMessage();
    }
  }
}

bool cycleMessage() {
 // counter = 0;
  cyclestatus = true;
  Serial.print("Sending data with Pressure:");
  Serial.println(sensorMax);
  return cyclestatus;
}

So in the if statement where it looks for Pressure < 50, i need to add an "AND" to also check for 2 sec delay before running the cycleMessage() function.

So in the if statement where it looks for Pressure < 50, i need to add an "AND" to also check for 2 sec delay before running the cycleMessage() function.

Not the best way, but as your code uses delay() already...

if (Pressure < 50) 
  {
  delay(2000);
  cycleMessage();
  }

However, I suspect that you have not properly described the requirement

Yes i dont want to use the delay(2000).

This way if the pressure goes above 50, it will ignore that.
So let me clear the agenda.
Once the pressure goes above 50, it runs the routine. Once the pressure goes below 50, it waits for a couple of seconds to make sure that the pressure does not go above 50 again, and then it runs the cyclemessage() funciton.

Delay(200) wont solve that because i still need to while function to check for pressure increase.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

If you want something to happen ONLY if the pressure is above 50 for 2 seconds then do it like this pseudo code

if (pressure < 50 )  {
   lastLowPressureTime = millis()
}

if (millis() - lastLowPressureTime >= 2000) {
    // pressure has been high for 2 secs so do stuff
}

...R

Thank you.
In your code, if the pressure < 50, the "lastLowPressureTime" will keep updating to millis(), so this wont work. Because the statement "millis() - lastLowPressureTime" will always return 0, and will never go above 2000 to engage.

The system system should wait 2 seconds after hitting the pressure<50 and then run the next function, if the mean time the pressure goes above 50, the while loop will again wait for pressure<50 to run the 2 sec timer.

Delta_G:
Pay attention to how he wrote that. I think maybe he got your requirement backwards.

I think so. To do it the other way it would be like this

if (pressure > 50 )  {
   lastHighPressureTime = millis()
}

if (millis() - lastHighPressureTime >= 2000) {
    // pressure has been low for 2 secs so do stuff
}

...R

Delta_G:
Pay attention to how he wrote that. I think maybe he got your requirement backwards. The if in the code there will trigger if the pressure has been high for 2 seconds. Surely from that you can figure out how to make it work reverse. Just check for the last time it was high instead of the last time it was low.

Understood.

Robin2:
I think so. To do it the other way it would be like this

if (pressure > 50 )  {

lastHighPressureTime = millis()
}

if (millis() - lastHighPressureTime >= 2000) {
    // pressure has been low for 2 secs so do stuff
}




...R

I see what you are saying. Thank you. A very simple solution but i made it way more complicated. thank you.

To do it only when pressure has been high and then goes low for two seconds or more:

int Pressure = 0;
const int MaxPressure = 3000;

void setup() {}

void loop()
{
  unsigned long currentMillis = millis();
  static unsigned long lastHighPressureTime = 0;

  Pressure = map(analogRead(A0), 0, 1023, 0, MaxPressure);  
  
  if (Pressure > 50)
  {
    lastHighPressureTime = currentMillis;
  }

  if (lastHighPressureTime != 0 && currentMillis - lastHighPressureTime >= 2000)
  {
    // pressure has been low for 2 secs so do stuff

    // Don't do it again until the pressure goes high again
    lastHighPressureTime = 0;
  }
}