Skip to main content
Fixed question as best as possible, added tag.
Source Link
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31

mill millis() replace delay  () question

// each "event" (LED) gets their own tracking variable
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;
 
// different intervals for each LED
int intervalLED12 = 500;
int intervalLED13 = 5000;

int intervalLED3 = 2000;
 
// each LED gets a state varaible
boolean LED13state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED12state = false;     // need to seed the light to be OFF
 
void setup() {
   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);

   pinMode(3, OUTPUT);
   
}
void loop() {
   // get current time stamp
   // only need one for both if-statements
   unsigned long currentMillis = millis();
 
   // time to toggle LED on Pin 12?
   if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) {
      LED12state = !LED12state;
      digitalWrite(12, LED12state);
      // save current time to pin 12's previousMillis
      previousMillisLED12 = currentMillis;
   }
 
// time to toggle LED on Pin 13?
  if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) {
      LED13state = !LED13state;
      digitalWrite(13, LED13state);
      // save current time to pin 13's previousMillis
      previousMillisLED13 = currentMillis;
  }
}

questionMy questions:

  1. Didn't see the led12 and led13 have 10 times defferency , almost same intervals, why?

    Didn't see the led12 and led13 have 10 times defferency, almost same intervals, why?

  2. how can use mill() as simple as delay, is that a must to use if.

    How can I use millis() as simple as delay?

  3. Is that a must to use if?

mill () replace delay  () question

// each "event" (LED) gets their own tracking variable
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;
 
// different intervals for each LED
int intervalLED12 = 500;
int intervalLED13 = 5000;

int intervalLED3 = 2000;
 
// each LED gets a state varaible
boolean LED13state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED12state = false;     // need to seed the light to be OFF
 
void setup() {
   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);

   pinMode(3, OUTPUT);
   
}
void loop() {
   // get current time stamp
   // only need one for both if-statements
   unsigned long currentMillis = millis();
 
   // time to toggle LED on Pin 12?
   if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) {
      LED12state = !LED12state;
      digitalWrite(12, LED12state);
      // save current time to pin 12's previousMillis
      previousMillisLED12 = currentMillis;
   }
 
// time to toggle LED on Pin 13?
  if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) {
      LED13state = !LED13state;
      digitalWrite(13, LED13state);
      // save current time to pin 13's previousMillis
      previousMillisLED13 = currentMillis;
  }
}

question:

  1. Didn't see the led12 and led13 have 10 times defferency , almost same intervals, why?
  2. how can use mill() as simple as delay, is that a must to use if.

millis() replace delay() question

// each "event" (LED) gets their own tracking variable
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;
 
// different intervals for each LED
int intervalLED12 = 500;
int intervalLED13 = 5000;

int intervalLED3 = 2000;
 
// each LED gets a state varaible
boolean LED13state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED12state = false;     // need to seed the light to be OFF
 
void setup() {
   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);

   pinMode(3, OUTPUT);
   
}
void loop() {
   // get current time stamp
   // only need one for both if-statements
   unsigned long currentMillis = millis();
 
   // time to toggle LED on Pin 12?
   if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) {
      LED12state = !LED12state;
      digitalWrite(12, LED12state);
      // save current time to pin 12's previousMillis
      previousMillisLED12 = currentMillis;
   }
 
// time to toggle LED on Pin 13?
  if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) {
      LED13state = !LED13state;
      digitalWrite(13, LED13state);
      // save current time to pin 13's previousMillis
      previousMillisLED13 = currentMillis;
  }
}

My questions:

  1. Didn't see the led12 and led13 have 10 times defferency, almost same intervals, why?

  2. How can I use millis() as simple as delay?

  3. Is that a must to use if?

added 178 characters in body
Source Link
Jot
  • 3.3k
  • 1
  • 14
  • 21
// each "event" (LED) gets their own tracking variable
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;
 
// different intervals for each LED
int intervalLED12 = 500;
int intervalLED13 = 5000;

int intervalLED3 = 2000;
 
// each LED gets a state varaible
boolean LED13state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED12state = false;     // need to seed the light to be OFF
 
void setup() {
   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);

   pinMode(3, OUTPUT);
   
}
void loop() {
   // get current time stamp
   // only need one for both if-statements
   unsigned long currentMillis = millis();
 
   // time to toggle LED on Pin 12?
   if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) {
      LED12state = !LED12state;
      digitalWrite(12, LED12state);
      // save current time to pin 12's previousMillis
      previousMillisLED12 = currentMillis;
   }
 
// time to toggle LED on Pin 13?
  if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) {
      LED13state = !LED13state;
      digitalWrite(13, LED13state);
      // save current time to pin 13's previousMillis
      previousMillisLED13 = currentMillis;
  }
}

unsigned long previousMillisLED12=0; unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;

// different intervals for each LED int intervalLED12 = 500; int intervalLED13 = 5000;

int intervalLED3 = 2000;

// each LED gets a state varaible boolean LED13state = false; // the LED will turn ON in the first iteration of loop() boolean LED12state = false; // need to seed the light to be OFF

void setup() { pinMode(13, OUTPUT); pinMode(12, OUTPUT);

pinMode(3, OUTPUT);

} void loop() { // get current time stamp // only need one for both if-statements unsigned long currentMillis = millis();

// time to toggle LED on Pin 12? if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) { LED12state = !LED12state; digitalWrite(12, LED12state); // save current time to pin 12's previousMillis previousMillisLED12 = currentMillis; }

// time to toggle LED on Pin 13? if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) { LED13state = !LED13state; digitalWrite(13, LED13state); // save current time to pin 13's previousMillis previousMillisLED13 = currentMillis; } }

// each "event" (LED) gets their own tracking variable

unsigned long previousMillisLED12=0; unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;

// different intervals for each LED int intervalLED12 = 500; int intervalLED13 = 5000;

int intervalLED3 = 2000;

// each LED gets a state varaible boolean LED13state = false; // the LED will turn ON in the first iteration of loop() boolean LED12state = false; // need to seed the light to be OFF

void setup() { pinMode(13, OUTPUT); pinMode(12, OUTPUT);

pinMode(3, OUTPUT);

} void loop() { // get current time stamp // only need one for both if-statements unsigned long currentMillis = millis();

// time to toggle LED on Pin 12? if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) { LED12state = !LED12state; digitalWrite(12, LED12state); // save current time to pin 12's previousMillis previousMillisLED12 = currentMillis; }

// time to toggle LED on Pin 13? if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) { LED13state = !LED13state; digitalWrite(13, LED13state); // save current time to pin 13's previousMillis previousMillisLED13 = currentMillis; } }

// each "event" (LED) gets their own tracking variable
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;
 
// different intervals for each LED
int intervalLED12 = 500;
int intervalLED13 = 5000;

int intervalLED3 = 2000;
 
// each LED gets a state varaible
boolean LED13state = false;     // the LED will turn ON in the first iteration of loop()
boolean LED12state = false;     // need to seed the light to be OFF
 
void setup() {
   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);

   pinMode(3, OUTPUT);
   
}
void loop() {
   // get current time stamp
   // only need one for both if-statements
   unsigned long currentMillis = millis();
 
   // time to toggle LED on Pin 12?
   if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) {
      LED12state = !LED12state;
      digitalWrite(12, LED12state);
      // save current time to pin 12's previousMillis
      previousMillisLED12 = currentMillis;
   }
 
// time to toggle LED on Pin 13?
  if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) {
      LED13state = !LED13state;
      digitalWrite(13, LED13state);
      // save current time to pin 13's previousMillis
      previousMillisLED13 = currentMillis;
  }
}
Source Link

mill () replace delay () question

// each "event" (LED) gets their own tracking variable

unsigned long previousMillisLED12=0; unsigned long previousMillisLED13=0;

unsigned long previousMillisLED3=0;

// different intervals for each LED int intervalLED12 = 500; int intervalLED13 = 5000;

int intervalLED3 = 2000;

// each LED gets a state varaible boolean LED13state = false; // the LED will turn ON in the first iteration of loop() boolean LED12state = false; // need to seed the light to be OFF

void setup() { pinMode(13, OUTPUT); pinMode(12, OUTPUT);

pinMode(3, OUTPUT);

} void loop() { // get current time stamp // only need one for both if-statements unsigned long currentMillis = millis();

// time to toggle LED on Pin 12? if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) { LED12state = !LED12state; digitalWrite(12, LED12state); // save current time to pin 12's previousMillis previousMillisLED12 = currentMillis; }

// time to toggle LED on Pin 13? if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) { LED13state = !LED13state; digitalWrite(13, LED13state); // save current time to pin 13's previousMillis previousMillisLED13 = currentMillis; } }

question:

  1. Didn't see the led12 and led13 have 10 times defferency , almost same intervals, why?
  2. how can use mill() as simple as delay, is that a must to use if.