Using millis() within an if statement

I have built a machine for a client and it is controlled using a P1AM-100 micro-controller with various I/O modules (the P1AM-100 is a DIN rail mounted, industrial Arduino that uses the MKR-Zero).
It is a fairly simple machine, when the start button is pushed, a motor starts then an air cylinder begins to extend. When the cylinder reaches the end of stroke (reed switch input to Arduino), it pauses for a pre-set duration and then the cylinder retracts until it is home, at which point the motor turns off. There are several more things and conditions going on but that;'s the basics and it all works very well. My client requested that the "dwell time" be adjustable via pa potentiometer with LCD display rather than in the code, they also requested that a "Home" button be installed that returns everything to the starting position regardless of where in the cycle it is. This "Home" button means I can't simply put a potentiometer controlled delay in the code as it wouldn't go to home until the delay ended (up to 10 minutes).

It isn't enough to simply have an "if(millis()-time > voltage1000) "* statement since there are more conditions involved.

Can I embed the millis part within an if statement?
eg. "if (hoodState==1 && stopState==0 && homeState==0 && augState==1 && extState==1 &&(millis()-time > voltage1000) )"* ?

why? just place conditional test code blocks inside the millis() test code block. Don't combine them in the test - then the timing stops completely if any of the conditions are not met.

I think I understand what you're saying, need to chew on it for a bit.
I will note that nothing else is happening during this dwell interval and, there is only one set of conditions where the dwell interval is required so the last part of your answer is not a concern (if I understand you).

I think you have to take a step back and not think about program code.
Can you describe in words what should be done in which situation ?

A millis-timer has code to start it and code for the millis-timer itself. The millis-timer runs on its own in the loop(). It is easy to turn it off or change the interval. I often use a bool enabled variable to be able to turn the millis-timer off.

// ----------------------
// start the millis-timer
// ----------------------
if some conditions are met
{
  // start the millis-timer
  enabled = true;
  previousMillis = millis();
}

...

// ----------------------
// the millis-timer
// ----------------------
if( enabled)    // does the millis timer run or not ?
{
  if( millis() - previousMillis >= interval)
  {
    previousMillis = millis();

    // millis-timer code
  }
}

keep your logic simple... and separated...

loop
{
  if x 
    do y

  if (homePressed)
    do retractCylinder

  if (millis - endOfStrokeTime > dwellTime)
    do retractCylinder
}

It is often much better to make the code a little more verbose by separating out the logic... it makes it much easier to understand and maintain. Anyone that has ever had to read their own code 12 months after they wrote it, will know what I mean...

Thanks, your example just enlightened me to something I didn't know could be done...
Having an if statement without a corresponding "do" something" line.
The fog is lifting.

You are certainly right about looking at code I wrote a year ago.
Lately, I have been adding remarked out text that explains each line of code as I see done in most examples (except where the line is very obvious).
A big part of my problem is that I don't do this regularly enough and so, never really learn all of the nuanced tricks.

I will try to simplify my logic into the most basic pieces but I am finding that there is always one or two other conditions that must be met for any one of the functions to happen.

Where logic needs to be complex (and sometimes it does), then comments are your friend. The more complex, the more you had to think about it... now and in the future when you look at it again... don't make yourself have to figure it out all over again :slight_smile:

PS. descriptive variable names are also friends of your future self.