Create a scanner with random pauses

im working on a project that I want 20 LED's to run one way and run a few random times also a random led then pause with a lit led for a few seconds random also and then continue running. This is what I have so far in my code. I have it running a sequence no problem but want the random times the sequence runs. Then pause on a random lit led for a random of seconds. I'm learning everyday with this and any help would be very much appreciated

const int delay_ms = 40;

void setup() {
int i;

for (i = 0; i <= 19; i++) {
pinMode(i, OUTPUT); // set pins as outputs
digitalWrite(i, LOW); // switch the output pins off

int pin_num;

void loop() {

for (pin_num = 0; pin_num <= 19; pin_num++) {
digitalWrite(pin_num, HIGH);
digitalWrite(pin_num, LOW);

With an Arduino Uno ?

How are the leds connected ? Do you use resistors ?
If you open a serial port with Serial.begin(9600); then you can use the serial monitor to see (debug) messages. In that case you can not use pin 0 and 1.

When you show your sketch on this forum, please put the sketch between code-tags. The </> button is for code-tags.

There are many good solutions for this.
I prefer to keep the loop() running as often as possible. Let's move forward one led per loop() iteration.
The code is easier when the led where it is stopped is already known before the leds start running.
Suppose the leds will move one led forward about 30 to 200 times.
Do you know the % operator, that is the remainder of a division: % - Arduino Reference
Suppose the number that the leds move forward is the base of the sketch and is called 'counter'. Then the led-pin is:

ledPin = counter % 20;  // remainder will be 0...19

This is not a working sketch. It show how the structure is. It is just one of the many solutions.

// global variables
int ledCount;
int counter = 0;

void setup()
  ledCount = random( 30, 200);

void loop()
  int ledPin = counter % 20;   // remainder will be 0...19

  if( counter == ledCount)
    // Reached the final led. Turn it on and wait a random time. Make new ledCount value.
    digitalWrite( ledPin, HIGH);
    int waitingTime = random( 1000, 2000);   // waiting time in milliseconds
    delay( waitingTime);
    digitalWrite( ledPin, LOW);

    ledCount = random( 30, 200);    // new random for number of leds to move forward
    counter = 0;
    digitalWrite( ledPin, HIGH);
    delay( 40);
    digitalWrite( ledPin, LOW);

This is from the Arduino reference:
// print a random number from 0 to 299
randNumber = random(300);
That is all there is to it.
Here is a link for you: random() - Arduino Reference

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.