# Binary Cube problems

hey I programmed a Binary cube with 3 leds. when i push the button a short time the arduino set a random value (1-7) and the leds light up. when i push the button all the time the leds are lighting up very fast but the value 7 (all leds are on) is lighting up longer an brighter than the other values (for example value 5 first and third led. can you help me?

``````int ledpin = 5;
int ledpin1 = 6;
int ledpin2 = 7;
int taster = 10;
long randNumber;
void setup()
{
pinMode(ledpin, OUTPUT);
pinMode(ledpin1,OUTPUT);
pinMode(ledpin2,OUTPUT);
pinMode(taster,INPUT);

Serial.begin(9600);

}

void loop()
{
randNumber = random(1, 8);
Serial.println(randNumber);
delay(100);
digitalWrite(taster, LOW);
}

if (randNumber == 1){
digitalWrite(ledpin, HIGH);}
else {digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);}

if (randNumber == 2){
digitalWrite(ledpin1, HIGH);}
else{ digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);}

if (randNumber == 3){
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin1, HIGH);}
else{ digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);}

if (randNumber == 4){
digitalWrite(ledpin2, HIGH);}
else{ digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);}

if (randNumber == 5){
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin2, HIGH);}
else{ digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);}

if (randNumber == 6){
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, HIGH);}
else{ digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);}

if (randNumber == 7){
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, HIGH);}
else{ digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);}

}
``````

and the circiut

loop() executes thousands of times a second, so a random number is generated thousands of times a second .

``````   digitalWrite(taster, LOW);
``````

Why ?

oh sry i tried something
`  digitalWrite(taster, LOW);  `
this is not important

and can i set a "slower loop"?

and can i set a "slower loop"?

No. But you can put our random number generator within a "blink without delay" millis() timer.

``````if(digitalRead(taster) == HIGH) {
const unsigned long interval = 1000;
static unsigned long lastRandomNumber;
if(millis() -lastRandomNumber >= interval)
{
lastRandomNumber = millis();
byte randNumber = random(1, 8);
Serial.println(randNumber);
}
}
``````

hey thanks but i dont know how to add this to my code^^
because im a newbie at arduino

hey thanks but i dont know how to add this to my code^^
because im a newbie at arduino

Being newbie does not prevent you from reading the code and thinking things through.
Where do you think it should go?

``````if   (digitalRead(taster)==HIGH){
randNumber = random(1, 8);
Serial.println(randNumber);
delay(100);
digitalWrite(taster, LOW);
}
``````

my suggested modification

``````if(digitalRead(taster) == HIGH) {
const unsigned long interval = 1000;
static unsigned long lastRandomNumber;
if(millis() -lastRandomNumber >= interval)
{
lastRandomNumber = millis();
byte randNumber = random(1, 8);
Serial.println(randNumber);
}
}
``````

hey i asked because the leds arent lighting up but the serial monitor shows me random values do you have a idea why?

Pick a `randNumber`.
Step through each `if (randNumber == [i]N[/i]) ··· `.
What happens?

I messed up with the variable scope when I wrote the snippet which I tested alone and not as part of your program. In your program there may be two variables randNumber with different scope and only the local one is changing and getting printed.

See if this works.

``````int ledpin = 5;
int ledpin1 = 6;
int ledpin2 = 7;
int taster = 10;
//long randNumber;
byte randNumber;

void setup()
{
pinMode(ledpin, OUTPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(taster, INPUT);

Serial.begin(9600);
}

void loop()
{
/*
randNumber = random(1, 8);
Serial.println(randNumber);
delay(100);
digitalWrite(taster, LOW);
}
*/
const unsigned long interval = 1000;
static unsigned long lastRandomNumber;
if (millis() - lastRandomNumber >= interval)
{
lastRandomNumber = millis();
// byte randNumber = random(1, 8);
randNumber = random(1, 8);
Serial.println(randNumber);
}
// delay(100);
// digitalWrite(taster, LOW);
}

if (randNumber == 1) {
digitalWrite(ledpin, HIGH);
}
else {
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
if (randNumber == 2) {
digitalWrite(ledpin1, HIGH);
}
else {
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
if (randNumber == 3) {
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin1, HIGH);
}
else {
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
if (randNumber == 4) {
digitalWrite(ledpin2, HIGH);
}
else {
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
if (randNumber == 5) {
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin2, HIGH);
}
else {
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
if (randNumber == 6) {
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, HIGH);
}
else {
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
if (randNumber == 7) {
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, HIGH);
}
else {
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
}
``````

hey thanks nice its working now