I need to ON one random led at a time

That is how you learn only to ask.
If you understood what you showed, you would not need to ask.

I am done with you.

I will try. Pseudocode:

clear all LEDs
repeat
{
  set a random LED
  delay
  clear the same LED
}

Simple, isn't it?

1 Like

@anon57585045
delay is blocking the code. that's why I did not used it.

I used delay only to keep the example simple. It is not what I am showing at all. I am showing how to efficiently switch the LEDs.

My hope was that you would apply the same logic to a millis() version. I am not going to do all the work for you.

It would look like:

clear all LEDs
repeat
{
  if millis timer is expired
  {
    reset millis timer
    clear the last LED
    set a new random LED
  }
}
1 Like

@GoForSmoke
if u don't want to reply do not reply it but i am having doubt so i asked it.

If I had understood clearly I would have not asked it.

The problem is, you're not asking only for clarification of a question. You are explicitly asking for a complete solution (full code).

if i want full solution (full code) then the full code was not required in the question as i had mentioned in it.
and by the way i had solved the query and i had posted it about see it clearly.
and my doubt was to solve the simple query not the full code. I had already made the full code but this solution was simple so asked.

I need only this quoted thing is clearly.

Okay, if it's not important to you, that's perfectly okay. The pseudocode I posted does explain what GoForSmoke said, but if you don't care, fine, enjoy building and coding.

I am not saying like "it is not important for me. "it is totatly important for me. I want to understand clearly that's why i asking. if i hurt your feelings. than I am sorry.

My feelings aren't hurt. If you don't understand, and want to understand, then help us by explaining what part(s) you don't understand.

What people are saying is, you don't need to turn off LEDs that are already off. Do you at least understand that part?

my doubt is only in this three line. where I put this line ?

and also doubt for this line what he want to say i cannot understand.

If you understand this both things then i request you to help me to understand. please.

That is completely the wrong question "where do I put this line". That is a cut and paste mentality. Try to understand what it means, what it does, then it will be stupidly obvious where to put it. Actually, you can not just place it in your existing program, you have to re-structure it to fit.

My advice is to forget the second question, it's not as important as the first.

but i want to know what he want to ask ?

Forget it. Concentrate on the first question. It's all you need. Go back to reply #24. Does that make any sense to you? Read carefully.

@aarg

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

int randNum;
static byte onPin;

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[onPin], LOW );
    onPin = pinNum;
    digitalWrite(led_pin[onPin], HIGH);
  }
}

is it okay? which i have write ? please check and let me know.

Don't you have hardware or a simulation to test it?

I have test it and working fine. but just want to verify from you. that's why i have posted. is it okay ?

Well, it works. So "okay" from what perspective?

Code quality?
Understanding?

from both perspective

then (A) why is 'onPin' declared as a static global variable?

and (B) what don't you understand?