mdraber
Published © GPL3+

Using millis() Function as an Alternative to Using Delay

In this tutorial I am looking at using millis function in the code. Describing the advantages it has over using delay function.

BeginnerFull instructions provided12,519
Using millis() Function as an Alternative to Using Delay

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
LED (generic)
LED (generic)
×1

Story

Read more

Schematics

Connectivity

Code

Classic Blink Sketch - using delay function

Arduino
#define LED1 3

void setup() {
 pinMode(LED1, OUTPUT);
}

void loop() {
 digitalWrite(LED1, HIGH); 
 delay(1000); 
  digitalWrite(LED1, LOW); 
  delay(1000); 
}

Blinking 2 LEDs - Using delay function

Arduino
#define LED1 3
#define LED2 9



void setup() {
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);

}
void loop() {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH); 
  delay(100); 
  digitalWrite(LED2, LOW);
  delay(100);
  digitalWrite(LED2, HIGH); 
  delay(100); 
  digitalWrite(LED2, LOW);
  delay(100);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, HIGH); 
  delay(100); 
  digitalWrite(LED2, LOW);
  delay(100);
  digitalWrite(LED2, HIGH); 
  delay(100); 
  digitalWrite(LED2, LOW);
  delay(100);

}

Blink Sketch - using millis

Arduino
unsigned long ms_from_start =0;
unsigned long ms_previous_read_LED1 = 0;
unsigned long LED1_interval=1000;

#define LED1 3

int LED1_state=0;

void setup() {
  pinMode(LED1, OUTPUT);
}

void loop() {
  ms_from_start = millis();
  if (ms_from_start-ms_previous_read_LED1>LED1_interval){
    ms_previous_read_LED1=ms_from_start;
    if (LED1_state==0) LED1_state=1; else LED1_state=0;
    digitalWrite(LED1,LED1_state);
  }
}

Binking two LEDs - using millis

Arduino
unsigned long ms_from_start =0;
unsigned long ms_previous_read_LED1 = 0;
unsigned long LED1_interval=1000;
unsigned long ms_previous_read_LED2 = 0;
unsigned long LED2_interval=250;


#define LED1 3
#define LED2 9


int LED1_state=0;
int LED2_state=0;


void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}
void loop() {
  ms_from_start = millis();

  if (ms_from_start-ms_previous_read_LED1> LED1_interval){
    ms_previous_read_LED1=ms_from_start;
    if (LED1_state==0) LED1_state=1; else LED1_state=0;
    digitalWrite(LED1,LED1_state);
  }

  if (ms_from_start-ms_previous_read_LED2> LED2_interval){
    ms_previous_read_LED2=ms_from_start;
    if (LED2_state==0) LED2_state=1; else LED2_state=0;
    digitalWrite(LED2,LED2_state);
  }

}

Blinking 2 LEDs - with millie, changing frequency with potentiometers

Arduino
unsigned long ms_from_start =0;
unsigned long ms_previous_read_LED1 = 0;
unsigned long LED1_interval=0;
unsigned long ms_previous_read_LED2 = 0;
unsigned long LED2_interval=0;


#define LED1 3
#define LED2 9

#define P1 A7
#define P2 A1

int LED1_state=0;
int LED2_state=0;


void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(P1, INPUT);
  pinMode(P2, INPUT);
}
void loop() {
  ms_from_start = millis();
  LED1_interval=0;
  LED2_interval=0;
  for(int i=0;i<5;i++){
   LED1_interval=LED1_interval+map(analogRead(P1),0,1023,100,1000);
   LED2_interval=LED2_interval+map(analogRead(P2),0,1023,100,1000);
  }
  LED1_interval=LED1_interval/5;
  LED2_interval=LED2_interval/5;
  
  if (ms_from_start-ms_previous_read_LED1> LED1_interval){
    ms_previous_read_LED1=ms_from_start;
    if (LED1_state==0) LED1_state=1; else LED1_state=0;
    digitalWrite(LED1,LED1_state);
  }

  if (ms_from_start-ms_previous_read_LED2> LED2_interval){
    ms_previous_read_LED2=ms_from_start;
    if (LED2_state==0) LED2_state=1; else LED2_state=0;
    digitalWrite(LED2,LED2_state);
  }

}

Credits

mdraber

mdraber

49 projects • 67 followers

Comments