Blink led in void setup()

Hi,
I would like to make LED blink when the device (temperature logger) is turned on, so only one time. Is it possibe to do it inside "void setup()"?

I tried this, but no luck..

int led = 4;

void setup() {                
pinMode(led, OUTPUT);     

 digitalWrite(led, HIGH);   
  delay(1000);             
  digitalWrite(led, LOW);  
  delay(1000); 
}

void loop() {
}

Show your cct.

Then try a for loop say 10 times .

Yes, just like that.
Anything you want to occur just one time can do that.

Or you could use a for:loop and have it blink 3 times before it loop() starts.

Or have a switch be read, and not advance into loop() until a button was pressed or similar.

Lots of things can be done before advancing into loop().

did you do a cross checking with putting the same code into loop()? Just to see if the LED is blinking there?
This is to ensure that the hardware-wiring is working

Thank you.
Got it work using the alreadyBlinked code:

int ledPin = 4;
int alreadyBlinked = 0;

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


void loop () {
     if(alreadyBlinked == 0)
     {
 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(2000);                  // waits a period of time
 digitalWrite(ledPin, LOW);    // sets the LED off
            alreadyBlinked = 1;
      }
}

Which makes us all wonder what was actually wrong with the way you tried it first, which should have done the same thing.

What did your original attempt do? What else besides the code must you have changed?

“The” alreadyBlinked code (!) is a kludge. While effective you shouldn’t go off thinking that setup() is inadequate to the task of performing some things one time at the beginning… that’s what and all it is meant for.

a7

Hi Fred,

I tested the blink-once in setup-code code on an Arduino-Uno with the onboard-LED (= pin 13)
did exactly what is expected. LED goes on for one second LED goes off

You are a bit lazy about code-formatting. If you press Ctrl-T in the Arduino-code.editor the code gets formatted for you.

In such a small code it does not matter if you have each line indented a different number of spaces. In bigger code correct indentnion will help to find missing or overcounted curly brackets "{}"

best regards Stefan

1 Like