Lesson 18: Electronic dice
تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
English
Roll without a dice
- A board game needs a dice, but you lost it. The Arduino can be one.
- Six LEDs show the number: 1 LED for a one, all 6 for a six.
- A button rolls, and
randompicks the number, so nobody knows what comes next.
Words for this lesson
| Word | Meaning |
|---|---|
| dice | a small cube with the numbers 1 to 6 for games |
| roll | throw the dice to get a number |
| random | you cannot say before what it will be |
| function | a named block of code you can use many times, like show(n) |
English
Which LEDs are on?
| n | Pins on | Pins off |
|---|---|---|
| 1 | 2 | 3, 4, 5, 6, 7 |
| 3 | 2, 3, 4 | 5, 6, 7 |
| 6 | 2, 3, 4, 5, 6, 7 | none |
- LED number i (counting from 0) is on pin 2 + i. So the first
nLEDs are pins 2 to 1 + n.
English
A random number
void setup() {
Serial.begin(9600);
for (int i = 0; i < 5; i++) {
Serial.println(random(1, 7));
}
}
void loop() {
}
random(1, 7)gives 1, 2, 3, 4, 5 or 6. The 7 is the first number it never gives.- On a real board the numbers come in the same order after every reset.
randomSeed(analogRead(A0));insetupmixes them up, because a pin with nothing on it reads a slightly different value each time.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Six LEDs on pins 2 to 7 show a number: 3 means the first three LEDs are on. Finish the function show(n): first switch all six off, then switch on the first n. The loop already counts 1, 2, 3, 4, 5, 6 with half a second for each number.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 4/5 · 280 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Now roll the dice. The button on pin 8 starts a roll: random(1, 7) gives a number from 1 to 6. Show it with show, and print You rolled: and the number. Before the first press all LEDs are off.
اضغط تشغيل لرؤية المخرجات هنا.