小時候的我們每個人都玩過這樣一種俗稱shai子找蜜,官方成為骰(tou)子的玩具,那么這種骰子有什么特點呢辅甥?
答:每一次擲出的數(shù)字都是不確定的,沒有規(guī)律的燎竖。
那么璃弄,今天我們來了解一款有意思的創(chuàng)客教育的Arduino實驗——智能骰子吧!
【實驗介紹】
本次實驗是利用Arduino套件构回,制作一個由按鍵控制使得數(shù)碼管隨機顯示數(shù)字的實驗夏块。
【實驗用具】
USB下載線×1
大面包板×1
共陰極八段數(shù)碼管×1
電阻×8
按鍵開關×1
跳線若干
【實驗電路圖】
【實驗代碼】
int switchPin=2; //定義switchPin引腳為2
#define A_LED 3
#define B_LED 4
#define C_LED 5
#define D_LED 6
#define E_LED 7
#define F_LED 8
#define G_LED 9 //數(shù)碼管中表示數(shù)字的引腳A~B分別連接第3~9引腳
int temp;
void setup()
{
Serial.begin(9600); //設置串口波特率為9600
pinMode(switchPin,INPUT);
pinMode(A_LED,OUTPUT);
pinMode(B_LED,OUTPUT);
pinMode(C_LED,OUTPUT);
pinMode(D_LED,OUTPUT);
pinMode(E_LED,OUTPUT);
pinMode(F_LED,OUTPUT);
pinMode(G_LED,OUTPUT); //設置2為輸入引腳;3~9為輸出引腳
}
void loop()
{
{
temp=random(1,7); //產(chǎn)生一個1~6中的隨機數(shù)
switch (temp){
case (1) :
{
Serial.println(1); //通過串口監(jiān)視器可以同步查看產(chǎn)生的隨機數(shù)
digitalWrite( A_LED , LOW );
digitalWrite( B_LED , HIGH );
digitalWrite( C_LED , HIGH );
digitalWrite( D_LED , LOW );
digitalWrite( E_LED , LOW );
digitalWrite( F_LED , LOW );
digitalWrite( G_LED , LOW ); //數(shù)碼管顯示“1”
break;
}
case (2) :
{
Serial.println(2);
digitalWrite( A_LED , HIGH );
digitalWrite( B_LED , HIGH );
digitalWrite( C_LED , LOW );
digitalWrite( D_LED , HIGH );
digitalWrite( E_LED , HIGH );
digitalWrite( F_LED , LOW );
digitalWrite( G_LED , HIGH ); //數(shù)碼管顯示“2”
break;
}
case (3) :
{
Serial.println(3);
digitalWrite( A_LED , HIGH );
digitalWrite( B_LED , HIGH );
digitalWrite( C_LED , HIGH );
digitalWrite( D_LED , HIGH );
digitalWrite( E_LED , LOW );
digitalWrite( F_LED , LOW );
digitalWrite( G_LED , HIGH ); //數(shù)碼管顯示“3”
break;
}
case (4) :
{
Serial.println(4);
digitalWrite( A_LED , LOW );
digitalWrite( B_LED , HIGH );
digitalWrite( C_LED , HIGH );
digitalWrite( D_LED , LOW );
digitalWrite( E_LED , LOW );
digitalWrite( F_LED , HIGH );
digitalWrite( G_LED , HIGH ); //數(shù)碼管顯示“4”
break;
}
case (5) :
{
Serial.println(5);
digitalWrite( A_LED , HIGH );
digitalWrite( B_LED , LOW );
digitalWrite( C_LED , HIGH );
digitalWrite( D_LED , HIGH );
digitalWrite( E_LED , LOW );
digitalWrite( F_LED , HIGH );
digitalWrite( G_LED , HIGH ); //數(shù)碼管顯示“5”
break;
}
case (6) :
{
Serial.println(6);
digitalWrite( A_LED , HIGH );
digitalWrite( B_LED , LOW );
digitalWrite( C_LED , HIGH );
digitalWrite( D_LED , HIGH );
digitalWrite( E_LED , HIGH );
digitalWrite( F_LED , HIGH );
digitalWrite( G_LED , HIGH ); //數(shù)碼管顯示“6”
break;
}
}
delay(1000);
}
}
【實驗知識點儲備】
了解隨機數(shù)
推薦大家看《時間簡史》纤掸,里面也有提及隨機數(shù)哦~