want to run two loops with different delays

Hi guys,

I have two loops and want to run loop1 every 30 seconds and loop1 every 30 seconds at the same time.

I tried this code but the result is not accurate as I want:

unsigned long startMillis;
unsigned long currentMillis;



void setup() {

 // put your setup code here, to run once:

Serial.begin(115200);


  startMillis = millis();
}

void loop() {
  // put your main code here, to run repeatedly:

Hi ();
Hello ();
  
}

void Hi () {
  
const unsigned long period = 30000; 
currentMillis = millis();  //get the current time
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    
// put your main code here, to run repeatedly:

  Serial.println("Hi");
  

   startMillis = currentMillis;
  }
}


void Hello () {

const unsigned long periods = 30000; 
  currentMillis = millis();  //get the current time
  if (currentMillis - startMillis >= periods)  //test whether the period has elapsed
  {
    

  Serial.println("Hello");
  
   startMillis = currentMillis;

  }
}

Any help please for this issue

Thanks for all

Use separate startMillis in your two functions.

you mean use startMillis = millis(); under every loop?

startMillis1
and
startMillis2

Yes thanks it works :wink: