Using a For loop as a timer

Hello! I’m new to Particle and just having some fun learning. I decided try a simple timer for my first project. I have a pulsing led to begin the timer, then the led goes solid for a set time, then it blinks to let the user know it’s done. I was hoping some folks could take a look at my code and let me know how I could refactor it to do better.

Is a For Loop even the right approach?

const int led = D3;
const int pushButton = D2;

int x;

unsigned long prepTime;
unsigned long medTime;
unsigned long downTime;


void setup() {
    
    pinMode(led, OUTPUT);
    pinMode(pushButton, INPUT_PULLUP);
    Serial.begin(9600);

}

void loop(){
    
    prepTime = 5;
    medTime = 5000;
    downTime = 4;
    
    int pushButtonState = digitalRead(pushButton);
    
    if(pushButtonState == LOW){
        
        for(int x = 0; x <= prepTime; x++){
            Serial.print(" Prep starts here: ");
            Serial.print(" led should fade in and out ");
            Serial.print(" Values: ");
            Serial.print(x);
                for(int i = 0; i <= 255; i+=5){
                    analogWrite(led, i);
                    delay(50);
                }
                for(int i = 255; i >= 0; i-=5){
                    analogWrite(led, i);
                    delay(50);
                }
            if(x == prepTime){
                x=0;
                break;
            }           
            
        }
        
        for(unsigned long x = 0; x <= medTime; x++){
            Serial.print(" Med starts here: ");
            Serial.print(" led should be solid ");
            Serial.print(" Values: ");
            Serial.print(x);
            digitalWrite(led, HIGH);
            if(x == medTime){
                x=0;
                break;
            }
        }
        
        for(int x = 0; x <= downTime; x++){
            Serial.print(" Down starts here: ");
            Serial.print(" led should be blinking ");
            Serial.print(" Values: ");
            Serial.print(x);
            digitalWrite(led, LOW);
            delay(1000);
            digitalWrite(led, HIGH);
            delay(1000);
            digitalWrite(led, LOW);
            delay(1000);
            if(x == downTime){
                x=0;
                Particle.publish("Complete","Good job!",60,PRIVATE); 
                break;
            }
        }
    }
}

Thanks in advance for your help!

This code would be blocking to the main system thread, so it may cause issues with the cloud connection. basically the “loop” has to finish at least every 10 seconds to service the cloud connection.

a better approach would be to use the millis() timer, have a search of the forums for non blocking code examples.

In addition to @Hootie81’s sound advise you might like to have a look at the references docs about Particle.process(), SYSTEM_MODE and SYSTEM_THREAD(ENABLED), which all deal with “interaction” between your code and the cloud maintenance.

Thanks mate! I started off down that route but got a little stuck. I’ll work it out, though.