millis() interval ==???

ok ive got

long previousMillis = 0;
long interval = 20000; 

unsigned long currentMillis = millis();
previousMillis = currentMillis;
else if (currentMillis - previousMillis >= interval)
previousMillis = currentMillis;

I need a way to make it only print the first 20 seconds only not every 20 seconds!! the 20 seconds is from when an event happens in the 1st if statement!

I don't understand your question.

I was going to suggest a modification to your code when I realized you have not posted it all so I can't. Code snippets are generally useless.

I suspect you are setting currentMillis in the wrong place.

Also, make all the millis() related values unsigned long

And I doubt if you need

if {
  previousMillis = currentMillis;

Have a look at how millis() is used in several things at a time


so by telling the arduino that previous millis is equal to current millis I am doing a time stamp....

surely there is an easy way to do a certain function 20 seconds from that timestamp but only once, until there is a new time stamp....?

A time stamp is whenever you make one, so the concept of "new" is up to you.

yes but I want the function to happen when the millis hits 20 seconds not every time it finds the millis equal to or more than 20 seconds (>=)

long previousMillis

Always use type "unsigned long" when working with millis() calculations.


yes but I want the function to happen when the millis hits 20 seconds not every time it finds the millis equal to or more than 20 seconds (>=)

I can't see the difference.

I don't know what other way to explain it. I want it to do the function at 20 seconds only.

Have you seen the blink without delay example in the IDE?

of course. blink without delay is the basis of my sketch

So, stretch the second to 20 seconds.
I can't see the problem here.

I am not trying this

else if(currentMillis - previousMillis >= 20000 && currentMillis - previousMillis <= 25000)

Is this helpful?

const int ledPin =  13;            // the number of the LED pin
int ledState = LOW;                // ledState used to set the LED
unsigned long previousMillis = 0;  // will store last time LED was updated
const unsigned long interval = 20*1000UL;   // interval at which to blink (milliseconds)
boolean flag = true;               // if true, continue to run        

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT); 
  //Run MyBlink 20 seconds from now
  previousMillis = millis(); 

void loop()
  unsigned long currentMillis = millis();

  if((currentMillis - previousMillis >= interval) && flag == true) {
    //uncommented the next line if you what things to restart
    //previousMillis = currentMillis; 
    //comment the next line if you want this to happen more than once
    flag = false;

void MyBlink()
  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
    ledState = HIGH;
    ledState = LOW;
  // set the LED with the ledState of the variable:
  digitalWrite(ledPin, ledState);

I'll try that now

Thanks Larry

You can set the start time to 0 to indicate that the timer should not be checked:

// Every 10 minutes turn on the LED and turn it off 20 seconds later

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Generally, you should use "unsigned long" for variables that hold time
unsigned long repeatStart = 0;   // Timer start for repeating
unsigned long delayStart = 0;    // Timer start for delay

// constants won't change :
const unsigned long repeatInterval = 600000UL;  // ten minutes
const unsigned long delayInterval = 20000UL;  // 20 seconds

void setup() {
  pinMode(ledPin, OUTPUT);

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - repeatStart >= repeatInterval) {
    // Turn LED on
    digitalWrite(ledPin, HIGH);

    // re-set the repeatStart to make it repeat
    repeatStart = currentMillis;

    // Start delay interval for delayed turn-off
    delayStart = currentMillis;

  // If the delay timer is running, check for the end of delay
  if (delayStart != 0 &&
      currentMillis - delayStart >= delayInterval) {
    // Turn LED off
    digitalWrite(ledPin, LOW);
    // Done with delay for now
    delayStart = 0;