本帖最后由 lxf 于 2020-8-17 13:31 编辑
目标
利用常闭震动传感器,使骰子的点数随机显示在MAX7219 8*8点阵屏上
实验材料
Arduino UNO开发板 四块MAX7219 8*8点阵屏 常闭震动传感器 配套USB数据线 配套杜邦线若干 Arduino IDE软件
内容
利用常闭震动传感器,甩动骰子,使骰子1-6的点数随机显示在MAX7219 8*8点阵屏上
接线图
备注:接线图中只接了1个MAX7219 8*8点阵屏
接线方式
Arduino UNO <------> 常闭震动传感器 5V <------> VCC GND <------> GND 3 <------> D0 Arduino UNO <------> MAX7219 8*8点阵屏1(IN) 5V <------> VCC GND <------> GND 11 <------> DIN 9 <------> CS 13 <------> CLK MAX7219 8*8点阵屏1(OUT) <------> MAX7219 8*8点阵屏2(IN) VCC <------> VCC GND <------> GND DOUT <------> DIN CS <------> CS CLK <------> CLK MAX7219 8*8点阵屏2(OUT) <------> MAX7219 8*8点阵屏3(IN) VCC <------> VCC GND <------> GND DOUT <------> DIN CS <------> CS CLK <------> CLK MAX7219 8*8点阵屏3(OUT) <------> MAX7219 8*8点阵屏4(IN) VCC <------> VCC GND <------> GND DOUT <------> DIN CS <------> CS CLK <------> CLK
程序实现代码
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <Max72xxPanel.h> //MAX7219点阵屏库文件
- uint8_t LEDArray[8];
- volatile int item; //定义一个整型变量item
- Max72xxPanel myMatrix = Max72xxPanel(9,4,1); //CS=9,水平点阵屏为4和竖直点阵屏为1
- const uint8_t A[8] PROGMEM ={0xc0,0xc0,0x00,0x00,0x00,0x00,0x00,0x00};
- const uint8_t B[8] PROGMEM ={0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00};
- const uint8_t C[8] PROGMEM ={0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0xc0};
- const uint8_t D[8] PROGMEM ={0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03};
- void my_1() { //函数1显示骰子数字1
- memcpy_P (&LEDArray, &A, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=0*8; index_j<0*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &B, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=1*8; index_j<1*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &C, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=2*8; index_j<2*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &D, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=3*8; index_j<3*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- }
- const uint8_t E[8] PROGMEM ={0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00};
- const uint8_t F[8] PROGMEM ={0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00};
- const uint8_t G[8] PROGMEM ={0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00};
- const uint8_t H[8] PROGMEM ={0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00};
- void my_2() { //函数2显示骰子数字2
- memcpy_P (&LEDArray, &E, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=0*8; index_j<0*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &F, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=1*8; index_j<1*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &G, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=2*8; index_j<2*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &H, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=3*8; index_j<3*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- }
- const uint8_t I[8] PROGMEM ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- const uint8_t J[8] PROGMEM ={0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00};
- const uint8_t K[8] PROGMEM ={0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00};
- const uint8_t L[8] PROGMEM ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- void my_3() { //函数3显示骰子数字3
- memcpy_P (&LEDArray, &I, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=0*8; index_j<0*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &J, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=1*8; index_j<1*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &K, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=2*8; index_j<2*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &L, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=3*8; index_j<3*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- }
- const uint8_t M[8] PROGMEM ={0x00,0x00,0x00,0x3c,0x3c,0x3c,0x00,0x00};
- const uint8_t N[8] PROGMEM ={0x00,0x00,0x00,0x3c,0x3c,0x3c,0x00,0x00};
- const uint8_t O[8] PROGMEM ={0x00,0x00,0x00,0x3c,0x3c,0x3c,0x00,0x00};
- const uint8_t P[8] PROGMEM ={0x00,0x00,0x00,0x3c,0x3c,0x3c,0x00,0x00};
- void my_4() { //函数4显示骰子数字4
- memcpy_P (&LEDArray, &M, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=0*8; index_j<0*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &N, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=1*8; index_j<1*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &O, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=2*8; index_j<2*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &P, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=3*8; index_j<3*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- }
- const uint8_t Q[8] PROGMEM ={0xc0,0xc0,0x00,0x3c,0x3c,0x3c,0x00,0x00};
- const uint8_t R[8] PROGMEM ={0x03,0x03,0x00,0x3c,0x3c,0x3c,0x00,0x00};
- const uint8_t S[8] PROGMEM ={0x00,0x00,0x00,0x3c,0x3c,0x3c,0xc0,0xc0};
- const uint8_t T[8] PROGMEM ={0x00,0x00,0x00,0x3c,0x3c,0x3c,0x03,0x03};
- void my_5() { //函数5显示骰子数字5
- memcpy_P (&LEDArray, &Q, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=0*8; index_j<0*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &R, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=1*8; index_j<1*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &S, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=2*8; index_j<2*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &T, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=3*8; index_j<3*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- }
- const uint8_t U[8] PROGMEM ={0x60,0x00,0x00,0x00,0x60,0x60,0x00,0x00};
- const uint8_t V[8] PROGMEM ={0x06,0x00,0x00,0x00,0x06,0x06,0x00,0x00};
- const uint8_t W[8] PROGMEM ={0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x60};
- const uint8_t X[8] PROGMEM ={0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x06};
- void my_6() { //函数6显示骰子数字6
- memcpy_P (&LEDArray, &U, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=0*8; index_j<0*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &V, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=1*8; index_j<1*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &W, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=2*8; index_j<2*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- memcpy_P (&LEDArray, &X, 8);
- for(int index_i=0; index_i<8; index_i++)
- {
- for(int index_j=3*8; index_j<3*8+8; index_j++)
- {
- if((LEDArray[index_i]&0x01)>0)
- myMatrix.drawPixel(index_j, 7-index_i,1);
- else
- myMatrix.drawPixel(index_j, 7-index_i,0);
- LEDArray[index_i] = LEDArray[index_i]>>1;
- }
- }
- myMatrix.write();
- }
- void setup(){
- item = 0; //item赋值为0
- myMatrix.fillScreen(0); //初始化点阵屏为全灭
- myMatrix.write();
- pinMode(3, INPUT); //将3号引脚设置为输入
- }
- void loop(){
- myMatrix.setIntensity(20); //设置MAX7219点阵屏亮度为20
- myMatrix.setRotation(0,1); //将MAX7219点阵屏屏幕序号0的角度旋转270度,根据实际情况调整
- myMatrix.setRotation(1,1); //将MAX7219点阵屏屏幕序号0的角度旋转270度,根据实际情况调整
- myMatrix.setRotation(2,1); //将MAX7219点阵屏屏幕序号0的角度旋转270度,根据实际情况调整
- myMatrix.setRotation(3,1); //将MAX7219点阵屏屏幕序号0的角度旋转270度,根据实际情况调整
- if (digitalRead(3) == HIGH) { //判断如果3号引脚为高电平,即筛子被摇动
- delay(1000); //延迟等待1秒
- item = random(1, 6); //变量item赋值为1-6的随机数
- if (item == 1) { //如果item为1,则执行函数my_1(),即显示数字1
- my_1();
- } else if (item == 2) { //如果item为2,则执行函数my_2(),即显示数字2
- my_2();
- } else if (item == 3) { //如果item为3,则执行函数my_3(),即显示数字3
- my_3();
- } else if (item == 4) { //如果item为4,则执行函数my_4(),即显示数字4
- my_4();
- } else if (item == 5) { //如果item为5,则执行函数 my_5(),即显示数字5
- my_5();
- } else if (item == 6) { //如果item为6,则执行函数 my_6(),即显示数字6
- my_6();
- }
- }
- }
复制代码 |