Components and supplies
1
Breadboard (generic)
1
8x8 LED Matrix
1
Arduino Nano R3
1
Touch Sensor
1
Jumper wires (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Code
arduino
1/* 2 This program simulates a dice by generating a randome number 3 between 4 1-6. The electronic dice can 5 be used with some board games that 6 require a dice to play. 7 A touch sensor is interfaced with arduino, and on 8 every touch, 9 a random number between 1-6 is generated and 10 displayed 11 on an LED Matrix. 12 This program is made by Shreyas for 13 Electronics Champ 14 YouTube Channel. 15 Please subscribe to this channel 16 Thank You 17*/ 18 19//Including 20 the library 21#include <LedControl.h> 22 23int DIN = 10; 24int CS = 9; 25int 26 CLK = 8; 27int sensor = 7; 28int randomNumber; 29String patternNumber = "0"; 30 31/* 32 33 Set the DIN (Data in) pin 34 Set the CLK (Clock) pin 35 Set the CS (Load 36 pulse) pin 37 Set the number of displays being used 38*/ 39LedControl matrix 40 = LedControl(DIN, CLK, CS, 1); 41 42void setup() { 43 44 /* 45 The Matrix 46 is in power-saving mode on startup, 47 we have to do a wakeup call 48 */ 49 50 matrix.shutdown(0, false); 51 matrix.setIntensity(0, 8); //Adjust the brightness; 52 maximum is 15 53 randomSeed(analogRead(A0)); //Read from unconnected analog 54 pin A0 55 pinMode(sensor, INPUT); //Set pin 7 as input 56 57 while 58 (digitalRead(sensor) == LOW) { //This loop is used to display the pattern on startup 59 60 61 displayPattern(); //function to dispaly a pattern on the display 62 continue; 63 64 65 } 66 67} 68 69void loop() { 70 71 if (digitalRead(sensor) == HIGH) { //If 72 the sensor is touched 73 74 randomNumber = random(1, 7); //Chooses a random 75 number between 1 and 6 76 displayNumber(); //A function to display 77 the number on the display 78 79 } 80 81} 82 83//This function is used to 84 set the position of the individual LEDs in the matrix 85void setBit(byte pos[]) 86 { 87 88 int i = 0; 89 90 for (i = 0; i < 8; i++) { 91 92 matrix.setRow(0, 93 i, pos[i]); 94 95 } 96 97} 98 99void displayNumber() { 100 //binary codes 101 for individual LEDs to represent 1 to 6 of the dice 102 byte one[8] = { B00000000, 103 104 B00000000, 105 B00000000, 106 B00011000, 107 108 B00011000, 109 B00000000, 110 B00000000, 111 112 B00000000 113 }; 114 115 byte two[8] = { B00000000, 116 117 B00000000, 118 B00110000, 119 B00110000, 120 121 B00001100, 122 B00001100, 123 B00000000, 124 125 B00000000 126 }; 127 128 byte three[8] = { B00000000, 129 130 B01100000, 131 B01100000, 132 B00011000, 133 134 B00011000, 135 B00000110, 136 B00000110, 137 138 B00000000 139 }; 140 141 byte four[8] = 142 { B00000000, 143 B01100110, 144 B01100110, 145 146 B00000000, 147 B00000000, 148 B01100110, 149 150 B01100110, 151 B00000000 152 }; 153 154 155 byte five[8] = { B00000000, 156 B01100110, 157 B01100110, 158 159 B00011000, 160 B00011000, 161 B01100110, 162 163 B01100110, 164 B00000000 165 }; 166 167 168 byte six[8] = { B00000000, 169 B11011011, 170 B11011011, 171 172 B00000000, 173 B00000000, 174 B11011011, 175 176 B11011011, 177 B00000000 178 }; 179 180 181 if (randomNumber == 1) { //If the number is 1... 182 183 displayPattern(); 184 //Displays the pattern 185 setBit(one); //Displays 186 the number 187 188 } 189 190 else if (randomNumber == 2) { //If the number is 191 2... 192 193 displayPattern(); //Displays the pattern 194 setBit(two); 195 //Displays the number 196 197 } 198 199 else if (randomNumber 200 == 3) { //If the number is 3... 201 202 displayPattern(); //Displays 203 the pattern 204 setBit(three); //Displays the number 205 206 } 207 208 209 else if (randomNumber == 4) { //If the number is 4... 210 211 displayPattern(); 212 //Displays the pattern 213 setBit(four); //Displays 214 the number 215 216 } 217 218 else if (randomNumber == 5) { //If the number is 219 5... 220 221 displayPattern(); //Displays the pattern 222 setBit(five); 223 //Displays the number 224 225 } 226 227 else if (randomNumber 228 == 6) { //If the number is 6... 229 230 displayPattern(); //Displays 231 the pattern 232 setBit(six); //Displays the number 233 234 } 235 236} 237 238void 239 displayPattern() { 240 //binary code for a pattern 241 byte pattern1[8] { B11111111, 242 243 B10000001, 244 B10000001, 245 B10000001, 246 247 B10000001, 248 B10000001, 249 B10000001, 250 251 B11111111 252 253 }; 254 255 byte pattern2[8] 256 { B00000000, 257 B01111110, 258 B01000010, 259 260 B01000010, 261 B01000010, 262 B01000010, 263 264 B01111110, 265 B00000000 266 267 }; 268 269 270 byte pattern3[8] { B00000000, 271 B00000000, 272 B00111100, 273 274 B00100100, 275 B00100100, 276 B00111100, 277 278 B00000000, 279 B00000000 280 281 }; 282 283 284 byte pattern4[8] { B00000000, 285 B00000000, 286 B00000000, 287 288 B00011000, 289 B00011000, 290 B00000000, 291 292 B00000000, 293 B00000000 294 295 }; 296 297 298 //To display the pattern 299 for (int i = 0; i < 3; i++) { 300 301 setBit(pattern1); 302 303 delay(50); 304 setBit(pattern2); 305 delay(50); 306 setBit(pattern3); 307 308 delay(50); 309 setBit(pattern4); 310 delay(50); 311 setBit(pattern4); 312 313 delay(50); 314 setBit(pattern3); 315 delay(50); 316 setBit(pattern2); 317 318 delay(50); 319 setBit(pattern1); 320 delay(50); 321 322 } 323 324}
Code
arduino
1/* 2 This program simulates a dice by generating a randome number between 3 1-6. The electronic dice can 4 be used with some board games that require a dice to play. 5 A touch sensor is interfaced with arduino, and on every touch, 6 a random number between 1-6 is generated and 7 displayed on an LED Matrix. 8 This program is made by Shreyas for 9 Electronics Champ YouTube Channel. 10 Please subscribe to this channel 11 Thank You 12*/ 13 14//Including the library 15#include <LedControl.h> 16 17int DIN = 10; 18int CS = 9; 19int CLK = 8; 20int sensor = 7; 21int randomNumber; 22String patternNumber = "0"; 23 24/* 25 Set the DIN (Data in) pin 26 Set the CLK (Clock) pin 27 Set the CS (Load pulse) pin 28 Set the number of displays being used 29*/ 30LedControl matrix = LedControl(DIN, CLK, CS, 1); 31 32void setup() { 33 34 /* 35 The Matrix is in power-saving mode on startup, 36 we have to do a wakeup call 37 */ 38 matrix.shutdown(0, false); 39 matrix.setIntensity(0, 8); //Adjust the brightness; maximum is 15 40 randomSeed(analogRead(A0)); //Read from unconnected analog pin A0 41 pinMode(sensor, INPUT); //Set pin 7 as input 42 43 while (digitalRead(sensor) == LOW) { //This loop is used to display the pattern on startup 44 45 displayPattern(); //function to dispaly a pattern on the display 46 continue; 47 48 } 49 50} 51 52void loop() { 53 54 if (digitalRead(sensor) == HIGH) { //If the sensor is touched 55 56 randomNumber = random(1, 7); //Chooses a random number between 1 and 6 57 displayNumber(); //A function to display the number on the display 58 59 } 60 61} 62 63//This function is used to set the position of the individual LEDs in the matrix 64void setBit(byte pos[]) { 65 66 int i = 0; 67 68 for (i = 0; i < 8; i++) { 69 70 matrix.setRow(0, i, pos[i]); 71 72 } 73 74} 75 76void displayNumber() { 77 //binary codes for individual LEDs to represent 1 to 6 of the dice 78 byte one[8] = { B00000000, 79 B00000000, 80 B00000000, 81 B00011000, 82 B00011000, 83 B00000000, 84 B00000000, 85 B00000000 86 }; 87 88 byte two[8] = { B00000000, 89 B00000000, 90 B00110000, 91 B00110000, 92 B00001100, 93 B00001100, 94 B00000000, 95 B00000000 96 }; 97 98 byte three[8] = { B00000000, 99 B01100000, 100 B01100000, 101 B00011000, 102 B00011000, 103 B00000110, 104 B00000110, 105 B00000000 106 }; 107 108 byte four[8] = { B00000000, 109 B01100110, 110 B01100110, 111 B00000000, 112 B00000000, 113 B01100110, 114 B01100110, 115 B00000000 116 }; 117 118 byte five[8] = { B00000000, 119 B01100110, 120 B01100110, 121 B00011000, 122 B00011000, 123 B01100110, 124 B01100110, 125 B00000000 126 }; 127 128 byte six[8] = { B00000000, 129 B11011011, 130 B11011011, 131 B00000000, 132 B00000000, 133 B11011011, 134 B11011011, 135 B00000000 136 }; 137 138 if (randomNumber == 1) { //If the number is 1... 139 140 displayPattern(); //Displays the pattern 141 setBit(one); //Displays the number 142 143 } 144 145 else if (randomNumber == 2) { //If the number is 2... 146 147 displayPattern(); //Displays the pattern 148 setBit(two); //Displays the number 149 150 } 151 152 else if (randomNumber == 3) { //If the number is 3... 153 154 displayPattern(); //Displays the pattern 155 setBit(three); //Displays the number 156 157 } 158 159 else if (randomNumber == 4) { //If the number is 4... 160 161 displayPattern(); //Displays the pattern 162 setBit(four); //Displays the number 163 164 } 165 166 else if (randomNumber == 5) { //If the number is 5... 167 168 displayPattern(); //Displays the pattern 169 setBit(five); //Displays the number 170 171 } 172 173 else if (randomNumber == 6) { //If the number is 6... 174 175 displayPattern(); //Displays the pattern 176 setBit(six); //Displays the number 177 178 } 179 180} 181 182void displayPattern() { 183 //binary code for a pattern 184 byte pattern1[8] { B11111111, 185 B10000001, 186 B10000001, 187 B10000001, 188 B10000001, 189 B10000001, 190 B10000001, 191 B11111111 192 193 }; 194 195 byte pattern2[8] { B00000000, 196 B01111110, 197 B01000010, 198 B01000010, 199 B01000010, 200 B01000010, 201 B01111110, 202 B00000000 203 204 }; 205 206 byte pattern3[8] { B00000000, 207 B00000000, 208 B00111100, 209 B00100100, 210 B00100100, 211 B00111100, 212 B00000000, 213 B00000000 214 215 }; 216 217 byte pattern4[8] { B00000000, 218 B00000000, 219 B00000000, 220 B00011000, 221 B00011000, 222 B00000000, 223 B00000000, 224 B00000000 225 226 }; 227 228 //To display the pattern 229 for (int i = 0; i < 3; i++) { 230 231 setBit(pattern1); 232 delay(50); 233 setBit(pattern2); 234 delay(50); 235 setBit(pattern3); 236 delay(50); 237 setBit(pattern4); 238 delay(50); 239 setBit(pattern4); 240 delay(50); 241 setBit(pattern3); 242 delay(50); 243 setBit(pattern2); 244 delay(50); 245 setBit(pattern1); 246 delay(50); 247 248 } 249 250}
Downloadable files
Schematic
Schematic

Comments
Only logged in users can leave comments