Turn on LED indicator when arduino uno ON for 5 second

hi there,i try to give external LED indicator for Arduino board when i give voltage input 5v on arduino ,my external LED turn ON for 5 second and then turn off.i try use millis.but it likes i use delay().but my External LED still blinking. because i dont want blinking my LED.

unsigned long interval=1000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
  
bool ledState = false; // state variable for the LED
  
void setup() {
 pinMode(13, OUTPUT);
 digitalWrite(13, ledState);
}
  
void loop() {
 unsigned long currentMillis = millis(); // grab current time
  
 // check if "interval" time has passed (1000 milliseconds)
 if ((unsigned long)(currentMillis - previousMillis) >= interval) {
   
   ledState = !ledState; // "toggles" the state
   digitalWrite(13, ledState); // sets the LED based on ledState
   // save the "current" time
   previousMillis = millis();
 }
}

Instead of toggling the LED state using

    ledState = !ledState; // "toggles" the state

simply set it to the state that you require it to be at, either HIGH or LOW

thanks uncle!!!....GOD bless YOU!!!!

what you mean Sr, could you explain more please

Which part don't you understand ?

(simply set it to the state that you require it to be at, either HIGH or LOW)

this one sr

I change this.

bool ledState = false;

with this :

bool ledState = true;

and change this:

ledState = !ledState;

with this:

ledState = false;

and this is what i want...hheheeh

The current code changes the state of teh LED when the period ends, so the LED blinks
If instead of

ledState = !ledState; // "toggles" the state

the code did

ledState = HIGH; // set the state to HIGH

Then the LED would stay HIGH after the period ends

NOTE: this is not the best way to do what is required but is simple to change the code to do it

thanks alot

thanks Sr

A smarter way to do what @ridhoazhar313 says he/she wants to do

boolean timing = true;

void setup()
{
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW); //start with LED off
}

void loop()
{
  if (timing && millis() >= 5000) //test whether 5 seconds has elapsed
  {
    digitalWrite(13, HIGH); //turn LED on
    timing = false; //no need to check again
  }
}

great!!!.....thanks uncle!!

I try your smartest code uncle,but my LED keeps ON and doesnt turn off. :slightly_smiling_face:

The built in LED should start off and remain off for 5 seconds then turn on and stay on

Is that what it is doing ?

Your topic title says

Turn on LED indicator when arduino uno ON for 5 second

In post #1 you say

One is the opposite of the other
Which do you want ?

Change the values in the digitalWrite() code lines to change what the code does

The sketch by @UKHeliBob from reply #11 in Wokwi with the led on for 5 seconds, then off:

ya,,,im sory..youre right..hahaha...thanks !!! :sweat_smile: :sweat_smile: :sweat_smile:

So which was it you wanted ?

its good,i have new knowladge to make simple code,and of course i need all...actually.Alhamdullillah!!!

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