//define global variables
unsigned long previousTimeSerialPrint;
unsigned long timeNow;
unsigned long time_delay = 500; //keep as unsigned long
//so that the data types are the same
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
previousTimeSerialPrint = millis();
}
void loop() {
// put your main code here, to run repeatedly:
timeNow = millis();
if (timeNow - previousTimeSerialPrint > time_delay){
Serial.println("Hello World");
previousTimeSerialPrint = millis(); //resets the time to 0
}
}
/*
or you could set previousTimeSerialPrint = timeNow,
this will give the same result
*/