【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百七十六:6mm大尺寸8x8LED方块方格点阵模块 可级联 红绿蓝白色 可选8级亮度
项目之十一:制作一个随机电子骰子
实验开源代码
- /*
- 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
- 实验一百七十六:6mm大尺寸8x8LED方块方格点阵模块 可级联 红绿蓝白色 可选8级亮度
- 项目之十一:制作一个随机电子骰子
- 接脚连线:按钮开关接D3
- MAX7219 UNO
- VCC →→→→→ 5V
- GND →→→→→ GND
- DIN →→→→→ D12(数据,数据接收引脚)
- CS →→→→→ D11(负载,命令接收引脚)
- CLK →→→→→ D10(时钟,时钟引脚)
- */
-
- #include "LedControl.h"
- LedControl lc=LedControl(12,10,11,1);
-
- unsigned long delaytime=50;
- int ButtonPin=3;
- int Current=1;
-
- void setup() {
- lc.shutdown(0,false); //MAX72XX 在启动时处于省电模式,必须叫醒
- lc.setIntensity(0,8); //将亮度设置为中等值
- lc.clearDisplay(0); //并清除显示
-
- randomSeed(analogRead(0));
- pinMode(ButtonPin, INPUT);
- }
-
- void showNum(int x) {
- //这里是字符的数据
- byte one[8]={
- B00000000,
- B00000000,
- B00000000,
- B00111000,
- B00111000,
- B00000000,
- B00000000,
- B00000000};
-
- byte two[8]={
- B00000000,
- B00000110,
- B00000110,
- B00000000,
- B00000000,
- B01100000,
- B01100000,
- B00000000};
-
- byte three[8]={
- B00000000,
- B00111000,
- B00111000,
- B00000000,
- B01100110,
- B01100110,
- B01100110,
- B00000000};
-
- byte four[8]={
- B00000000,
- B01100110,
- B01100110,
- B00000000,
- B00000000,
- B01100110,
- B01100110,
- B00000000};
-
- byte five[8]={
- B00000000,
- B01100110,
- B01100110,
- B00011000,
- B00011000,
- B01100110,
- B01100110,
- B00000000};
-
- byte six[8]={
- B01100110,
- B01100110,
- B00000000,
- B01100110,
- B01100110,
- B00000000,
- B01100110,
- B01100110};
-
- switch (x) {
- case 1:
- lc.setRow(0,0,one[0]);
- lc.setRow(0,1,one[1]);
- lc.setRow(0,2,one[2]);
- lc.setRow(0,3,one[3]);
- lc.setRow(0,4,one[4]);
- lc.setRow(0,5,one[5]);
- lc.setRow(0,6,one[6]);
- lc.setRow(0,7,one[7]);
- break;
-
- case 2:
- lc.setRow(0,0,two[0]);
- lc.setRow(0,1,two[1]);
- lc.setRow(0,2,two[2]);
- lc.setRow(0,3,two[3]);
- lc.setRow(0,4,two[4]);
- lc.setRow(0,5,two[5]);
- lc.setRow(0,6,two[6]);
- lc.setRow(0,7,two[7]);
- break;
-
- case 3:
- lc.setRow(0,0,three[0]);
- lc.setRow(0,1,three[1]);
- lc.setRow(0,2,three[2]);
- lc.setRow(0,3,three[3]);
- lc.setRow(0,4,three[4]);
- lc.setRow(0,5,three[5]);
- lc.setRow(0,6,three[6]);
- lc.setRow(0,7,three[7]);
- break;
-
- case 4:
- lc.setRow(0,0,four[0]);
- lc.setRow(0,1,four[1]);
- lc.setRow(0,2,four[2]);
- lc.setRow(0,3,four[3]);
- lc.setRow(0,4,four[4]);
- lc.setRow(0,5,four[5]);
- lc.setRow(0,6,four[6]);
- lc.setRow(0,7,four[7]);
- break;
- case 5:
- lc.setRow(0,0,five[0]);
- lc.setRow(0,1,five[1]);
- lc.setRow(0,2,five[2]);
- lc.setRow(0,3,five[3]);
- lc.setRow(0,4,five[4]);
- lc.setRow(0,5,five[5]);
- lc.setRow(0,6,five[6]);
- lc.setRow(0,7,five[7]);
- break;
-
- case 6:
- lc.setRow(0,0,six[0]);
- lc.setRow(0,1,six[1]);
- lc.setRow(0,2,six[2]);
- lc.setRow(0,3,six[3]);
- lc.setRow(0,4,six[4]);
- lc.setRow(0,5,six[5]);
- lc.setRow(0,6,six[6]);
- lc.setRow(0,7,six[7]);
- break;
- }
- }
-
- void loop() {
- int Next;
- boolean MarkStart=false; //标记是否按键抬起
-
- if (digitalRead(ButtonPin)==LOW) {
- showNum(Current);
- do {
- Next=random(1,7);
- }
-
- while (Current==Next); //因为如果两次出现相同的数字,看起来
- //会觉得没有变,所以这里要保证生成不同
- Current=Next;
- delay(delaytime);
- MarkStart=true;
- }
-
- if ((MarkStart==true) && (digitalRead(ButtonPin)==HIGH)){ //按键抬起,生成实际显示的结果
- MarkStart=false;
- showNum(random(1,7));
- }
- }
复制代码
|