Shaking Dice
When we shake this box it give random number upto 6 digit number so, it behave like a dice it can be use as many ways as we use normal dice
Components and supplies
1
6 DOF Sensor - MPU6050
1
Breadboard (generic)
1
LED Dot Matrix Display, Red
1
Arduino Nano R3
1
Jumper wires (generic)
Tools and machines
1
Hot glue gun (generic)
Project description
Code
Shaking Dice
c_cpp
1/* 2Blog-Artikel: Schttel-Wrfel mit LED Matrix und Bewegungssimulation 3 4https://techpluscode.de/schuettel-wuerfel-mit-led-matrix-und-bewegungssimulation/ 5 6techpluscode.de 7Copyright 2019 von Thomas Angielsky 8*/ 9 10//Bibliothek fr die Ansteuerung der 8x8 LED-Matrix einbinden 11//Include lib for the 8x8 LED matrix 12#include "LedControl.h" 13#include "Wire.h" 14 15//int PinTiltX = 2; //Pin fr Tiltsensor X (if we use Tilt Sensor) 16//int PinTiltY = 3; //Pin fr Tiltsensor Y 17 18 19const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69. 20 21int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data 22int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data 23int16_t temperature; // variables for temperature data 24 25char tmp_str[7]; // temporary variable used in convert function 26 27char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor. 28 sprintf(tmp_str, "%6d", i); 29 return tmp_str; 30} 31 32//Pins der LED-Matrix 33//Pins of the LED matrix 34int PinCLK = 7; 35int PinCS = 6; 36int PinDIN = 5; 37 38LedControl lc = LedControl(PinDIN, PinCLK, PinCS, 1); 39 40//Koordinaten der Wrfelaugen in der LED-Matrix 41//Coordinates of the Dice points in the LED matrix 42int DicePic[8][6][2] = 43{ 44 { //leere Matrix und Startposition: 45 {9,9}, //1. Punkt 46 {9,8}, //2. Punkt 47 {9,7}, //3. Punkt 48 {9,6}, //4. Punkt 49 {9,5}, //5. Punkt 50 {9,4} //6. Punkt 51 }, 52 { //1: 53 {4,4}, //1. Punkt 54 {-1,-1}, //2. Punkt 55 {-1,-1}, //3. Punkt 56 {-1,-1}, //4. Punkt 57 {-1,-1}, //5. Punkt 58 {-1,-1} //6. Punkt 59 }, 60 { //2: 61 {2,2}, //1. Punkt 62 {6,6}, //2. Punkt 63 {-1,-1}, //3. Punkt 64 {-1,-1}, //4. Punkt 65 {-1,-1}, //5. Punkt 66 {-1,-1} //6. Punkt 67 }, 68 { //3: 69 {2,6}, //1. Punkt 70 {6,2}, //2. Punkt 71 {4,4}, //3. Punkt 72 {-1,-1}, //4. Punkt 73 {-1,-1}, //5. Punkt 74 {-1,-1} //6. Punkt 75 }, 76 { //4: 77 {2,2}, //1. Punkt 78 {2,6}, //2. Punkt 79 {6,2}, //3. Punkt 80 {6,6}, //4. Punkt 81 {-1,-1}, //5. Punkt 82 {-1,-1} //6. Punkt 83 }, 84 { //5: 85 {2,2}, //1. Punkt 86 {2,6}, //2. Punkt 87 {6,2}, //3. Punkt 88 {6,6}, //4. Punkt 89 {4,4}, //5. Punkt 90 {-1,-1} //6. Punkt 91 }, 92 { //6: 93 {2,1}, //1. Punkt 94 {2,4}, //2. Punkt 95 {2,7}, //3. Punkt 96 {6,1}, //4. Punkt 97 {6,4}, //5. Punkt 98 {6,7} //6. Punkt 99 }, 100 { //Start: 101 {-1,-1}, //1. Punkt 102 {-1,-1}, //2. Punkt 103 {-1,-1}, //3. Punkt 104 {-1,-1}, //4. Punkt 105 {-1,-1}, //5. Punkt 106 {-1,-1} //6. Punkt 107 } 108 }; 109 110 111//Variablen der Wrfelaugen: Position, Richtung, Geschwindigkeit fr X und Y 112//Variables of the dice: position, direction, speed for X and Y 113float DiceXpos[6]; 114float DiceXdir[6]; 115volatile byte DiceXspeed[6]; 116float DiceYpos[6]; 117float DiceYdir[6]; 118volatile byte DiceYspeed[6]; 119 120 121int DiceValue; 122unsigned long timestamp; 123byte Mode; 124int volatile shakes; 125int ShakesPerSecond; 126int step; 127 128void InterruptChecks() { 129 //Schttel-Anzahl zhlen 130 //Count Shakes 131 shakes=shakes+1; 132 //Serial.println(millis()); 133 timestamp=millis(); 134} 135 136void SetSpeedX() { 137 if (Mode==0) { 138 //alle Wrfel in X beschleunigen 139 //Speed-up dice in X 140 for (int i = 0; i < 6; i++) { 141 if (DiceXspeed[i]<255) {DiceXspeed[i]=DiceXspeed[i]+5;} 142 } 143 } 144 InterruptChecks(); 145} 146 147void SetSpeedY() { 148 if (Mode==0) { 149 //alle Wrfel in Y beschleunigen 150 //Speed-up dice in Y 151 for (int i = 0; i < 6; i++) { 152 if (DiceYspeed[i]<255) {DiceYspeed[i]=DiceYspeed[i]+5;} 153 } 154 } 155 InterruptChecks(); 156} 157 158void ShowLed(int x, int y, bool onoff) { 159//LED nur anzeigen, wenn im sichtbaren Bereich 160//show only, when x/y in matrix 161 if ((x<8) and (y<8) and (x>=0) and (y>=0)) { 162 lc.setLed(0, x, y, onoff); 163 } 164} 165 166void ShowDot(int x, int y, bool onoff) { 167//Wrfel-Auge anzeigen oder ausblenden 168//Show or hide dice point 169 ShowLed(x-1, y-1, onoff); 170 ShowLed(x, y-1, onoff); 171 ShowLed(x-1, y, onoff); 172 ShowLed(x, y, onoff); 173} 174 175void ShowDicePic(int value) { 176//Wurf anzeigen 177//Show dice 178 179boolean done; 180 181 //alle Punkte von der aktuellen Position aus zur Zielposition von DiceValue bewegen 182 //move all points from current position to destination of DiceValue 183 for (int i = 0; i < 6; i++) { 184 DiceXspeed[i]=100; 185 DiceYspeed[i]=100; 186 187 //Werte fr X berechnen 188 //Calc x values 189 DiceXdir[i]=0; 190 if (int(DiceXpos[i])>DicePic[value][i][0]) {DiceXdir[i]=-1;} 191 else if (int(DiceXpos[i])<DicePic[value][i][0]) {DiceXdir[i]=1;} 192 193 DiceYdir[i]=0; 194 if (int(DiceYpos[i])>DicePic[value][i][1]) {DiceYdir[i]=-1;} 195 else if (int(DiceYpos[i])<DicePic[value][i][1]) {DiceYdir[i]=1;} 196 } 197 198 //Serial.println(value); 199 //Serial.println("Bewegung Start // Start moving"); 200 //Punkte bewegen 201 do { 202 //Serial.println("Bewegung // Moving"); 203 for (int i = 0; i < 6; i++) { 204 if (int(DiceXpos[i])!=DicePic[value][i][0]) { 205 DoStep(DiceXpos[i],DiceXdir[i],DiceXspeed[i],false); 206 } 207 if (int(DiceYpos[i])!=DicePic[value][i][1]) { 208 DoStep(DiceYpos[i],DiceYdir[i],DiceYspeed[i],false); 209 } 210 } 211 212 lc.clearDisplay(0); 213 for (int i = 0; i < 6; i++) { 214 ShowDot(int(DiceXpos[i]), int(DiceYpos[i]), true); 215 } 216 217 delay(50); 218 219 //Sind alle Augen an ihrer Zielposition 220 //Dice points are on destition position 221 done=true; 222 for (int i = 0; i < 6; i++) { 223 if (int(DiceXpos[i])!=DicePic[value][i][0]) {done=false;} 224 if (int(DiceYpos[i])!=DicePic[value][i][1]) {done=false;} 225 } 226 227 } while (done==false); 228 //Serial.println("Bewegung Ende // End moving"); 229 230 lc.clearDisplay(0); 231 for (int i = 0; i < 6; i++) { 232 ShowDot(DicePic[value][i][0],DicePic[value][i][1], true); 233 } 234} 235 236 237void DoStep(float &pos, float &dir, volatile byte &sp, bool check) { 238 pos=pos+float(sp)/255*dir; 239 240 if (check==true) { 241 if (pos>7) { 242 pos=7; 243 dir=dir*(-1); 244 } 245 if (pos<1) { 246 pos=1; 247 dir=dir*(-1); 248 } 249 250 } 251 // Geschwindigkeit wird pro Schritt langsamer 252 // Velocity decreases every step 253 if (sp>0) {sp=sp-1;} 254} 255 256void MoveDots() { 257 //alle Wrfel einen Schritt weiter bewegen 258 //move dice points one step further 259 for (int i = 0; i < 6; i++) { 260 //neue Koordinaten berechnen 261 //calc new coordinates 262 DoStep(DiceXpos[i],DiceXdir[i],DiceXspeed[i],true); 263 DoStep(DiceYpos[i],DiceYdir[i],DiceYspeed[i],true); 264 } 265 266 //Wrfel-Augen anzeigen 267 //show dice points 268 lc.clearDisplay(0); 269 for (int i = 0; i < 6; i++) { 270 ShowDot(int(DiceXpos[i]), int(DiceYpos[i]), true); 271 } 272 273} 274 275 276 277void setup() { 278 Serial.begin(9600); 279 Wire.begin(); 280 Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board) 281 Wire.write(0x6B); // PWR_MGMT_1 register 282 Wire.write(0); // set to zero (wakes up the MPU-6050) 283 Wire.endTransmission(true); 284 //Der MAX7219 ist beim Starten im Power-Saving Modus, 285 //er muss aufgeweckt werden. 286 //The MAX7219 is in power-saving mode on startup, 287 //we have to do a wakeup call 288 lc.shutdown(0, false); 289 //Helligkeit auf einen Mittelwert 290 //Set the brightness to a medium values 291 lc.setIntensity(0, 8); 292 //und Display lschen 293 //and clear the display 294 lc.clearDisplay(0); 295 296 randomSeed(analogRead(0)); 297 DiceValue=0; 298 299 for (int i = 0; i < 6; i++) { 300 DiceXpos[i]=DicePic[7][i][0]; 301 DiceYpos[i]=DicePic[7][i][1]; 302 303 DiceXdir[i]=random(3)-1; 304 DiceYdir[i]=random(3)-1; 305 DiceXspeed[i]=random(126)+120; 306 DiceYspeed[i]=random(126)+120; 307 } 308 309 //Pins einstellen 310 //Setup the pins 311// pinMode(PinTiltX, INPUT_PULLUP); (for Tilt Sensor) 312 // pinMode(PinTiltY, INPUT_PULLUP); 313// attachInterrupt(digitalPinToInterrupt(PinTiltX),SetSpeedX,CHANGE); 314 // attachInterrupt(digitalPinToInterrupt(PinTiltY),SetSpeedY,CHANGE); 315 316 lc.clearDisplay(0); 317 318 timestamp=millis(); 319 Mode=1; 320 321 ShowDicePic(6); 322 delay(1000); 323 324 lc.clearDisplay(0); 325 Mode=0; 326 Serial.begin(9600); 327 step=0; 328 shakes=0; 329} 330 331void loop() { 332 333Wire.beginTransmission(MPU_ADDR); 334 Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40] 335 Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active. 336 Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers 337 gyro_z = Wire.read()<<8 | Wire.read(); 338 accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L) 339Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z)); 340 //Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z)); 341 342 Serial.println(); 343 delay(1000); 344 345 delay(50); 346 step=step+1; 347 if (step>20) { 348 //1 sek ist vorbei 349 //1 sec is over 350 step=0; 351 ShakesPerSecond=shakes; 352 shakes=0; 353 } 354 355 if(gyro_z>7500||gyro_z<-3000) 356 { 357 MoveDots(); 358 if (millis()-timestamp>2000) { 359 //seit 2 sek kein Schtteln mehr 360 //there is no shaking since 2 sec 361 Mode=1; 362 DiceValue=random(6)+1; 363 ShowDicePic(DiceValue); 364 } 365 366 367 if (ShakesPerSecond>5) { 368 //Es wird wieder geschttelt 369 //shaking again 370 Mode=0; 371 } 372 } 373 374} 375
Shaking Dice
c_cpp
1/* 2Blog-Artikel: Schttel-Wrfel mit LED Matrix und Bewegungssimulation 3 4https://techpluscode.de/schuettel-wuerfel-mit-led-matrix-und-bewegungssimulation/ 5 6techpluscode.de 7Copyright 8 2019 von Thomas Angielsky 9*/ 10 11//Bibliothek fr die Ansteuerung der 8x8 LED-Matrix 12 einbinden 13//Include lib for the 8x8 LED matrix 14#include "LedControl.h" 15#include 16 "Wire.h" 17 18//int PinTiltX = 2; //Pin fr Tiltsensor X (if we use Tilt Sensor) 19//int 20 PinTiltY = 3; //Pin fr Tiltsensor Y 21 22 23const int MPU_ADDR = 0x68; // I2C 24 address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69. 25 26int16_t 27 accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer 28 raw data 29int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data 30int16_t 31 temperature; // variables for temperature data 32 33char tmp_str[7]; // temporary 34 variable used in convert function 35 36char* convert_int16_to_str(int16_t i) { 37 // converts int16 to string. Moreover, resulting strings will have the same length 38 in the debug monitor. 39 sprintf(tmp_str, "%6d", i); 40 return tmp_str; 41} 42 43//Pins 44 der LED-Matrix 45//Pins of the LED matrix 46int PinCLK = 7; 47int PinCS = 6; 48int 49 PinDIN = 5; 50 51LedControl lc = LedControl(PinDIN, PinCLK, PinCS, 1); 52 53//Koordinaten 54 der Wrfelaugen in der LED-Matrix 55//Coordinates of the Dice points in the LED 56 matrix 57int DicePic[8][6][2] = 58{ 59 { //leere Matrix und Startposition: 60 61 {9,9}, //1. Punkt 62 {9,8}, //2. Punkt 63 {9,7}, //3. Punkt 64 {9,6}, 65 //4. Punkt 66 {9,5}, //5. Punkt 67 {9,4} //6. Punkt 68 }, 69 { //1: 70 71 {4,4}, //1. Punkt 72 {-1,-1}, //2. Punkt 73 {-1,-1}, //3. Punkt 74 75 {-1,-1}, //4. Punkt 76 {-1,-1}, //5. Punkt 77 {-1,-1} //6. Punkt 78 79 }, 80 { //2: 81 {2,2}, //1. Punkt 82 {6,6}, //2. Punkt 83 {-1,-1}, 84 //3. Punkt 85 {-1,-1}, //4. Punkt 86 {-1,-1}, //5. Punkt 87 {-1,-1} 88 //6. Punkt 89 }, 90 { //3: 91 {2,6}, //1. Punkt 92 {6,2}, //2. Punkt 93 94 {4,4}, //3. Punkt 95 {-1,-1}, //4. Punkt 96 {-1,-1}, //5. Punkt 97 98 {-1,-1} //6. Punkt 99 }, 100 { //4: 101 {2,2}, //1. Punkt 102 {2,6}, 103 //2. Punkt 104 {6,2}, //3. Punkt 105 {6,6}, //4. Punkt 106 {-1,-1}, //5. 107 Punkt 108 {-1,-1} //6. Punkt 109 }, 110 { //5: 111 {2,2}, //1. Punkt 112 113 {2,6}, //2. Punkt 114 {6,2}, //3. Punkt 115 {6,6}, //4. Punkt 116 {4,4}, 117 //5. Punkt 118 {-1,-1} //6. Punkt 119 }, 120 { //6: 121 {2,1}, //1. Punkt 122 123 {2,4}, //2. Punkt 124 {2,7}, //3. Punkt 125 {6,1}, //4. Punkt 126 {6,4}, 127 //5. Punkt 128 {6,7} //6. Punkt 129 }, 130 { //Start: 131 {-1,-1}, //1. 132 Punkt 133 {-1,-1}, //2. Punkt 134 {-1,-1}, //3. Punkt 135 {-1,-1}, //4. 136 Punkt 137 {-1,-1}, //5. Punkt 138 {-1,-1} //6. Punkt 139 } 140 }; 141 142 143//Variablen 144 der Wrfelaugen: Position, Richtung, Geschwindigkeit fr X und Y 145//Variables of 146 the dice: position, direction, speed for X and Y 147float DiceXpos[6]; 148float 149 DiceXdir[6]; 150volatile byte DiceXspeed[6]; 151float DiceYpos[6]; 152float DiceYdir[6]; 153volatile 154 byte DiceYspeed[6]; 155 156 157int DiceValue; 158unsigned long timestamp; 159byte 160 Mode; 161int volatile shakes; 162int ShakesPerSecond; 163int step; 164 165void InterruptChecks() 166 { 167 //Schttel-Anzahl zhlen 168 //Count Shakes 169 shakes=shakes+1; 170 //Serial.println(millis()); 171 172 timestamp=millis(); 173} 174 175void SetSpeedX() { 176 if (Mode==0) { 177 //alle 178 Wrfel in X beschleunigen 179 //Speed-up dice in X 180 for (int i = 0; i < 181 6; i++) { 182 if (DiceXspeed[i]<255) {DiceXspeed[i]=DiceXspeed[i]+5;} 183 } 184 185 } 186 InterruptChecks(); 187} 188 189void SetSpeedY() { 190 if (Mode==0) { 191 192 //alle Wrfel in Y beschleunigen 193 //Speed-up dice in Y 194 for (int 195 i = 0; i < 6; i++) { 196 if (DiceYspeed[i]<255) {DiceYspeed[i]=DiceYspeed[i]+5;} 197 198 } 199 } 200 InterruptChecks(); 201} 202 203void ShowLed(int x, int y, bool 204 onoff) { 205//LED nur anzeigen, wenn im sichtbaren Bereich 206//show only, when 207 x/y in matrix 208 if ((x<8) and (y<8) and (x>=0) and (y>=0)) { 209 lc.setLed(0, 210 x, y, onoff); 211 } 212} 213 214void ShowDot(int x, int y, bool onoff) { 215//Wrfel-Auge 216 anzeigen oder ausblenden 217//Show or hide dice point 218 ShowLed(x-1, y-1, onoff); 219 220 ShowLed(x, y-1, onoff); 221 ShowLed(x-1, y, onoff); 222 ShowLed(x, y, onoff); 223} 224 225void 226 ShowDicePic(int value) { 227//Wurf anzeigen 228//Show dice 229 230boolean done; 231 232 233 //alle Punkte von der aktuellen Position aus zur Zielposition von DiceValue bewegen 234 235 //move all points from current position to destination of DiceValue 236 for (int 237 i = 0; i < 6; i++) { 238 DiceXspeed[i]=100; 239 DiceYspeed[i]=100; 240 241 242 //Werte fr X berechnen 243 //Calc x values 244 DiceXdir[i]=0; 245 if 246 (int(DiceXpos[i])>DicePic[value][i][0]) {DiceXdir[i]=-1;} 247 else if (int(DiceXpos[i])<DicePic[value][i][0]) 248 {DiceXdir[i]=1;} 249 250 DiceYdir[i]=0; 251 if (int(DiceYpos[i])>DicePic[value][i][1]) 252 {DiceYdir[i]=-1;} 253 else if (int(DiceYpos[i])<DicePic[value][i][1]) {DiceYdir[i]=1;} 254 255 } 256 257 //Serial.println(value); 258 //Serial.println("Bewegung Start 259 // Start moving"); 260 //Punkte bewegen 261 do { 262 //Serial.println("Bewegung 263 // Moving"); 264 for (int i = 0; i < 6; i++) { 265 if (int(DiceXpos[i])!=DicePic[value][i][0]) 266 { 267 DoStep(DiceXpos[i],DiceXdir[i],DiceXspeed[i],false); 268 } 269 270 if (int(DiceYpos[i])!=DicePic[value][i][1]) { 271 DoStep(DiceYpos[i],DiceYdir[i],DiceYspeed[i],false); 272 273 } 274 } 275 276 lc.clearDisplay(0); 277 for (int i = 0; i < 6; i++) 278 { 279 ShowDot(int(DiceXpos[i]), int(DiceYpos[i]), true); 280 } 281 282 283 delay(50); 284 285 //Sind alle Augen an ihrer Zielposition 286 //Dice 287 points are on destition position 288 done=true; 289 for (int i = 0; i < 6; 290 i++) { 291 if (int(DiceXpos[i])!=DicePic[value][i][0]) {done=false;} 292 if 293 (int(DiceYpos[i])!=DicePic[value][i][1]) {done=false;} 294 } 295 296 } while 297 (done==false); 298 //Serial.println("Bewegung Ende // End moving"); 299 300 lc.clearDisplay(0); 301 302 for (int i = 0; i < 6; i++) { 303 ShowDot(DicePic[value][i][0],DicePic[value][i][1], 304 true); 305 } 306} 307 308 309void DoStep(float &pos, float &dir, volatile byte 310 &sp, bool check) { 311 pos=pos+float(sp)/255*dir; 312 313 if (check==true) { 314 315 if (pos>7) { 316 pos=7; 317 dir=dir*(-1); 318 } 319 if 320 (pos<1) { 321 pos=1; 322 dir=dir*(-1); 323 } 324 325 } 326 // 327 Geschwindigkeit wird pro Schritt langsamer 328 // Velocity decreases every step 329 330 if (sp>0) {sp=sp-1;} 331} 332 333void MoveDots() { 334 //alle Wrfel einen Schritt 335 weiter bewegen 336 //move dice points one step further 337 for (int i = 0; i < 338 6; i++) { 339 //neue Koordinaten berechnen 340 //calc new coordinates 341 342 DoStep(DiceXpos[i],DiceXdir[i],DiceXspeed[i],true); 343 DoStep(DiceYpos[i],DiceYdir[i],DiceYspeed[i],true); 344 345 } 346 347 //Wrfel-Augen anzeigen 348 //show dice points 349 lc.clearDisplay(0); 350 351 for (int i = 0; i < 6; i++) { 352 ShowDot(int(DiceXpos[i]), int(DiceYpos[i]), 353 true); 354 } 355 356} 357 358 359 360void setup() { 361 Serial.begin(9600); 362 363 Wire.begin(); 364 Wire.beginTransmission(MPU_ADDR); // Begins a transmission 365 to the I2C slave (GY-521 board) 366 Wire.write(0x6B); // PWR_MGMT_1 register 367 368 Wire.write(0); // set to zero (wakes up the MPU-6050) 369 Wire.endTransmission(true); 370 371 //Der MAX7219 ist beim Starten im Power-Saving Modus, 372 //er muss aufgeweckt 373 werden. 374 //The MAX7219 is in power-saving mode on startup, 375 //we have to 376 do a wakeup call 377 lc.shutdown(0, false); 378 //Helligkeit auf einen Mittelwert 379 380 //Set the brightness to a medium values 381 lc.setIntensity(0, 8); 382 //und 383 Display lschen 384 //and clear the display 385 lc.clearDisplay(0); 386 387 randomSeed(analogRead(0)); 388 389 DiceValue=0; 390 391 for (int i = 0; i < 6; i++) { 392 DiceXpos[i]=DicePic[7][i][0]; 393 394 DiceYpos[i]=DicePic[7][i][1]; 395 396 DiceXdir[i]=random(3)-1; 397 DiceYdir[i]=random(3)-1; 398 399 DiceXspeed[i]=random(126)+120; 400 DiceYspeed[i]=random(126)+120; 401 } 402 403 404 //Pins einstellen 405 //Setup the pins 406// pinMode(PinTiltX, INPUT_PULLUP); 407 (for Tilt Sensor) 408 // pinMode(PinTiltY, INPUT_PULLUP); 409// attachInterrupt(digitalPinToInterrupt(PinTiltX),SetSpeedX,CHANGE); 410 411 // attachInterrupt(digitalPinToInterrupt(PinTiltY),SetSpeedY,CHANGE); 412 413 414 lc.clearDisplay(0); 415 416 timestamp=millis(); 417 Mode=1; 418 419 ShowDicePic(6); 420 421 delay(1000); 422 423 lc.clearDisplay(0); 424 Mode=0; 425 Serial.begin(9600); 426 427 step=0; 428 shakes=0; 429} 430 431void loop() { 432 433Wire.beginTransmission(MPU_ADDR); 434 435 Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and 436 MPU-6050 Register Map and Descriptions Revision 4.2, p.40] 437 Wire.endTransmission(false); 438 // the parameter indicates that the Arduino will send a restart. As a result, the 439 connection is kept active. 440 Wire.requestFrom(MPU_ADDR, 7*2, true); // request 441 a total of 7*2=14 registers 442 gyro_z = Wire.read()<<8 | Wire.read(); 443 accelerometer_z 444 = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 445 (ACCEL_ZOUT_L) 446Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z)); 447 448 //Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z)); 449 450 451 Serial.println(); 452 delay(1000); 453 454 delay(50); 455 step=step+1; 456 457 if (step>20) { 458 //1 sek ist vorbei 459 //1 sec is over 460 step=0; 461 462 ShakesPerSecond=shakes; 463 shakes=0; 464 } 465 466 if(gyro_z>7500||gyro_z<-3000) 467 468 { 469 MoveDots(); 470 if (millis()-timestamp>2000) { 471 //seit 2 sek 472 kein Schtteln mehr 473 //there is no shaking since 2 sec 474 Mode=1; 475 476 DiceValue=random(6)+1; 477 ShowDicePic(DiceValue); 478 } 479 480 481 482 if (ShakesPerSecond>5) { 483 //Es wird wieder geschttelt 484 //shaking again 485 486 Mode=0; 487 } 488 } 489 490} 491
Downloadable files
connection with MPU6050
connection with MPU6050

Comments
Only logged in users can leave comments