small medium large xlarge

Back to: All Forums  Arduino
Generic-user-small
15 Jan 2012, 16:21
Derek (2 posts)

I’m having trouble with the first exercise in the Binary Dice chapter. I understand for example how the digitalWrite(result & B001) works, but can not figure out how to use a number to turn on a series of LEDs. Any help would be much appreciated.

Also (and maybe this is my problem), I don’t really understand line 34 of the code “output_result(guess);”. It seems to me that the output_result() function defined later in the code only contains the variable “result”. Why does it work with “result” as well as with “guess”?

So far the book has been great. I’d just like to make sure I’m understanding everything before moving forward.

Generic-user-small
15 Jan 2012, 16:42
Derek (2 posts)

I did get this to work, but I’m not sure that it is the best way to go about this program? And still I am unsure about the output_result() function.

void output_result(const long result) { if (result == 1){ digitalWrite(LEDa, LOW); digitalWrite(LEDb, LOW); digitalWrite(LEDc, LOW); digitalWrite(LEDd, HIGH); digitalWrite(LEDe, LOW); digitalWrite(LEDf, LOW); digitalWrite(LEDg, LOW); } if (result == 2){ digitalWrite(LEDa, HIGH); digitalWrite(LEDb, LOW); digitalWrite(LEDc, LOW); digitalWrite(LEDd, LOW); digitalWrite(LEDe, LOW); digitalWrite(LEDf, LOW); digitalWrite(LEDg, HIGH); } if (result == 3){ digitalWrite(LEDa, HIGH); digitalWrite(LEDb, LOW); digitalWrite(LEDc, LOW); digitalWrite(LEDd, HIGH); digitalWrite(LEDe, LOW); digitalWrite(LEDf, LOW); digitalWrite(LEDg, HIGH); } if (result == 4){ digitalWrite(LEDa, HIGH); digitalWrite(LEDb, LOW); digitalWrite(LEDc, HIGH); digitalWrite(LEDd, LOW); digitalWrite(LEDe, HIGH); digitalWrite(LEDf, LOW); digitalWrite(LEDg, HIGH); } if (result == 5){ digitalWrite(LEDa, HIGH); digitalWrite(LEDb, LOW); digitalWrite(LEDc, HIGH); digitalWrite(LEDd, HIGH); digitalWrite(LEDe, HIGH); digitalWrite(LEDf, LOW); digitalWrite(LEDg, HIGH); } if (result == 6){ digitalWrite(LEDa, HIGH); digitalWrite(LEDb, HIGH); digitalWrite(LEDc, HIGH); digitalWrite(LEDd, LOW); digitalWrite(LEDe, HIGH); digitalWrite(LEDf, HIGH); digitalWrite(LEDg, HIGH); } }

Maik_schmidt_avatar2_pragsmall
15 Jan 2012, 17:35
Maik Schmidt (122 posts)

Hi Derek!

This might be not the most elegant way to solve the problem, but if it works, it is just fine!

Of course, you could try to find an algorithm using less statements. For example, you could represent the dice’s LEDs as an array and turn the current result into a binary number. Then you’d iterate over the bits of the binary number and control the LEDs accordingly.

But for this example your solution is absolutely sufficient.

Regarding your question about the output_result function: this function outputs an integer number as blinking LEDs and it does not care where the number comes from. So it outputs the result of the roll of a dice as well as a user’s guess.

Cheers,

Maik

Generic-user-small
05 Jun 2012, 23:29
Kevin Schweikert (1 post)

I had no problem getting the dice to work with random numbers 1 through 6 with the following line:
long result = random(1, 7); However, when I tried long result = random(1, 8); (thinking I’d use all binary digits) the random number is always 1. I tried other numbers and output the result serially to the computer with no problems. Can someone explain why 1 through 7 does not work??

Maik_schmidt_avatar2_pragsmall
07 Jun 2012, 09:47
Maik Schmidt (122 posts)

Hi Kevin!

I am not sure, if I fully understand what you’re trying to accomplish. As you can see on http://arduino.cc/en/Reference/Random the random(min, max) function returns a random number as a long value. The value is between min (inclusive) and max (exclusive). For example, random(4, 90) returns random values between 4 and 89.

Cheers, Maik

You must be logged in to comment