LED on off using Millis() function

Hi Guys.
I'm starting to study the Millis() function using simple code on and off with delay.
I've watched a lot of videos but still struggling to understand the Millis() function.
I want to start to understand how Millis work using Led on and off.
but i want to control the timing or the duration of the led off and on time.
ive found many post code and try to understand the code but still even if, i edit the duration time its not giving me the right result that i expect there still a problem.

can you please code this from using delay to millis?

I hope you can help me.
I'm just begginer and trying to learn.
thanks in advance guys.

LED 1 = 13;
LED 2 = 12;
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED 1, OUTPUT);
pinMode(LED 2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {`Preformatted text`
  digitalWrite(LED 1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(LED 1, LOW);    // turn the LED off by making the voltage LOW
  delay(3000);                       // wait for a second
}
digitalWrite(LED 2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(60000);                       // wait for a second
  digitalWrite(LED 2, LOW);    // turn the LED off by making the voltage LOW
  delay(10000);                       // wait for a second
     }
}

But you don't seem to have posted your attempt?

Did you look at...

Come on buddy... at least try.

Good attempt at posting code using code tags but as you might have noticed, it did not quite work out. </> is a button in the reply window of the forum, not something you must type

Edit your post, remove that, select all code and click the </> button. Next save your post

==

  1. LED 1 = 13 is incorrect syntax; look at the blink example how it is done. Same for LED 2 = 12.
  2. Where do you think that loop ends?

Got it thanks. :blush: :blush: :blush:

Hai red,
I will and keep on trying until it will give me the result i want.

Spaces aren't allowed in names ("identifiers"). You can use letters, digits, and unserscore characters. If you want something that looks like a space, use underscore:

LED_1 = 13;
LED_2 = 12;

Get used to auto formatting using ctrl-T. It shows the block indentation, here you can see another reason why your code won't compile:

LED 1 = 13;
LED 2 = 12;
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED 1, OUTPUT);
  pinMode(LED 2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  `Preformatted text`
  digitalWrite(LED 1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(LED 1, LOW);    // turn the LED off by making the voltage LOW
  delay(3000);                       // wait for a second
}
digitalWrite(LED 2, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(60000);                       // wait for a second
digitalWrite(LED 2, LOW);    // turn the LED off by making the voltage LOW
delay(10000);                       // wait for a second
}
}

The loop() ending problem was mentioned in reply #3.

@evenmerk

time = millis();

Work like ... when you write this line, millis function take the amount of millisecond from the time you turn on the Arduino. The variable "time" are unsigned long variable. A 4 byte number. A number from 0 to 4,294,967,295. When it reach the end, it will start from 0.

Let take an example ...

Let say you just start the Arduino and the code see this ... my_time = millis( ); So my_time will have the number ---> 10. And the code do other stuff, and when it read again the line, the number will be ---> 21. And repeat .... As you see, the number 10 ( the old value ) versus number 21 ( the new value ) have a difference of 11. So to make a delay of 1000, you need a difference of 1000 or above. When it reach the 1000 difference, you re-init the old value, and you compare with the "new" old value with a regular millis check, until you got again the 1000 difference, and repeat the cycle.

Let take the blink code

byte light = 5; // Let connect a led to pin 5 with a 470 ohms resistor.

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

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

Now with the use of millis()

byte light = 5; // connect a led to pin 5

boolean light_state; // the state of the led

unsigned long old_time;
unsigned long check_time;
unsigned long time_difference;

unsigned long time_delay = 1000; // the time delay you want. 

void setup()
{
   pinMode(light, OUTPUT);
  old_time = millis(); // check the time as now. 
  digitalWrite(light, HIGH); // Turn the led on
  light_state = true; // set the state to on.
}

void loop()
{
    check_time = millis(); // check the time now
   time_difference = old_time - check_time; // calculate the time difference
   // check the time difference is equal or more than 1000  AND the state of the led is ON 
   if ( (time_difference >= time_delay)  && ( light_state == true)) 
   {
        digitalWrite(light, LOW); // turn off the led
        light_state = false; // change the state of the led. 
        old_time = millis(); // re-init the time
   }
// check the time_difference is equal or more than 1000 AND the state of led is off
   if ( (time_difference >= time_delay)  && ( light_state == false)) 
   {
        digitalWrite(light, HIGH); // turn on the led
        light_state = true; // change the state of the led. 
        old_time = millis(); // re-init the time
   }
}

its getting clearer to me now. very detailed.

thank you very much for Sir.

I see. Thank you very much sir for correction. I'm starting to learn some points to avoid in coding.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.