# Hello everyone. Whos the master of LED blinking here?

I think I will start out by introducing myself. Im jonnie, Im 15, just got the arduino uno and a few things with it that I have been saving for. Now I am ready to start experimenting so I look forward to hopefully getting help and when I get better helping others in this forum. I have been playing with the blink program. Im taking computer programming in school, which is really basic stuff but Im trying to get the feel of the programming code.

I would like to try to find a way to make one LED blink one time, two times or three times. Is there a way that I can have the computer choose which one to do on its own. Like one time do one blink, then another time do three blinks then another time do two blinks and just keep doing whatever it wants on its own without me having to tell it what to do? Thanks to whoever answers this.

I would like to try to find a way to make one LED blink one time, two times or three times.

And then what?

Is there a way that I can have the computer choose which one to do on its own.

random() - http://arduino.cc/en/Reference/Random

Right now I am trying to understand how to do things with one LED first then I can get better.
If I use random () how do I assign the three options to choose from?
I played around using the random statement to light LEDS at 1,2,3. I learned something already but how can I tell arduino,
#1 option = blink 1 time

I played around using the random statement to light LEDS at 1,2,3. I learned something already but how can I tell arduino,
#1 option = blink 1 time

What does "blink an LED" mean to you?

To me, it means turn it on, wait a while, then turn it off, wait a while, and do the same steps again.

You can use a for loop to iterate some number of times. How many times? Well, the number of times that random() picked.

But, then what? loop() runs again, picks another random number, and starts the LED going on and off again.

How will you know how many times the LED "blinked" for each random number selection?

You could have a switch. When the switch becomes pressed (see the state change detection example), blink the LED the appropriate number of times, and then wait for the next switch press.

All I want to do is make the arduino choose between blink 1 2 3 times.
Thats all for now.

You can use a switch http://arduino.cc/en/Reference/SwitchCase, basically tell random to produce a number between 0 and 2 (or 1 and 3 which ever you prefer) and use the number generated to select between 3 cases.

For Example

``````switch(random(1,3)){
case 1:
//do something for case 1
break;
case 2:
//do something for case 2
break;
case 3:
//do something for case 3
break;
}
``````

@jortband
The random function returns a value between the first argument and one less than the second argument, so random(1,3) will return either 1 or 2 each time it is called. It will never return 3.

All I want to do is make the arduino choose between blink 1 2 3 times.
Thats all for now.

Then, put all the code in setup() and leave the body of loop() empty.

I get the impression that you don't understand that loop() is called over and over again.

OK Here’s my effort
It will blink a random number of times (between 1 and 8) for a period (decided at random) between 50 and 400 microseconds) every (random period decided between 1.5 and double the blink duration)

``````int blinkPin=13;
unsigned long duration;
unsigned long period;

void setup()
{
Serial.begin(57600);
}

void loop()
{
{
period=random(duration*1.5,duration*2);//how often
}
//delay(100);
}

{
unsigned long frame=millis()%period;
bool newState=(frame<duration);
}
}
``````
``````if (!blinksToGo)
``````

Get in the habit of writing readable code. This construct is fine if blinksToGo is a boolean. It is NOT.

``````if (blinksToGo == 0)
``````

Ints should be compared to ints.

Get in the habit of properly indenting your code or using Tools + Auto Format before posting code.

``````  {digitalWrite(blinkPin,newState);
``````

NOTHING follows the { on the same line.

another way, perhaps more suitable for the newcomer

``````byte ledPin = 13;

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

void loop()
{
delay(2000);
}

{
for (int i = 0; i < repetitions; i++)
{
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}
}
``````

PaulS:

``````if (!blinksToGo)
``````

Get in the habit of writing readable code. This construct is fine if blinksToGo is a boolean. It is NOT.

``````if (blinksToGo == 0)
``````

Ints should be compared to ints.

Get in the habit of properly indenting your code or using Tools + Auto Format before posting code.

``````  {digitalWrite(blinkPin,newState);
``````

NOTHING follows the { on the same line.

! =false ==0 ==LOW In this situation are interchangeable.

Blinks to go is an int. How would you like me to express 0 as an int?

As for how I lay my code out, it's served me well enough for over 40 years. I doubt I'll change now!