I am using milliss () function as shown in the code. But noted that the message comes after the period = 30000... But I want to first display the messsage and then display again after period using millis function.. Can someone help me get this done?

int period = 30000;
unsigned long time_now = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);

}

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

if(millis() > time_now + period){
time_now = millis();
Serial.print ("You got messagae");
}

}

Perhaps that?

@Idahowalker Thi is still working after the period.. The message is coming after the period...I want the message to come and then come again after period....
Is there a way do..while loop can help?

Add a copy of the print statement to the end of setup.

@Whandall Kindly show me in the code

There is only one print statement, only one setup, only one end of that routine.

Now you'll get a message after 3 seconds or every loop.

Oh you want the message to print all the time OK.

Show me what you mean by editing my code please

This will make the message A continuously.... I want the message to work once and then other after millis.
That's why I was asking if do...while loop will work

int period = 30000;
bool DisplayFirstTime = true;
unsigned long time_now = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);

}

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

if((millis() - time_now >= period) || DisplayFirstTime ){
DisplayFirstTime = false;
time_now = millis();
Serial.print ("You got messagae");
}

}

What does that mean?

Wow... This works well . Thank you so much

This works well... can you explain the code please?
In case I want to implement it elsewhere?

Perhaps you might understand a simpler version:

int period = 30000;
bool DisplayFirstTime = true;
unsigned long time_now = millis() - period;

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
}

void loop() {
  if((millis() - time_now >= period) {
  time_now = millis();
  Serial.print ("You got messagae");
  }
}
int period = 30000;
bool DisplayFirstTime = true;
unsigned long time_now = 0;
void setup()
{
  Serial.begin (9600);
}

void loop()
{
  // The || means 'or', so it reads like : if the period has passed or DisplayFirstTime is true
  // it then sets DisplayFirstTime to false, this allows only the timer to work.
  if ((millis() - time_now >= period) || DisplayFirstTime ) 
  {
    DisplayFirstTime = false;
    time_now = millis();
    Serial.print ("You got messagae");
  }
}

Can this work with a function....If I replace the Serial.print ("You got messagae") with a function...Let's say a function call to send SMS

With or without the 30 second delay?

Without:

void oneTime() {
  static bool firstTime = true;
  if (firstTime) {
  Serial.print ("You got messagae");
  firstTime = false;
  }
}
    

Yes. Just add "time_now == 0 || " to the beginning of the timer check. This will cause the 'if' to display the message the first time through loop() and then again every 30 seconds.

const unsigned int period = 30000;
unsigned long time_now = 0;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin (9600);
}

void loop()
{
  // put your main code here, to run repeatedly:
  if (time_now == 0 || millis() - time_now >= period)
  {
    time_now = millis();
    Serial.print ("You got messagae");
  }
}

As you can see, there are multiple solutions to implementing this task...

It will probably fill the serial buffer until it blocks, before the first millisecond is over.