Go Down

Topic: Want to blink only 1 time? newbie question (Read 5403 times) previous topic - next topic

lxx_28

I,

Today i received my arduino and was looking at the code of the blink led.

I thought I altered the code , to let it blink only one time.

I tested:
{
 digitalWrite(ledPin, HIGH);  
 delay(1000);                  
 digitalWrite(ledPin, LOW);
 delay(1000);
 digitalWrite(ledPin, HIGH);
}

and:
{
 digitalWrite(ledPin, HIGH);   // set the LED on
 delay(1000);                  // wait for a second
 digitalWrite(ledPin, LOW);
 delay(1000);
 digitalWrite(ledPin, LOW);
}

But it doesn't stop. Where is it going wrong?

I hope somebody can help me.

AWOL

"loop()" will always repeat. If you want it to execute just once, put it in "setup()"

lxx_28

Yep that works :),

Thx alot.

Now it also work when i use    "for (int x=0; x<10; x++)"
to let it blink 10 times :).
I was always putting it in loop()......




ReCreate

Put this in the setup
Code: [Select]
digitalWrite(ledPin, HIGH);  
delay(1000);
digitalWrite(ledPin, LOW);  

segu

Altered classical first example see last line in LOOP(), I think a more ARDUINO way ... I hope ;)
[code]
int ledPin =  13;    // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup()   {                
 // initialize the digital pin as an output:
 pinMode(ledPin, OUTPUT);    
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                    
{
 digitalWrite(ledPin, HIGH);   // set the LED on
 delay(1000);                  // wait for a second
 digitalWrite(ledPin, LOW);    // set the LED off
 while(1); //delay(1000);                  // wait for a second

RoyK

#5
Sep 21, 2009, 02:17 am Last Edit: Sep 21, 2009, 02:24 am by RoyK Reason: 1
Either way is ok -- if you put a closing brace } for loop() in the example just above. The important thing is that you learned quite a bit just with that simple example.

This way of doing it might make some sense in certain situations also. There are usually several ways to do most anything. Some might be more appropriate than others in some situations but the important thing is that the job gets done and you understand how.

Code: [Select]
int ledPin =  13;    // LED connected to digital pin 13
int alreadyBlinked = 0;
// The setup() method runs once, when the sketch starts

void setup()   {                
// initialize the digital pin as an output:
pinMode(ledPin, 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(1000);                  // wait for a second
            digitalWrite(ledPin, LOW);    // set the LED off
            alreadyBlinked = 1;
      }
}

Go Up