Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code written below to control 8 relays in random. The program is for my Halloween props. I am new to coding. I only want to have the maximum of 3 relays operating at any one time in a random sequence. Any ideas on how I can achieve this problem.

Many Thanks in advance
C++
#define time1 100
#define time2 3000

void setup()
{
pinMode(2, OUTPUT); //relay 1
pinMode(3, OUTPUT); //relay 2
pinMode(4, OUTPUT); //relay 3
pinMode(5, OUTPUT); //relay 4
pinMode(6, OUTPUT); //relay 5
pinMode(7, OUTPUT); //relay 6
pinMode(8, OUTPUT); //relay 7
pinMode(9, OUTPUT); //relay 8
delay(50); //Check that all relays are inactive at Reset
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
}
//--(end setup )---

void loop()
{

digitalWrite(random(2,10), LOW); // sets a random relay on
delay(random(time1,time2)); // wait for some time (between 1 and 30 seconds)
digitalWrite(random(2,10), HIGH); // sets a random relay off
delay(random(time1,time2)); // wait for some time (between 1 and 30 seconds)
}


What I have tried:

Tried re writing the code but was unsuccessful and the program froze.
Posted
Updated 3-Sep-17 3:05am
v2
Comments
PIEBALDconsult 2-Sep-17 18:24pm    
Off-hand, I suggest making a set of eight-bit values, each with three bits set -- you'll need fifty-six of them.
Then pick one at random.
CPallini 3-Sep-17 8:55am    
Ninety-three, because the OP needs at maximum three relays switched ON.
PIEBALDconsult 3-Sep-17 9:50am    
On behalf of the viewers, I insist on exactly three relays on at all times. :D
One and two relays just won't do.
CPallini 4-Sep-17 3:26am    
Sorry: one, two or zero. :-D
PIEBALDconsult 4-Sep-17 11:25am    
OK, but not as good a show.

This is C# code, but should be easily converted to Arduino's C:
C#
// Generate 20 values...
for (int x = 0; x < 20; x++)
{
    // Holds the byte value were turning bits on in.
    byte value = 0;

    // count holds the maximum number of bits were going to
    // enable this time around, up to maxBitsOnPerByte.
    // In your case, maxBitsOnPerByte should be 3.

    // RNG is the random number class. 'Next' will return
    // an integer between 0 and the specified limit, EXCLUSIVE.
    // In your example, this means 0, 1, 2, and 3.
    int count = RNG.Next(maxBitsOnPerByte + 1);

    // Loop 'count' times, turning on a random bit in the byte.
    // Note, this CAN turn the same bit on multiple times!
    for (int y = 0; y < count; y++)
    {
        // Take a single bit (1) and shift it left a random
        // number (from 0 to 7) bits. Then OR this value
        // with the existing value we're tracking.

        // In C#, the value of an OR operation is an integer
        // that must be cast back to a byte.
        value = (byte)(value | (0x1 << RNG.Next(8)));
    }

    // Convert the value to a string or 1's and 0's,
    // padded with 0's of course.
    string binary = Convert.ToString(value, 2).PadLeft(8, '0');
    Console.WriteLine($"Value: {binary}");
}
 
Share this answer
 
v2
As a simple approach, you might (randomly) choose how many relays could be ON at a given instant (say MAX_ON, it's just rand()%4).
Then iterate MAX_ON times, picking at each iteration a random relay (rand()%7). A possible relay repetition would not invalidate the (could) assumption.


[Update]
Something like (warning: not tested)
C
for (;;)
{
  int max_on = random(4);
  int n;
  for (n=0; n<max_on; ++n)
  {
    digitalWrite(random(2,10), LOW); 
  }
  delay(random(1,31)); // wait for some time (between 1 and 30 seconds)
}
[/Update]
 
Share this answer
 
v3
Comments
Member 13390625 3-Sep-17 15:02pm    
This solution sounds nice simple for a beginner like me, Which part of my original code would I modify? Would this work?

digitalWrite(random()%4)), LOW); // sets a MAX Relay random on
digitalWrite(random()%7)), LOW ; // sets Max On times
delay(random(time1,time2)); // wait for some time (between 1 and 30 seconds) digitalWrite(random(2,10), HIGH); // sets a random relay off
delay(random(time1,time2)); // wait for some time (between 1 and 30 seconds)

If this code would work, will it run in a constant loop?
Cheers
Mark.
CPallini 4-Sep-17 4:12am    
See my updated solution.
Member 13390625 4-Sep-17 18:38pm    
Thanks for your help, I haven't got a clue where to stick this instruction in my code I have listed previously....I copied your updated code into the Arduino IDE programme....Thingy.....and hit the verification button, kept on receiving messages similar to this:

exit status 1
expected ')' before ';' token
What have I done wrong?
CPallini 5-Sep-17 2:56am    
Sorry, there was a mistake in my code. Should be fixed now.
Member 13390625 5-Sep-17 17:35pm    
Hi Ya, Thanks for time and effort in helping me,
This is what I have done, combining my original code with your revised code. I hope I have put the two together correctly. Unfortunately, If this is the correct coding,

#define time1 100
#define time2 3000


void setup()
{
pinMode(2, OUTPUT); //relay 1
pinMode(3, OUTPUT); //relay 2
pinMode(4, OUTPUT); //relay 3
pinMode(5, OUTPUT); //relay 4
pinMode(6, OUTPUT); //relay 5
pinMode(7, OUTPUT); //relay 6
pinMode(8, OUTPUT); //relay 7
pinMode(9, OUTPUT); //relay 8
delay(50); //Check that all relays are inactive at Reset
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
}
//--(end setup )---

void loop()


{
for (;;)
{
int max_on = random(4);
int n;
for (n=0; n<max_on; ++n)
{
digitalWrite(random(2,10), LOW);
}
delay(random(1,31)); // wait for some time (between 1 and 30 seconds)
}

I receive this error code when Verified using Arduino IDE:

exit status 1
expected '}' at end of input

Have I broken something?
I think I should stick to being a VW Mechanic, this coding is a completely foreign language to me.(Nuts and bolts I do understand) Ha Ha.
Quote:
I have a code written below to control 8 relays in random.

Random means anything between 0 and 8 relays.
To get 3 relays in 8, you should use a technique named shuffle.
shuffling
You must build a program that will simulate shuffling cards.
Say that you have a deck of 8 cards, 5 cards black and 3 cards red.
You pick 1 at random in the 8 cards, and you remove it from the deck.
You pick 1 at random in the 7 cards remaining, and you remove it from the deck.
And so on ...
How does it work in a program ?
Set an array of size 8, set 5 elements to 0 and 3 elements to 1.
Pick a random number between 1 and 8, swap that element with element 1.
Pick a random number between 2 and 8, swap that element with element 2.
Pick a random number between 3 and 8, swap that element with element 3.
...
The result in the array is the position of your relays.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900