Other Arduino Codes and Videos by Robojax

Arduino Millis function explained with 3 examples

How to use millis() function with Arduino - Example 1

This code example has been explained in video. Please watch the video and use this code.

Resources for this sketch

Example 1 of using millis() in Arduino programming


 /*
 * Example-1 of using millis() with Arduino
 * Written by Ahmad Shamshiri on July 27, 2019
 * in Ajax, Ontario, Canada
 * Watch video instruction for millis() :
 */

unsigned long event = 5000;// 5 seconds
void setup() {
  Serial.begin(9600);//initialize serial monitor
  Serial.println("Introduction to millis");
  pinMode(2,INPUT_PULLUP);
 
}

void loop() {
  // Robojax.com millis() example 1
  Serial.println(millis());
  int pb = digitalRead(2);// read pin 2
  if(pb ==LOW)
  {
      if(millis() >= event)
      {
        Serial.println("Take action");
      }// if end
  }// if end
  
}// loop end
   

Example 2 of using millis() in Arduino programming


 
/*
 * Example-2 of using millis() with Arduino
 * Written by Ahmad Shamshiri on July 27, 2019
 * in Ajax, Ontario, Canada
 * Watch video instruction for millis() :
 */

unsigned long event = 5000;// 5 seconds
unsigned long buttonPushed =0;

void setup() {
  Serial.begin(9600);//initialize serial monitor
  Serial.println("Introduction to millis");
  pinMode(2,INPUT_PULLUP);
  


}

void loop() {
  // Robojax.com example-2 millis()
  Serial.println(millis());
  int pb = digitalRead(2);// read pin 2
  if(pb ==LOW && buttonPushed ==0)
  {
    buttonPushed =millis();
    Serial.print("First push at:");
    Serial.println(buttonPushed);
  }
  if( pb ==LOW &&  buttonPushed >0 )
  {
   unsigned long difference = millis()- buttonPushed;
      if( difference >= event)
      {
        Serial.println("Action after 5 seconds of first push");
      }
  }
  
}   

Get Early access to my videos and codes via Patreon http://bit.ly/2ZnITLz be my Patron with monthly pledge of $1, $5 or $10.

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal