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.
출력을 보려면 '실행'을 클릭하세요.