Random Relay 6 pins / 3 pins works, 3 does'nt. What's wrong?

Hi all,

Nano + 8x relay

Only the first 3 pins of my sketch works, half the sketch doesnt work.
I dont know what is wrong as it is just the same lines with different numbers..

int ledPin1 = 2;
int ledState1 = HIGH;
unsigned long previousMillis1 = 0;

int ledPin2 = 3;
int ledState2 = HIGH;
unsigned long previousMillis2 = 0;

int ledPin3 = 7; <================= changing this with any digital out is ok
int ledState3 = HIGH;
unsigned long previousMillis3 = 0;

int ledPin4 = 5;
int ledState4 = HIGH;
(...)
=============== The 3 following pins just turn the relays Off once and then nothing else.

  // These variables store the flash pattern
// and the current state of the LED

int randNumber1 = 5;
int randNumber2 = 5;
int randNumber3 = 5;
int randNumber4 = 5;
int randNumber5 = 5;
int randNumber6 = 5;
int randNumber7 = 3;
int randNumber8 = 3;
int randNumber9 = 4;
int randNumber10 = 4;
int randNumber11 = 7;
int randNumber12 = 7;

int ledPin1 =  2;      // the number of the LED pin
int ledState1 = HIGH;             // ledState used to set the LED
unsigned long previousMillis1 = 0;        // will store last time LED was updated

int ledPin2 =  3;
int ledState2 = HIGH;
unsigned long previousMillis2 = 0;

int ledPin3 =  4;
int ledState3 = HIGH;
unsigned long previousMillis3 = 0;

int ledPin4 =  5;
int ledState4 = HIGH;
unsigned long previousMillis4 = 0;

int ledPin5 =  6;
int ledState5 = HIGH;
unsigned long previousMillis5 = 0;

int ledPin6 =  7;
int ledState6 = HIGH;
unsigned long previousMillis6 = 0;

int t1 = 500;
int t2 = 1000;
int t3 = 3000;
int t4 = 20500;



void setup()
{
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  
}

void loop()
{
 

  randNumber1 = random(t1,t2);
  Serial.println(randNumber1);
  randNumber2 = random(t3,t4);
  Serial.println(randNumber2);
  
  randNumber3 = random(t1,t2);
  Serial.println(randNumber3);
  randNumber4 = random(t3,t4);
  Serial.println(randNumber4);
  
  randNumber5 = random(t1,t2);
  Serial.println(randNumber5);
  randNumber6 = random(t3,t4);
  Serial.println(randNumber6);
  
  randNumber7 = random(t1,t2);
  Serial.println(randNumber7);
  randNumber8 = random(t3,t4);
  Serial.println(randNumber8);
  
  randNumber9 = random(t1,t2);
  Serial.println(randNumber9);
  randNumber10 = random(t3,t4);
  Serial.println(randNumber10);
  
  randNumber11 = random(t1,t2);
  Serial.println(randNumber11);
  randNumber12 = random(t3,t4);
  Serial.println(randNumber12);
  
  unsigned long currentMillis1 = millis();
  unsigned long currentMillis2 = millis();
  unsigned long currentMillis3 = millis();
  unsigned long currentMillis4 = millis();
  unsigned long currentMillis5 = millis();
  unsigned long currentMillis6 = millis();


  if ((ledState1 == LOW) && (currentMillis1 - previousMillis1 >= randNumber1))
  {
    ledState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis1;   // Remember the time
    digitalWrite(ledPin1, ledState1);    // Update the actual LED
  }
  else if ((ledState1 == HIGH) && (currentMillis1 - previousMillis1 >= randNumber2))
  {
    ledState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis1;  // Remember the time
    digitalWrite(ledPin1, ledState1);  // Update the actual LED
  }






  if ((ledState2 == LOW) && (currentMillis2 - previousMillis2 >= randNumber3))
  {
    ledState2 = HIGH;  // turn it on
    previousMillis2 = currentMillis2;   // Remember the time
    digitalWrite(ledPin2, ledState2);   // Update the actual LED
  }
  
  else if ((ledState2 == HIGH) && (currentMillis2 - previousMillis2 >= randNumber4))
  {
    ledState2 = LOW;  // Turn it off
    previousMillis2 = currentMillis2;  // Remember the time
    digitalWrite(ledPin2, ledState2);  // Update the actual LED
  }






  if ((ledState3 == LOW) && (currentMillis3 - previousMillis3 >= randNumber5))
  {
    ledState3 = HIGH;
    previousMillis3 = currentMillis3;
    digitalWrite(ledPin3, ledState3);
  }

  else if ((ledState3 == HIGH) && (currentMillis3 - previousMillis3 >= randNumber6))
  {
    ledState3 = LOW;
    previousMillis3 = currentMillis3;
    digitalWrite(ledPin3, ledState3);
  }



  
  
  if ((ledState4 == LOW) && (currentMillis4 - previousMillis4 >= randNumber7))
  {
    ledState4 = HIGH;
    previousMillis4 = currentMillis4;
    digitalWrite(ledPin4, ledState4);
  }

  else if ((ledState4 == HIGH) && (currentMillis4 - previousMillis4 >= randNumber8))
  {
    ledState4 = LOW;
    previousMillis4 = currentMillis4;
    digitalWrite(ledPin4, ledState4);
  }




  
  if ((ledState5 == LOW) && (currentMillis5 - previousMillis5 >= randNumber9))
  {
    ledState4 = HIGH;  // turn it on
    previousMillis4 = currentMillis5;   // Remember the time
    digitalWrite(ledPin5, ledState5);   // Update the actual LED
  }

  else if ((ledState5 == HIGH) && (currentMillis5 - previousMillis5 >= randNumber10))
  {
    ledState4 = LOW;  // Turn it off
    previousMillis4 = currentMillis5;  // Remember the time
    digitalWrite(ledPin5, ledState5);  // Update the actual LED
  }

  
  if ((ledState6 == LOW) && (currentMillis6 - previousMillis6 >= randNumber11))
  {
    ledState4 = HIGH;  // turn it on
    previousMillis4 = currentMillis6;   // Remember the time
    digitalWrite(ledPin6, ledState6);   // Update the actual LED
  }

  else if ((ledState6 == HIGH) && (currentMillis6 - previousMillis6 >= randNumber12))
  {
    ledState4 = LOW;  // Turn it off
    previousMillis4 = currentMillis6;  // Remember the time
    digitalWrite(ledPin6, ledState6);  // Update the actual LED
  }

}

aimant_random2b_bug.ino (5.05 KB)

int randNumber1 = 5;
int randNumber2 = 5;
int randNumber3 = 5;
int randNumber4 = 5;
int randNumber5 = 5;
int randNumber6 = 5;
int randNumber7 = 3;
int randNumber8 = 3;
int randNumber9 = 4;
int randNumber10 = 4;
int randNumber11 = 7;
int randNumber12 = 7;

When you start numbering variable names, it's time to start thinking about arrays.

  unsigned long currentMillis1 = millis();
  unsigned long currentMillis2 = millis();
  unsigned long currentMillis3 = millis();
  unsigned long currentMillis4 = millis();
  unsigned long currentMillis5 = millis();
  unsigned long currentMillis6 = millis();

6 copies of now is just plain dumb.

The last three pins all update the time stored in previousMillis4, though they depend on previousMillis4, previousMillis5, and previousMillis6.

Proper use of arrays would have made that impossible to copy/paste/edit incorrectly, since no copy/paste/edit would have been needed.

The last three pins all update the time stored in previousMillis4,

Copy/Paste/Forget to change code strikes again.

Thank you for saying i m plain dumb, it must be true because I didn't understand a massive oart of your reply.
What depends on previousmillis4, 5, 6 and not on 1, 2 and 3 ?

1, 2 and 3 works, 4, 5 and 6 looks like a numbers too, so.. why it doesnt work ?

Thanks for pointing the newbie errors and talking to me about arrays, i will check that.
But should be my problem resolved with your reply ?

do you mean

unsigned long currentMillis1 = millis();
unsigned long currentMillis2 = millis();
unsigned long currentMillis3 = millis();
unsigned long currentMillis4 = millis();
unsigned long currentMillis5 = millis();
unsigned long currentMillis6 = millis();

I did this by feeling, I thought it would give me different currentMillis "stored" times. Should I change millis() to millis1() 2, 3 etc ?

4, 5 and 6 looks like a numbers too, so.. why it doesnt work ?

Look at the code that works. You compare currentMillis1 to previosMillis1, and then update previousMillis1.

Look at the code that does not work. You compare currentMillis5 to previosMillis5, and then update previousMillis4.

You need to explain why you think that should work.

If you need to perform one of 6 tasks depending on what time it is now, how many times do you need to look at your watch? ONCE! Not 6 times.

Should I change millis() to millis1() 2, 3 etc ?

You can certainly try, but there is only one millis() function, like you only wear one watch, so you can probably guess how well that will work. If not, try it.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Ok!!
Thank you for your eyes :slight_smile:

Fact is that the Nanos I recieved arn't working with Board : "Arduino Nano"
Only with "Mini" or "Arduino/Genuino Uno".

So it made me think about a bigger problem.

Thank you again

I didn't take the time to put your random ranges into an array (I couldn't figure it out actually) but you could consider simplifying your code using the (suggested) array...

byte ledPin[6] = {2,3,4,5,6,7};
unsigned long lastMillis[6] = {0};
unsigned long ledDelay[6] = {0};

void setup() 
{
  randomSeed(analogRead(A0));
  for(int i = 0; i < sizeof(ledPin)/sizeof(ledPin[0]); i++)
  {
    pinMode(ledPin[i], OUTPUT);
    digitalWrite(ledPin[i], random(2));
    ledDelay[i] = random(2500);
  }
}

void loop() 
{
  for(int i = 0; i < sizeof(ledPin)/sizeof(ledPin[0]); i++)
  {
    if(millis() - lastMillis[i] > ledDelay[i])
    {
      digitalWrite(ledPin[i], !digitalRead(ledPin[i])); //edit added the not
      lastMillis[i] = millis();
      ledDelay[i] = random(2500);
    }
  }
}

compiles but not tested

do you mean

unsigned long currentMillis1 = millis();
unsigned long currentMillis2 = millis();
unsigned long currentMillis3 = millis();
unsigned long currentMillis4 = millis();
unsigned long currentMillis5 = millis();
unsigned long currentMillis6 = millis();

I did this by feeling, I thought it would give me different currentMillis "stored" times. Should I change millis() to millis1() 2, 3 etc ?

You look on the clock and write down the time (hours and minutes).
You repeat this in quick succession.

How big do you think that the chances are that the times that you wrote down are different? Same applies to the Arduino, it can look at the clock hundreds of times per millisecond. So all your variables will basically have the same time.

Conclusion: you only need one variable to store the current time.

Not only previousMillis is wrong (no code tags to use bold). Last few lines all do their best to set ledState4 = LOW all the time. I can't see ledState5 nor ledState6 ever change value

if ((ledState5 == LOW) && (currentMillis5 - previousMillis5 >= randNumber9))
{
ledState4 = HIGH; // turn it on

Hi,
Have you loaded a simple code that checks each output by activating each relay onr after the other.

Tom.... :slight_smile:

Thank you very much BulldogLowell for this.
I loaded it and it looks fine.
It will take a few time for me to understand that code. Looks like few levels up.

By that time, I see two "random(2500)"
What will they change independently ?

I do not use the relay anymore, I was waiting for IRL540N mosfet I ordered.
This is for turning on and off 12v 500ma magnets.

I can't make them on too long, mosfets are ok, but these magnets are getting hot.

So what i m looking for is a range of 12000-30000ms OFF and a range of 50-2500ms ON.

My rustic/1000ACN code is bad.
I dont know if what I m going to tell is what 3-4 of you tried to make me understand but,
It does turn the outputs ON in a random way, but all together. Then waits random times for each outputs but they are all close to each others. So It become a repetitive randomness.

That is not what i m looking for.
I m looking to make this slowly and descretely live its life :

BulldogLowell:
I didn't take the time to put your random ranges into an array (I couldn't figure it out actually) but you could consider simplifying your code using the (suggested) array...

byte ledPin[6] = {2,3,4,5,6,7};

unsigned long lastMillis[6] = {0};
unsigned long ledDelay[6] = {0};

void setup()
{
  randomSeed(analogRead(A0));
  for(int i = 0; i < sizeof(ledPin)/sizeof(ledPin[0]); i++)
  {
    pinMode(ledPin[i], OUTPUT);
    digitalWrite(ledPin[i], random(2));
    ledDelay[i] = random(2500);
  }
}

void loop()
{
  for(int i = 0; i < sizeof(ledPin)/sizeof(ledPin[0]); i++)
  {
    if(millis() - lastMillis[i] > ledDelay[i])
    {
      digitalWrite(ledPin[i], !digitalRead(ledPin[i])); //edit added the not
      lastMillis[i] = millis();
      ledDelay[i] = random(2500);
    }
  }
}




compiles but not tested