Random digital write to array

I have 12 digital ouputs stored in an array in this format:

int en[] = {2,3,4,5,6,7,8,12,A1,A2A3,A4,A5};

I would like to create a function that chooses a random seed number between 1-4096, converts that number to binary, then digitalwrite to the appropriate pins. I can do the first two parts but I need a way to define the 4096 possible states without defining them individually. The ouputs are LEDs for a 12 bit display. Thanks :slight_smile:

Can you post your existing code and a circuit diagram please.
It will give us a starting point.

You might like to look at this link;

Thanks.. Tom... :grinning: :+1: :coffee: :australia:


Something like this ?

uint16_t randomValue = 1234;

for ( uint8_t i = 0; i < 12; i++ )
  uint8_t pin = en[i];
  uint8_t bit = ( randomValue >> i ) & 1;
  digitalWrite( pin, bit == 1 ? HIGH : LOW );

@guix Please document your code for the OP - they may not know what a uint16_t, or a uint8_t are, or what a turnary operator is, or how it works.

int rand = random(4096);
for (byte bit = 0; bit < 12; bit++ )
  digitalWrite(en[bit], bitRead(rand, bit) == 1 ? HIGH : LOW );


that's equivalent to setting each pin randomly to high or low, so

for (byte bit = 0; bit < 12; bit++ )
  digitalWrite(en[bit], random(2) == 1 ? HIGH : LOW );

Except that you can't represent 4096 with 12 LEDs, you would need 13th led for that. 4095 is the largest number you can represent with 12 LEDs.

1 Like

I'm about to implement your and guix's code. If im understanding this correctly the conditional / tunary operator chooses a statement based on the condition presented.

so I could rephrase "digitalWrite(en[bit], random(2) == 1 ? HIGH : LOW );" as

IF bit = 1 THEN do something ELSE do something else

also where did you get random(2) from and you declared "bit" as a byte whereas I have 12 bits, will the function change the variable to an appropriate data type automatically?

furthermore, in the expression "digitalWrite(en[bit], random(2) == 1 ? HIGH : LOW );"

are "en[bit" and "random(2) == 1 ? HIGH : LOW" exclusive to digitialwrite? or are they simple sequential statements being parsed in order? I hope this makes sense.

Thanks for the help

Not a statement, a value.

In some other languages, you could write

digitalWrite(pin, if (i == 1) HIGH else LOW);

but in C it's a little less easy to read

digitalWrite(pin, i == 1 ? HIGH : LOW);

Your plan is to make a random value between 0 and 4095, in other words a random 12 bit value, then extract each of the bits to light, or not light, 12 LEDs. But that's exactly the same as making 12 random values between 0 and 1, in other words 12 random 1-bit values. "random(2)" gives a random number between 0 and 1, a 1-bit random value. So instead of extracting bits from a 12-bit random value, why not just make 12 random 1-bit values? It's simpler!

The variable "bit" in my code does not hold a 12 bit value, only a number between 0 and 11. So a byte, which is 8 bits and can hold numbers up to 255, is ok to use for that.

I was under the impression that byte used 8 bits of memory on the chip but now I see its representing 8 bits of information, very cool. I got my circuit to work generating random colours and random patterns. Now I would like a way to create patterns within the random output.

For example imagine 12 LEDs in a horizontal row. I would like one pattern to be 100000000001 and one pattern to be 110000000011 etc.

My objective is to create patterns that appear in a random order, kind of like the popular red rider LED circuit. Eventually I would like to incorporate mathematical functions for these patterns but for now I'm content with setting each bit.


No, they are standard C expressions and can be used in any line of C code
"en[bit]" simply accesses the array you declared at the top of your code. If "bit" was 10, for example, it would be the same as "en[10]" which is A4, the pin number that digitalWrite needs. And "random(2) == 1 ? HIGH : LOW" gives a HIGH or LOW value it also needs.

HIGH and LOW are just values that are pre-defined in the Arduino language (not the C language, by the way). Arduino language is based on C but with some extra commands/functions like digitalWrite() and extra values like HIGH and LOW added on.

In fact HIGH is 1 and LOW is 0. So you could write

digitalWrite(en[bit], random(2));

and it would work. Or at least it would work today. But in a future version of the Arduino language, they might decide to change the values for HIGH and LOW to be something else, like 100 and 200, and then the code above would stop working. So even though we know that HIGH=1 and LOW=0, it's best not to use "tricks" that rely on those values, but instead always use HIGH and LOW explicitly.

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