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();
}
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 ;)