random number generator hanging my sketch - Christmas light project [solved]

I'm not a good programmer but I can copy and paste, and I am learning.

I am programming (2) Arduino Nano's to run some Christmas lights this year. Each nano runs an 8 channel ss relay board and I have Christmas lights wired to the relay boards. Or I will anyway, right now they are on my desk running LED's so I can create and edit my patterns.
One is designated as 8 and the other is designated as 6.
The 8 controller will control lights on 4 poles, 2 channels per pole, colors and white.
The 6 controller will control 2 sets of 3 lights, and a set of 2 lights. (driveway lights)
When the 8 controller gets to the end of the sketch and starts over, it will reset the 6 controller so they will always be in sync.

My problem is that when the 8 controller gets to one of the random number generators in the sketch, it will hang there and just do random numbers forever until I cycle power. I have tried everything I can think of and it's still happening. Never really happens in the same place in the sketch, it seems to be happening at random.
my random number generator problem happens randomly. lol
Also, when this happens, I have the sketch turning on LED13 for a split second so I can see when the program restarts. LED13 comes on and stays on during a hang so I think the reset pin (12) is being held low also. I don't understand how this is even possible.
I even added
digitalWrite (led, LOW);
digitalWrite (sync, HIGH);
to my alloff subroutine and it still happens.

One last clue or question... I am powering these 2 nano's with 5.15 vdc. Is that too low? Could this be a power problem? Should I bump it up to 6 or 7vdc?

You should be able to figure out my wiring from this sketch, it's pretty simple. I ran a wire from pin 12 on the 8 ch controller to RST on the 6 ch controller.

My 2 sketches make this post too big and it won't post.
I'll reply to myself with the 8 sketch, then again with the 6 sketch.

Here is the 8 ch code:

int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay4 = 5;
int relay5 = 6;
int relay6 = 7;
int relay7 = 8;
int relay8 = 9;
int sync = 12;
int led = 13;

const int timef = 650;
const int timec = 450;
const int led_delay = 200;
const int led_count = 8; 
const int led_pins[led_count] = {2, 3, 4, 5, 6, 7, 8, 9}; 

void alloff() {
  for (int x = 1; x < 10; x++) {
    digitalWrite(x, HIGH);
    digitalWrite (led, LOW);
    digitalWrite (sync, HIGH);
  }
}

void randoms() {
  for (int o = 0; o <= 30; o++) {
    int r = random(1, 4);
    int w = random(4, 9);
    digitalWrite(led_pins[r], LOW);
    digitalWrite(led_pins[w], LOW);
    delay(led_delay);
    digitalWrite(led_pins[r], HIGH);
    digitalWrite(led_pins[w], HIGH);
   }
}

void me() {
  digitalWrite(relay1, LOW);
  delay(timef / 2);
  digitalWrite(relay5, LOW);
  delay(timef / 2);
  alloff();
  digitalWrite(relay2, LOW);
  delay(timef / 2);
  digitalWrite(relay6, LOW);
  delay(timef / 2);
  alloff();
  digitalWrite(relay3, LOW);
  delay(timef / 2);
  digitalWrite(relay7, LOW);
  delay(timef / 2);
  alloff();
  digitalWrite(relay4, LOW);
  delay(timef / 2);
  digitalWrite(relay8, LOW);
  delay(timef / 2);
  alloff();
}

void me2() {
  digitalWrite(relay4, LOW);
  delay(timef / 2);
  digitalWrite(relay8, LOW);
  delay(timef / 2);
  alloff();
  digitalWrite(relay3, LOW);
  delay(timef / 2);
  digitalWrite(relay7, LOW);
  delay(timef / 2);
  alloff();
  digitalWrite(relay2, LOW);
  delay(timef / 2);
  digitalWrite(relay6, LOW);
  delay(timef / 2);
  alloff();
  digitalWrite(relay1, LOW);
  delay(timef / 2);
  digitalWrite(relay5, LOW);
  delay(timef / 2);
  alloff();
}

void flashx() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
  digitalWrite(relay2, LOW);
  digitalWrite(relay7, LOW);
  delay(timef);
  digitalWrite(relay3, LOW);
  digitalWrite(relay6, LOW);
  delay(timef);
  digitalWrite(relay4, LOW);
  digitalWrite(relay5, LOW);
  delay(timef);
  digitalWrite(relay1, HIGH);
  digitalWrite(relay8, HIGH);
  delay(timef);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay7, HIGH);
  delay(timef);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay6, HIGH);
  delay(timef);
  digitalWrite(relay4, HIGH);
  digitalWrite(relay5, HIGH);
  delay(timef);
  alloff();
}

void countrw() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay7, LOW);
  delay(timef);
  alloff();
  digitalWrite(relay2, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
  alloff();
  digitalWrite(relay3, LOW);
  digitalWrite(relay5, LOW);
  delay(timef);
  alloff();
  digitalWrite(relay4, LOW);
  digitalWrite(relay6, LOW);
  delay(timef);
  alloff();
  digitalWrite(relay3, LOW);
  digitalWrite(relay5, LOW);
  delay(timef);
  alloff();
  digitalWrite(relay2, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
  alloff();
  digitalWrite(relay1, LOW);
  digitalWrite(relay7, LOW);
  delay(timef);
  alloff();
}

void flash12() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  delay(timef);
}

void flash13() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay3, LOW);
  delay(timef);
}

void flash14() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay4, LOW);
  delay(timef);
}

void flash23() {
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  delay(timef);
}
void flash24() {
  digitalWrite(relay2, LOW);
  digitalWrite(relay4, LOW);
  delay(timef);
}

void flash34() {
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  delay(timef);
}

void flash56() {
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);
  delay(timef);
}

void flash57() {
  digitalWrite(relay5, LOW);
  digitalWrite(relay7, LOW);
  delay(timef);
}

void flash58() {
  digitalWrite(relay5, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
}

void flash67() {
  digitalWrite(relay6, LOW);
  digitalWrite(relay7, LOW);
  delay(timef);
}

void flash68() {
  digitalWrite(relay6, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
}

void flash78() {
  digitalWrite(relay7, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
}

void flashr() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  delay(timef);
}

void flashw() {
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);
  digitalWrite(relay7, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
}

void flashr2() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  delay(timef / 2);
}

void flashw2() {
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);
  digitalWrite(relay7, LOW);
  digitalWrite(relay8, LOW);
  delay(timef / 2);
}

void countupr() {
  for (int x = 1; x < 5; x++) {
    digitalWrite(x, LOW);
    delay(timec);
    digitalWrite(x, HIGH);
  }
}

void countdownr() {
  for (int x = 5; x > 1; x = x - 1) {
    digitalWrite(x, LOW);
    delay(timec);
    digitalWrite(x, HIGH);
  }
}

void countupw() {
  for (int x = 6; x < 9; x++) {
    digitalWrite(x, LOW);
    delay(timec);
    digitalWrite(x, HIGH);
  }
}

void countdownw() {
  for (int x = 9; x > 5; x = x - 1) {
    digitalWrite(x, LOW);
    delay(timec);
    digitalWrite(x, HIGH);
  }
}

void rst() {
  digitalWrite (led, HIGH);
  digitalWrite (sync, LOW);
  delay (timec);
  digitalWrite (led, LOW);
  digitalWrite (sync, HIGH);
  delay (timec);
}

void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(sync, OUTPUT);
}

void loop() {

  rst();

  alloff();
  me();
  alloff();
  randoms();
  alloff();
  me2();
  alloff();
  me();
  alloff();
  me2();
  alloff();
  randoms();
  alloff();
  countupr();
  alloff();
  countdownr();
  alloff();
  countupw();
  alloff();
  countdownw();
  alloff();

  flash13();
  flash68();
  alloff();
  flash24();
  flash57();
  alloff();
  flash13();
  flash68();
  alloff();
  flash24();
  flash57();
  alloff();

  randoms();
  alloff();

  countrw();
  alloff();
  flashx();
  alloff();
  countrw();
  alloff();
  flashx();
  alloff();

  flash13();
  alloff();
  flash24();
  alloff();
  flash13();
  alloff();
  flash24();
  alloff();
  flash14();
  alloff();
  flash23();
  alloff();
  flash14();
  alloff();
  flash23();
  alloff();

  flash57();
  alloff();
  flash68();
  alloff();
  flash57();
  alloff();
  flash68();
  alloff();
  flash58();
  alloff();
  flash67();
  alloff();
  flash58();
  alloff();
  flash67();
  alloff();

  flash13();
  flash24();
  alloff();
  flash57();
  flash68();
  alloff();
  flash13();
  flash24();
  alloff();
  flash57();
  flash68();
  alloff();

  flashr();
  alloff();
  flashw();
  alloff();
  flashr();
  alloff();
  flashw();
  alloff();

  flashr2();
  alloff();
  flashw2();
  alloff();
  flashr2();
  alloff();
  flashw2();
  alloff();
  flashr2();
  alloff();
  flashw2();
  alloff();
  flashr2();
  alloff();
  flashw2();
  alloff();

}

...and 5 minutes later, here is the 6 ch code:

int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay4 = 5;
int relay5 = 6;
int relay6 = 7;
int relay7 = 8;
int relay8 = 9;
int led = 13;

const int timef = 650;
const int timec = 450;
const int led_delay = 200;
const int led_count = 8; 
const int led_pins[led_count] = {2, 3, 4, 5, 6, 7, 8, 9}; 

void alloff() {
  for (int x = 1; x < 10; x++) {
    digitalWrite(x, HIGH);
    digitalWrite (led, LOW);
  }
}

void randoms() {
  for (int o = 0; o <= 20; o++) {
    int rl = random(1, 4);
    int rr = random(4, 7);
    int rw = random(7, 9);
    digitalWrite(led_pins[rl], LOW);
    digitalWrite(led_pins[rr], LOW);
    digitalWrite(led_pins[rw], LOW);
    delay(led_delay);
    digitalWrite(led_pins[rl], HIGH);
    digitalWrite(led_pins[rr], HIGH);
    digitalWrite(led_pins[rw], HIGH);
  }
}

void flashx() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay6, LOW);
  delay(timef);
  digitalWrite(relay2, LOW);
  digitalWrite(relay5, LOW);
  delay(timef);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  delay(timef);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
  delay(timef);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay5, HIGH);
  delay(timef);
  digitalWrite(relay1, HIGH);
  digitalWrite(relay6, HIGH);
  delay(timef);
  alloff();
}

void flash78() {
  digitalWrite(relay7, LOW);
  digitalWrite(relay8, LOW);
  delay(timec);
}

void flash7() {
  digitalWrite(relay7, LOW);
  delay(timec);
}

void flash8() {
  digitalWrite(relay8, LOW);
  delay(timec);
}

void flash12() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  delay(timef);
}

void flash13() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay3, LOW);
  delay(timef);
}

void flash23() {
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  delay(timef);
}

void flash45() {
  digitalWrite(relay4, LOW);
  digitalWrite(relay5, LOW);
  delay(timef);
}
void flash56() {
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);
  delay(timef);
}

void flash46() {
  digitalWrite(relay4, LOW);
  digitalWrite(relay6, LOW);
  delay(timef);
}

void flash14() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay4, LOW);
  delay(timef);
}

void flash25() {
  digitalWrite(relay2, LOW);
  digitalWrite(relay5, LOW);
  delay(timef);
}

void flash36() {
  digitalWrite(relay3, LOW);
  digitalWrite(relay6, LOW);
  delay(timef);
}

void flashl() {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay8, LOW);
  delay(timef);
}

void flashr() {
  digitalWrite(relay4, LOW);
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);
  digitalWrite(relay7, LOW);
  delay(timef);
}

void cuonoff() {
  for (int x = 1; x < 8; x++) {
    digitalWrite(x, LOW);
    delay(timec);
    digitalWrite(x, HIGH);
  }
}
void custayon() {
  for (int x = 1; x < 8; x++) {
    digitalWrite(x, LOW);
    delay(timec);
  }
}

void cuturnoff() {
  for (int x = 1; x < 8; x++) {
    digitalWrite(x, HIGH);
    delay(timec);
  }
}

void rst() {
  digitalWrite (led, HIGH);
  delay (timec);
  digitalWrite (led, LOW);
  delay (timec);
}

void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
  pinMode (led, OUTPUT);
}

void loop() {

  rst();

  alloff();

  randoms();
  alloff();

  flash14();
  alloff();
  flash25();
  alloff();
  flash36();
  alloff();
  flash78();
  alloff();
  flash14();
  flash25();
  flash36();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash78();
  alloff();

  randoms();
  alloff();

  flash14();
  alloff();
  flash25();
  alloff();
  flash36();
  alloff();
  flash78();
  alloff();
  flash14();
  flash25();
  flash36();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash78();
  alloff();

  randoms();
  alloff();

  flash14();
  alloff();
  flash25();
  alloff();
  flash36();
  alloff();
  flash78();
  alloff();
  flash14();
  flash25();
  flash36();
  flash7();
  alloff();
  flash8();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();

  flashx();
  alloff();

  randoms();
  alloff();

  flashl();
  alloff();
  flashr();
  alloff();
  flashl();
  alloff();
  flashr();
  alloff();
  flashl();
  alloff();
  flashr();
  alloff();

  flash78();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash78();
  alloff();

  cuonoff();
  alloff();
  custayon();
  cuturnoff();
  flash78();
  alloff();
  cuonoff();
  flash78();
  alloff();
  custayon();
  cuturnoff();
  alloff();

  flash7();
  alloff();
  flash8();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();

  flash14();
  alloff();
  flash25();
  alloff();
  flash36();
  alloff();
  flash78();
  alloff();
  flash14();
  flash25();
  flash36();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash7();
  alloff();
  flash8();
  alloff();
  flash78();
  alloff();

  randoms();
  randoms();

}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

sdtag:

const int led_count = 8; 

const int led_pins[led_count] = {2, 3, 4, 5, 6, 7, 8, 9};

sdtag:

    int rw = random(7, 9);

digitalWrite(led_pins[rl], LOW);
    digitalWrite(led_pins[rr], LOW);
    digitalWrite(led_pins[rw], LOW);

Keep in mind arrays are zero indexed. led_pins[8] is the 9th element of an array sized to 8 elements. Accessing outside the bounds of an array results in undefined behavior.

sdtag:
One last clue or question... I am powering these 2 nano's with 5.15 vdc. Is that too low? Could this be a power problem? Should I bump it up to 6 or 7vdc?

Where is the 5.15 VDC connected?

Sorry about my lack of formatting.

5.15 goes to VIN.

sdtag:
Sorry about my lack of formatting.

You can fix your posts and put [code] before your code and [/code] after your code.

sdtag:
5.15 goes to VIN.

From the "Documentation" tab of https://store.arduino.cc/arduino-nano:

Power

The Arduino Nano can be powered via the Mini-B USB connection, 6-20V unregulated external power supply (pin 30), or 5V regulated external power supply (pin 27). The power source is automatically selected to the highest voltage source.

What they mean by "pin 30" is the pin marked VIN on your Nano. What they mean by "pin 27" is the pin marked 5V. The VIN pin is connected to a voltage regulator, which causes a voltage drop. For this reason, if you're going to power the Nano via the VIN pin you need to supply at least 6 V. The 5V pin has no voltage regulator. That means there is no voltage drop and the board will be powered at the same voltage you are supplying. This also means that if you supply more voltage to the 5V pin than the Nano is rated for, you will destroy it. You also need to be aware that the voltage regulator on the Nano can't supply much current. If you connect a power supply to the VIN pin on the Nano, and try to power multiple relays and LEDs from the Nano board, that will probably exceed the capabilities of the voltage regulator.

I always thought that the 5v pin was out, not in.
I'll try 6vdc on VIN and then can I use 5V pin27 to power my relay board?
Or should I use another buck converter to power the relay board?

After re-reading your answer I think it's best to use another buck converter.

Nice - my leds are brighter now. :slight_smile:

sdtag:
I always thought that the 5v pin was out, not in.
I'll try 6vdc on VIN and then can I use 5V pin27 to power my relay board?
Or should I use another buck converter to power the relay board?

Nice - my leds are brighter now. :slight_smile:

It's both in and out, it's just a connection to the power rail of the board.

You can use an external regulator to prep the power for your LEDs, but be sure to keep the grounds of the power supplies and LEDs and the Arduino all connected together.

so this is a power thing?
My code looks ok to you guys?

I think I want to remove

digitalWrite (led, LOW);
digitalWrite (sync, HIGH);

from my alloff routine. I added them trying to fix this problem.

Been running on 6vdc for a few minutes now - no hangs.

Thank you everybody! I knew I came to the right place!

If anybody cares I'll post a video of my Christmas lights on youtube and I'll come back here and add a link.

I have these 2 nano's running 8ch relay boards and an RGB controller going to music and a ws2812 controller chasing leds. Plus all the "normal" stuff.

Watching them run on my desk, it just happened again. My 8ch nano is stuck in a random pattern and LED13 is on. The 6ch nano is still running so pin 12 on the 8ch must still be high.
Maybe I should make the 6ch reset the 8ch???

Did you fix the bug that @pert pointed out in reply #3 ?

thanks for the hint
but that code is from controller 6 and it's not hanging.
Controller 8 is hanging during a random number pattern

sdtag:
thanks for the hint
but that code is from controller 6 and it's not hanging.
Controller 8 is hanging during a random number pattern

Memory overwrite codes are sneaky. You may not see a problem now, it'll manefest right after you turn the display on and leave for the night.

Your coding is basically sound, but it's very difficult to read and debug. This happens when the same sequence is repeated over and over. Plus you have a variable named o. Makes it extra hard to read. The only one worse is l.

I don't really understand arrays all that well. (Obviously) I copied and pasted the original code which was 1 random number from 2 to 8. My lighting layout is 2 groups of 3 lighted objects and one group of 2 lighted objects. When 1 random number was picked, 1 lighted object out of 8 would come on. It wasn't enough. I am trying to make it generate random numbers from 2 to 4, 5 to 7, and 8 to 9 at the same time. I want at least one pin on in each group on during the random number thing. It was working until recently and I've been going nuts trying to figure out what I did. Or why it hangs now. I have 2 running in my box in the garage that run fine, they don't hang at all. It's these 2 on my desk that I am trying to get ready for back ups. 2 nanos, I call them 6 and 8. The 8 will reset the 6 every cycle.

I don't know why I used o as a variable. it is hard to read. I copied and pasted and didn't think about it.

And like I said somewhere up above, this code is from the 6 controller and it doesn't hang. It's the 8 controller and it's code there is only 2 groups, not 3. 1 to 4 and 4 to 9.

So, bottom line, what I am trying to accomplish is to have 2 groups of 4 have 1 pin random on at the same time on the 8 controller, and 2 groups of 3 and 1 group of 2 on at the same time on the 6 controller.

I am trying to make it generate random numbers from 2 to 4, 5 to 7, and 8 to 9 at the same time.

Your array is like a set of drawers numbered 0..7 (not 2..9, that's the content). To get your pin numbers, you first open one of the drawers 0 to 2 and read the number that is stored in that drawer, next open one of the drawers 3 to 5 and read the number and lastly open one of te drawers 7 to 8 and read the number.

So your random numbers must be:
0..2 to select one of the pins 2 or 3 or 4 from the array
3..5 to select one of the pins 5 or 6 or 7 from the array
6..7 to select pin 8 or 9 from the array

    int rl = random(0, 3);
    int rr = random(3, 6);
    int rw = random(6, 8);

I think that was it thank you.

I won't post my whole code again but on the 6 ch it is like you have it
on the 8 ch it is 0, 4 and 4, 8.

running for a few minutes now. no lockups.
I feel better now that it won't lock up as soon as I leave
thank you
thank you all