Turn off random relays with random delays.

Hi!
I am trying to start with some small Arduino projects, but I need help to get over the first thresholds.
I am trying to turn off 8 relays in random order with random delays in between, when I press a button. After the last relay have been turned off, all relays should be turned on again until I press the button the next time.
Could someone please give me the right setup and functions to start with.

Thank you!

Could someone please give me the right setup and functions to start with.

void setup()
{
}

Use digitalRead() to read the switch, randomSeed() to see the random number generator, random() to get random numbers, digitalWrite() to turn the relays off and on, and delay() to waste time.

Don't forget the right "pinMode"s

What I need help with is how i can use a random function after another random function and make sure I do not pick the same random number. I want the relays to turn of one by one in a random order.

Have you seen this?

https://www.arduino.cc/en/Reference/RandomSeed

.

Hi!
Can´t anyone point to a program that does this or similar. I think I need a program to start from.
I want to learn how to do it my self. But I am not there yet.

Pick what you need out of this

byte prevRandom;
byte newRandom;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  while (newRandom == prevRandom)
  {
    newRandom = random(0, 10);
  }
  prevRandom = newRandom;
  Serial.println(newRandom);
  delay(500);
}

uddmannen:
What I need help with is how i can use a random function after another random function and make sure I do not pick the same random number. I want the relays to turn of one by one in a random order.

Google card shuffle algorithm.

Hi!
I did it. It works as I planned.
Now I just need to add a button to restart it.
Help!

void setup() {
 int i;
 Serial.begin(9600);
 
 int arr[] = {3, 4, 5, 6, 7, 8, 9, 10};
 int n = sizeof(arr)/ sizeof(arr[0]);
 randomize (arr, n);
 for (i - 0; i < n; i++) {
   Serial.print(arr[i]);
   Serial.print("   ");
 }
    
     pinMode(3, OUTPUT);
     pinMode(4, OUTPUT);
     pinMode(5, OUTPUT);
     pinMode(6, OUTPUT);    
     pinMode(7, OUTPUT);
     pinMode(8, OUTPUT);
     pinMode(9, OUTPUT);
     pinMode(10, OUTPUT);
 
 digitalWrite(3, HIGH);
 digitalWrite(4, HIGH);
 digitalWrite(5, HIGH);
 digitalWrite(6, HIGH);
 digitalWrite(7, HIGH);
 digitalWrite(8, HIGH);
 digitalWrite(9, HIGH);
 digitalWrite(10, HIGH);
 delay(2000);
 
 digitalWrite(arr[0], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println("   "); //prints new line
 Serial.println(arr[0]); //prints pin-number
 delay(1000);
 digitalWrite(arr[0], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for a second

 digitalWrite(arr[1], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println(arr[1]); //prints pin-number
 delay(1000);
 digitalWrite(arr[1], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for 5-15 seconds

 digitalWrite(arr[2], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println(arr[2]); //prints pin-number
 delay(1000);
 digitalWrite(arr[2], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for 5-15 seconds

 digitalWrite(arr[3], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println(arr[3]); //prints pin-number
 delay(1000);
 digitalWrite(arr[3], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for 5-15 seconds

 digitalWrite(arr[4], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println(arr[4]); //prints pin-number
 delay(1000);
 digitalWrite(arr[4], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for 5-15 seconds

 digitalWrite(arr[5], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println(arr[5]); //prints pin-number
 delay(1000);
 digitalWrite(arr[5], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for 5-15 seconds

 digitalWrite(arr[6], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println(arr[6]); //prints pin-number
 delay(1000);
 digitalWrite(arr[6], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for 5-15 seconds

 digitalWrite(arr[7], LOW);   // turn the LED off (LOW is the voltage level)
 Serial.println(arr[7]); //prints pin-number
 delay(1000);
 digitalWrite(arr[7], HIGH);    // turn the LED on again
 delay(random(5000,7000));              // wait for 5-15 seconds
 }
void loop() {

}

void swap (int *a, int *b)
{
   int temp = *a;
   *a = *b;
   *b = temp;
}

void randomize ( int arr[], int n )
{
   // Use a different seed value so that we don't get same
   // result each time we run this program
   randomSeed(analogRead(A0));

   // Start from the last element and swap one by one. We don't
   // need to run for the first element that's why i > 0
   for (int i = n-1; i > 0; i--)
   {
       long j = random(0, n);
       swap(&arr[i], &arr[j]);
       
   }
}

It works as I planned.

Post it.

UKHeliBob:
Post it.

Post it PROPERLY!

uddmannen:
Now I just need to add a button to restart it.

your next post is "Pushbutton not working"

you need to ditch all the delays there....

Made it with push button and everything!
Thank you for your help!

 int i;
 int buttonState = 0;

void setup() {
 pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);    
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  
  pinMode(2 ,INPUT_PULLUP);   // activates the internal pull-up resistor 
  Serial.begin(9600);
  

  }
void loop() {

  buttonState = digitalRead(2);  // check if button is pushed
 
  if(buttonState==LOW)
  {
  int arr[] = {3, 4, 5, 6, 7, 8, 9, 10};
  int n = sizeof(arr)/ sizeof(arr[0]);
  randomize (arr, n);
  
  for (i - 0; i < n; i++) {
    Serial.print(arr[i]);
    Serial.print("   ");
  }
     
     
  delay(1000);
  digitalWrite(11, HIGH);      // turn the LED on.
  delay(1000);
  digitalWrite(11, LOW);       // turn the LED off.
  delay(random(2000,7000));
  
  digitalWrite(arr[0], LOW);   // turn the RELAY off
  Serial.println("   ");       // prints new line
  Serial.println(arr[0]);      // prints RELAY-number
  delay(1000);
  digitalWrite(arr[0], HIGH);  // turn the RELAY on again
  delay(random(3000,10000));   // wait for 3 to 10 seconds

  digitalWrite(arr[1], LOW);   // turn the RELAY off
  Serial.println(arr[1]);      // prints RELAY-number
  delay(1000);
  digitalWrite(arr[1], HIGH);  // turn the RELAY on again
  delay(random(3000,10000));    // wait for 3-10 seconds

  digitalWrite(arr[2], LOW);   
  Serial.println(arr[2]); 
  delay(1000);
  digitalWrite(arr[2], HIGH);   
  delay(random(3000,10000));              

  digitalWrite(arr[3], LOW);   
  Serial.println(arr[3]); 
  delay(1000);
  digitalWrite(arr[3], HIGH);    
  delay(random(3000,10000));              

  digitalWrite(arr[4], LOW);   
  Serial.println(arr[4]); 
  delay(1000);
  digitalWrite(arr[4], HIGH);    
  delay(random(3000,10000));              

  digitalWrite(arr[5], LOW);  
  Serial.println(arr[5]); 
  delay(1000);
  digitalWrite(arr[5], HIGH);    
  delay(random(3000,10000));      
         
  digitalWrite(arr[6], LOW);   
  Serial.println(arr[6]); 
  delay(1000);
  digitalWrite(arr[6], HIGH);    
  delay(random(3000,10000));              

  digitalWrite(arr[7], LOW);   
  Serial.println(arr[7]); 
  delay(1000);
  digitalWrite(arr[7], HIGH);    
     

  delay(1000);
  digitalWrite(11, HIGH);      // turn the LED on.
  delay(500);
  digitalWrite(11, LOW);       // turn the LED off.
  delay(500);
  
  digitalWrite(11, HIGH);      // turn the LED on.
  delay(500);
  digitalWrite(11, LOW);       // turn the LED off.
  delay(500);
  
  digitalWrite(11, HIGH);      // turn the LED on.
  delay(500);
  digitalWrite(11, LOW);       // turn the LED off.
  }
   else
   {
    //other instructions if needed, executed if button is not pushed 
   }
}

void swap (int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void randomize ( int arr[], int n )
{
    // Use a different seed value so that we don't get same
    // result each time we run this program
    randomSeed(analogRead(A0));
 
    // Start from the last element and swap one by one. We don't
    // need to run for the first element that's why i > 0
    for (int i = n-1; i > 0; i--)
    {
        long j = random(0, n);
        swap(&arr[i], &arr[j]);
        
    }
}

I made you an Automaton sketch that does the same thing in an event driven manner.

#include <Automaton.h>

Atm_led relay[8];
Atm_timer reset;
Atm_button button;

void setup() {

  randomSeed( analogRead( A5 ) );

  for ( int i = 0; i < 8; i++ ) {
    relay[i].begin( 4 + i ); // Initialize relays on pins 4..11
  }

  reset.begin( 0 )// First reset immediately (0 s)
    .onTimer( [] ( int idx, int v, int up ) {
      for ( int i = 0; i < 8; i++ ) {
        relay[i].trigger( relay[i].EVT_ON ); // Turn 8 relays on
      }   
    });

  button.begin( 2 ) // Button on pin 2
    .onPress( [] ( int idx, int v, int up ) { // When pressed
      if ( v ) {
        for ( int i = 0; i < 8; i++ ) {
          relay[i]
            .blink( random( 3000, 10000 ), 0, 1 ) // Set blink once for a random time
            .trigger( relay[i].EVT_BLINK ); // And trigger the 'blink'
        }
        reset
         .interval( 10000 )
         .trigger( reset.EVT_START ); // Trigger reset in 10 s
      }
    });   

  reset.trigger( reset.EVT_START ); // Reset relays (on)
}

void loop() {
  automaton.run();
}

The latest master branch of Automaton is available here: https://github.com/tinkerspy/Automaton/wiki