Touchless Electronic Dice Using Arduino
Created a touchless Covid Free Electronic Dice using Arduino to play some board games with my son.
Project description
Code
Code
arduino
1#include "LedControl.h" // Include library for the 8x8 LED matrix 2 3// Pins of the 8x8 LED Matrix 4int PinCLK = 7; 5int PinCS = 6; 6int PinDIN = 5; 7int IRSensor = 2; // Connect the IR sensor to arduino pin 2 8 9LedControl lc = LedControl(PinDIN, PinCLK, PinCS, 1); 10int statusSensor = 0; 11 12// Coordinates of the Dice points in the LED matrix 13int DicePic[8][6][2] = 14{ 15 { // Matrix und Start Position: 16 {9,9}, {9,8}, {9,7}, {9,6}, {9,5}, {9,4} 17 }, 18 { //1: 19 {4,4}, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1} 20 }, 21 { //2: 22 {2,2}, {6,6}, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1} 23 }, 24 { //3: 25 {2,6}, {6,2}, {4,4}, {-1,-1}, {-1,-1}, {-1,-1} 26 }, 27 { //4: 28 {2,2}, {2,6}, {6,2}, {6,6}, {-1,-1}, {-1,-1} 29 }, 30 { //5: 31 {2,2}, {2,6}, {6,2}, {6,6}, {4,4}, {-1,-1} 32 }, 33 { //6: 34 {2,1}, {2,4}, {2,7}, {6,1}, {6,4}, {6,7} 35 }, 36 { //Start: 37 {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1} 38 } 39}; 40 41 42// Variables of the dice: position, direction, speed for X and Y 43float DiceXpos[6]; 44float DiceXdir[6]; 45volatile byte DiceXspeed[6]; 46float DiceYpos[6]; 47float DiceYdir[6]; 48volatile byte DiceYspeed[6]; 49int DiceValue; 50unsigned long timestamp; 51 52void ShowLed(int x, int y, bool onoff) { 53// Show only, when x/y in matrix 54 if ((x<8) and (y<8) and (x>=0) and (y>=0)) { 55 lc.setLed(0, x, y, onoff); 56 }; 57}; 58 59 60void ShowDot(int x, int y, bool onoff) { 61 // Show or hide dice point 62 ShowLed(x-1, y-1, onoff); 63 ShowLed(x, y-1, onoff); 64 ShowLed(x-1, y, onoff); 65 ShowLed(x, y, onoff); 66}; 67 68 69// Show the number on the dice 70void ShowDicePic(int value) { 71 boolean done; 72 // Move all points from current position to destination of DiceValue 73 for (int i = 0; i < 6; i++) { 74 DiceXspeed[i] = 100; 75 DiceYspeed[i] = 100; 76 77 // Calc x values 78 DiceXdir[i] = 0; 79 if (int(DiceXpos[i])>DicePic[value][i][0]) {DiceXdir[i]=-1;} 80 else if (int(DiceXpos[i])<DicePic[value][i][0]) {DiceXdir[i]=1;} 81 82 DiceYdir[i] = 0; 83 if (int(DiceYpos[i])>DicePic[value][i][1]) {DiceYdir[i]=-1;} 84 else if (int(DiceYpos[i])<DicePic[value][i][1]) {DiceYdir[i]=1;} 85 }; 86 87 do { 88 // Serial.println("Moving"); 89 for (int i = 0; i < 6; i++) { 90 if (int(DiceXpos[i])!=DicePic[value][i][0]) { 91 DoStep(DiceXpos[i],DiceXdir[i],DiceXspeed[i],false); 92 }; 93 if (int(DiceYpos[i])!=DicePic[value][i][1]) { 94 DoStep(DiceYpos[i],DiceYdir[i],DiceYspeed[i],false); 95 }; 96 }; 97 98 lc.clearDisplay(0); 99 for (int i = 0; i < 6; i++) { 100 ShowDot(int(DiceXpos[i]), int(DiceYpos[i]), true); 101 }; 102 103 delay(50); 104 105 // Dice points are on destition position 106 done=true; 107 for (int i = 0; i < 6; i++) { 108 if (int(DiceXpos[i])!=DicePic[value][i][0]) {done=false;} 109 if (int(DiceYpos[i])!=DicePic[value][i][1]) {done=false;} 110 } 111 112 } while (done==false); 113 //Serial.println("End moving"); 114 115 lc.clearDisplay(0); 116 for (int i = 0; i < 6; i++) { 117 ShowDot(DicePic[value][i][0], DicePic[value][i][1], true); 118 }; 119}; 120 121 122void DoStep(float &pos, float &dir, volatile byte &sp, bool check) { 123 pos = pos + float(sp)/255*dir; 124 125 if (check==true) { 126 if (pos>7) { 127 pos = 7; 128 dir = dir*(-1); 129 }; 130 if (pos<1) { 131 pos = 1; 132 dir = dir*(-1); 133 }; 134 }; 135 // Velocity decreases every step 136 if (sp>0) {sp=sp-1;} 137}; 138 139 140void setup() { 141 timestamp = millis(); 142 Serial.begin(9600); 143 // The MAX7219 is in power-saving mode on startup. So, we need to send a wakeup call 144 lc.shutdown(0, false); 145 lc.setIntensity(0, 8); // Set the brightness to a medium values 146 lc.clearDisplay(0); // Then clear the display 147 148 // Innitial animation 149 for (int i = 0; i < 6; i++) { 150 DiceXpos[i] = DicePic[7][i][0]; 151 DiceYpos[i] = DicePic[7][i][1]; 152 153 DiceXdir[i] = random(3)-1; 154 DiceYdir[i] = random(3)-1; 155 DiceXspeed[i] = random(126)+120; 156 DiceYspeed[i] = random(126)+120; 157 }; 158 159 lc.clearDisplay(0); // Clear the animation 160 ShowDicePic(6); // Display number 6 on startup 161 delay(1000); 162 163 pinMode (IRSensor, INPUT); // Sensor pin INPUT 164}; 165 166 167void loop() { 168 delay(50); 169 statusSensor = digitalRead(IRSensor); // Read the data from the IR Sensor 170 Serial.println(statusSensor); 171 172 if (statusSensor == 1){ 173 if (millis()-timestamp>2000) { 174 DiceValue = random(6)+1; // Generate a number between 1 and 6 175 ShowDicePic(DiceValue); 176 }; 177 }; 178}; 179
Comments
Only logged in users can leave comments