# Outputing random binary nibble from pin 0 to pin 3

Hello all I just registered to the forums and I hope that I will be a regular. I’ve got my Arduino Duemilanove 328 some time back but I just didn’t had the time to play with it.

Now I’m trying to get this problem up and running. My question is, how can I output a random binary nibble (random(15) in decimal, 2^4) throughout pins 0 1 2 3 ?

I’ve tried to sort out the problem, but I just didn’t have any luck I’m attaching my code that I have been managed to put togerther so far, its a little messy because I am debugging it.

``````int icPin[3];
int x;

void blank() {
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}

void DecToBin(int Broj) {
icPin[3] = Broj % 2;
Broj=Broj/2;
icPin[2] = Broj % 2;
Broj=Broj/2;
icPin[1] = Broj % 2;
Broj=Broj/2;
icPin[0] = Broj % 2;

}

void LightUpCR() {
x = random(15);
Serial.print("Momentalen broj : ");
Serial.println(x);
DecToBin(x);
/* Serial.print("Momentalno vrednosti na icPin : ");
Serial.print(icPin[0]);
Serial.print(" ");
Serial.print(icPin[1]);
Serial.print(" ");
Serial.print(icPin[2]);
Serial.print(" ");
Serial.print(icPin[3]);
Serial.println(" "); */
boolean a, b, c, d;
a = true;
b = true;
c = true;
d = true;
if (icPin[0] == 0) { a = false; }
if (icPin[1] == 0) { b = false; }
if (icPin[2] == 0) { c = false; }
if (icPin[3] == 0) { d = false; }

if (a==true) Serial.print("a - true ");
else Serial.print("a - false ");

if (b==true) Serial.print("b - true ");
else Serial.print("b - false ");

if (c==true) Serial.print("c - true ");
else Serial.print("c - false ");

if (d==true) Serial.println("d - true ");
else Serial.println("d - false ");
//blank();
digitalWrite(0, a);
digitalWrite(1, b);
digitalWrite(2, c);
digitalWrite(3, d);

//Serial.println(" ");
}

void setup() {
Serial.begin(9600);
for (int i=0; i<=3; i++) {
pinMode(i, OUTPUT);
}
}

void loop() {
LightUpCR();
delay(500);
blank();
}
``````

There are not enough elements in icPin and it really should be declared boolean

``````[glow]boolean[/glow] icPin[[glow]4[/glow]];
``````

A small change when calling random to get the full range…

``````  x = random([glow]16[/glow]);
``````

byte is a more appropriate datatype for Broj…

``````void DecToBin([glow]byte[/glow] Broj)
``````

I don’t have the means to test it but this is what I ended up with…

``````boolean icPin[4];
byte x;

void blank()
{
for ( char i=0; i < 4; ++i )
{
digitalWrite( i, LOW );
}
}

void DecToBin(byte Broj)
{
char i;

for ( i=0; i <= 3; ++i )
{
icPin[i] = ( Broj & ( 1 << i ) ) != 0;
}
}

void LightUpCR()
{
char i;

x = random(16);
Serial.print("Momentalen broj : ");
Serial.println(x);
DecToBin(x);

if ( icPin[0] ) Serial.print("a - true ");
else Serial.print("a - false ");

if ( icPin[1] ) Serial.print("b - true ");
else Serial.print("b - false ");

if ( icPin[2] ) Serial.print("c - true ");
else Serial.print("c - false ");

if ( icPin[3] ) Serial.println("d - true ");
else Serial.println("d - false ");

//blank();

for ( i=0; i <= 3; ++i )
{
digitalWrite( i, icPin[i] );
}
}

void setup()
{
Serial.begin(9600);

for ( char i=0; i <= 3; i++ )
{
pinMode( i, OUTPUT );
}
}

void loop()
{
LightUpCR();
delay(500);
blank();
}
``````

Don’t try this though, it uses pins 0 & 1 and those should be reserved for the serial communications use this instead:-

``````for ( i=2; i <= 6; ++i )
{
digitalWrite( i, icPin[i] );
}
``````

To output on pins 2 - 5

On the other hand that is a bit long winded, try this where val contains what you want to output:-

``````if(val & 1) digitalWrite(2,HIGH); else digitalWrite(2,LOW);
if(val & 2) digitalWrite(3,HIGH); else digitalWrite(3,LOW);
if(val & 4) digitalWrite(4,HIGH); else digitalWrite(4,LOW);
if(val & 8) digitalWrite(5,HIGH); else digitalWrite(5,LOW);
``````

For 4 lines it hardly seems worth going itto a loop.

Well... it didn't work guys. I need this signal from 4 pins so I can control a 4-to-16 demultiplexer for a 4x4x4 led cube :/ The code you gave me does exactly as mine did, doesn't output them randomly :| Changing the pins for avoiding pin0 and pin1 didn't work as well..

Only one note, random() should be 15, because from 0 to 15 there are 16 number e.g. one nibble ;)

Well... it didn't work guys

Post the Sketch.

Only one note, random() should be 15

The documentation, source code, testing, and I all disagree with you.

Well… it didn’t work guys.

Well I don’t believe you.

It might be your wiring that is mixed up.

Only one note, random() should be 15, because from 0 to 15 there are 16 number e.g. one nibble

random(x) should be x=16 to get values 0..15 - see: http://arduino.cc/en/Reference/Random

a random number between min and max-1 (long)

;)