Using millis instead of delay

The function delay pauses the program for the amount of time (in milliseconds) specified as parameter.  This can make your project a bit confusing sometimes, especially when you use it in Processing. As an advice, never use delay function in Processing.

Below a sample code you can use in Arduino:

int period = 5000;
long time_now = 0;
 
void setup() {
    Serial.begin(9600);
}
 
void loop() {
    if(millis() >= time_now + period){
        time_now += period;
        Serial.println("Hello");
    }
   
    //Run other code
}

If you are using Processing the code will be this:

int period = 5000;
long time_now = 0;
 
void setup() {
}
 
void draw() {
    if(millis() >= time_now + period){
        time_now += period;
        println("Hello");
    }
   
    //Run other code
}

this tutorial is based on this website:

Arduino Tutorial: Using millis() Instead of delay()