hi ,
i´am trying to do a homework about the basics of arduino and i have to make a led (turn on for 500 ms and off for 500 ms ) a specific amount of time which has to be randomly assigned from 0 to 10. and then make the led turn off for 2 sec before starting the process again . i know iàm maybe on the right track but my code is missing a lot , so it will be nice if someone could help me completing it .
You starting point should be the “blink without delay“ sketch. It is one of the first example sketches in the Arduino IDE.
delay() cannot be used in your solution.
Your thread title is a bit misleading because you want to turn a Led on and off, not the Arduino.
Hello,
my advice to do this task:
You need a timer function providing a timer for the blinking task and one timer for the randomized repeat of the blinking.
I think you guys are over-complicating the solution. The OP doesn't need to do anything else, so why not use delay()? After mastering this, it would be good to learn about Blink Without Delay, etc...
void setup()
{
randomSeed(analogRead(0));
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
int rn = random(0, 11); // random number from 0-10
for ( int x = 0; x < rn; x++ )
{
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
// led is already off, no need to turn it off twice
//digitalWrite(LED_BUILTIN, LOW);
delay(2000);
}