I need to ON one random led at a time

I have tried this below code for ON a random LED at a time but in this below code on the basis of random number but all LEDs are ON but no one is OFF. How can I OFF the LED ?

const int led_pin[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const long Interval = 1000;
unsigned long previousMillis = 0;

int randNum;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i <= 8; i++)
  {
    pinMode(led_pin[i], OUTPUT);
  }
}

void loop()
{

  randNum = random(9);
  unsigned long currentMillis = millis();
  led_on(currentMillis, randNum);
}

void led_on(unsigned long currentMillis, int pinNum)
{
  if (currentMillis - previousMillis >= Interval)
  {
    previousMillis = currentMillis;
    digitalWrite(led_pin[pinNum], HIGH);
    Serial.println(pinNum);
  }
}

Please anyone help.

Do something similar for OFF

. . .
digitalWrite(led_pin[pinNum], LOW);
. . .

Like what ?
I cannot understand how can I do it ?

void led_on(unsigned long currentMillis, int pinNum)
{
  if (currentMillis - previousMillis >= Interval)
  {
    previousMillis = currentMillis;
    digitalWrite(led_pin[pinNum], HIGH);

    digitalWrite(random(2,11), LOW);

    Serial.println(pinNum);
  }
}

I'd start with something like this

const int led_pin[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const long Interval = 1000;
unsigned long previousMillis = 0;

int randNum;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i <= 8; i++)
  {
    pinMode(led_pin[i], OUTPUT);
  }
}

void loop()
{

  randNum = random(9);
  unsigned long currentMillis = millis();
  led_on(currentMillis, randNum);
}

void led_on(unsigned long currentMillis, int pinNum)
{
  if (currentMillis - previousMillis >= Interval)
  {
    previousMillis = currentMillis;
    digitalWrite(led_pin[pinNum], HIGH);
    Serial.println(pinNum);
  }
  else {
    digitalWrite(led_pin[pinNum], LOW);
  }
}

See if this does the trick the way you want

void led_doingTheOffThing()
{

        for ( int i = 0; i <9; i++ )
        {
          digitialWrite(led_pin([i], LOW);
        }


}

or something like that

um...

Is this homework?

@anon57585045
Have you any solution ?

You have been give solutions.

Can I take that as a "yes" answer?

Not work perfectly. again its off the random number led.

If you put all the suggestions together, you should be able to figure out how to change things to work as needed.

What are you still having trouble figuring out ?

Help. Did you write the code you presented or did you copy it?

How much of the code that you presented do you know how it works?

Here is the solution for my problem. I have resolved it.

const int led_pin[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const long Interval = 1000;
unsigned long previousMillis = 0;

int randNum;

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i <= 8; i++)
  {
    pinMode(led_pin[i], OUTPUT);
  }
}

void loop()
{

  randNum = random(9);
  unsigned long currentMillis = millis();
  led_on(currentMillis, randNum);
}

void led_on(unsigned long currentMillis, int pinNum)
{
  if (currentMillis - previousMillis >= Interval)
  {
    previousMillis = currentMillis;
    for (int i = 0; i <= 8; i++)
    {
      if (pinNum == i)
      {
        digitalWrite(led_pin[i], HIGH);
      }
      else
      {
        digitalWrite(led_pin[i], LOW);
      }
    }
  }
}

I have write on my own.

congratulations !

It reads like it should work and Arduino is so fast that the set high or low for-loop may run in a few microseconds. Using direct port methods, about 1 microsecond.

Suppose that void led_on() kept a saved value of which pin is ON

static byte on_pin; // pin turned ON last

then when time runs out,
digitalWrite( on_pin, LOW );
on_pin = pinNum; // the next pin to turn ON
digitalWrite( on_pin, HIGH );

Since on;y 1 led is on, only 1 needs turned off
The next led gets turned on, 2 digitalWrite( Arduino safe ) instead of 8.

Do you want to work the pins through the pin registers for speed you don't need?

1 Like

@GoForSmoke
I cannot understand properly. I mean I understand what you want say but that example I cannot understand properly. can you please write the perfect example ? where I need to change that think ? can you please help. or if you don't mind then can you please give whole code or function.

@GoForSmoke

What you want to say in this line I cannot understand it. sorry can you please explain ?