0

i want the led to light at the beginning and stays like this for a while then turns off, so i wrote:

int led=13;
void setup() {
  pinMode(led,OUTPUT);    
}
void loop() {
    digitalWrite(led,HIGH);
    delay (5000);
    digitalWrite (led,LOW);
}

as i understand it should start with high means on digitalWrite(led,HIGH)...and stays like this for 5000us delay (5000) then it should turn off with digitalWrite (led,LOW). but why its not working, it starts with off for afew seconds then stays on..what i understand wrong with these simple steps ????

2
  • when you reset the arduino it starts to run the code. ... it runs all the code in void setup() only once, then it runs all the code in void loop() and repeats running the code in void loop() over and over until you reset the arduino, or turn it off. .... if you want the arduino to do something only once then do not put the code in void loop()
    – jsotola
    Commented Dec 30, 2017 at 2:52
  • Thnx, can you please answer my other question here. arduino.stackexchange.com/questions/47858/… Commented Dec 30, 2017 at 21:29

3 Answers 3

1

It immediately loops, from the digital write low to digital write high. Add a second delay after the low, so that it waits before it loops to the top again.

2
  • thnx, can you please explain to me that loop and why it acts like that...as i understand it should end with digitalwrite off... because its the last step at the programm Commented Dec 2, 2017 at 22:34
  • i added the timer after low..but what happens is a flash..it repeat itself...but i want it so start with on for a definte time then it ends with off and stays off Commented Dec 2, 2017 at 22:39
1

The "loop()" will keep repeating itself, turning the led on and off forever. If you want turn the led on just once, put the commands for turn the led on in "setup()" section:


void setup() {
    pinMode(led,OUTPUT);
    digitalWrite(led,HIGH);
    delay (5000);
    digitalWrite (led,LOW);
} 
void loop() {
}

Other method would be add a variable to count how much times the led should light. I copied the following sketch from here


int ledPin =  13;    // LED connected to digital pin 13
int alreadyBlinked = 0;

// The setup() method runs once, when the sketch starts
void setup()   {
pinMode(ledPin, OUTPUT);    // initialize the digital pin as an output:
}

// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()                    
{
     if(alreadyBlinked == 0)
     {
            digitalWrite(ledPin, HIGH);   // set the LED on
            delay(5000);                  // wait for five seconds
            digitalWrite(ledPin, LOW);    // set the LED off
            alreadyBlinked = 1;
      }
}

When the variable "alreadyBlinked" is set to 1, the led will not turn on again.

3
  • thnx , i got it, i just wanted to understand the structure of the program and instructions, now for more understanding of the loop i put once void loop(){ digitalWrite(led,HIGH); digitalWrite(led,LOW);} and once high then low, but why on both cases the led always stays on ??? Commented Dec 3, 2017 at 0:21
  • Did you check the variables names? Is the led correctly connected? One pin of the led should be connected to the arduino output, and the other pin to the ground. In the first option, digitalwrite (led,LOW) is the last command, so, the led should stay off after setup. In the second option, digitalWrite(ledPin, LOW) is issued before alreadyBlinked = 1, and, since then, the led will not turn on again. I'll try this sketch tomorrow.
    – mguima
    Commented Dec 3, 2017 at 0:58
  • yes, i checked..i got it now Commented Dec 4, 2017 at 23:51
-1

That delay does not do anything but wasting 5 seconds...you need to do like:

void loop() {
    digitalWrite(led,HIGH);
    delay (2500);
    digitalWrite (led,LOW);
    delay (2500);
}

The code runs sequentially...in your code after turning off the led it immediately turns it on again and waits 5 seconds...this is so fat that you do not notice it.

3
  • i have applied this but it blinks, it doesnt stop ...i want it to stop after lighting for a few seconds Commented Dec 2, 2017 at 22:49
  • thnx for the explaining that its so fast so i wont notice Commented Dec 3, 2017 at 0:27
  • The loop function as its name suggests will happen over and over again. You need to set a flag. e.g. An integer that counts how many time the loop has been executed. Then an if statement to check and operate accordingly
    – Dumbo
    Commented Dec 3, 2017 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.