the E-Dice version of the wooden labyrinth. Try catching the blinking led by pitch and roll the dice. please forgive... it my first c++ program

Dependencies:   USBDevice mbed

Committer:
VBDaniel
Date:
Sat Dec 13 22:26:44 2014 +0000
Revision:
0:e2c7e03219ab
the E-Dice version of the wooden labyrinth. Try catching the blinking led by pitch and roll the dice. ; please forgive... it my first c++ program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
VBDaniel 0:e2c7e03219ab 1 #include "mbed.h"
VBDaniel 0:e2c7e03219ab 2 #include "USBSerial.h"
VBDaniel 0:e2c7e03219ab 3 #include <stdlib.h>
VBDaniel 0:e2c7e03219ab 4 #include "MMA845x.h"
VBDaniel 0:e2c7e03219ab 5
VBDaniel 0:e2c7e03219ab 6 //Virtual serial port over USB
VBDaniel 0:e2c7e03219ab 7 USBSerial serial;
VBDaniel 0:e2c7e03219ab 8
VBDaniel 0:e2c7e03219ab 9 //accelerometer
VBDaniel 0:e2c7e03219ab 10 PinName const SDA = P0_5;
VBDaniel 0:e2c7e03219ab 11 PinName const SCL = P0_4;
VBDaniel 0:e2c7e03219ab 12 PinName const INT1 = P0_20;
VBDaniel 0:e2c7e03219ab 13 PinName const INT2 = P0_2;
VBDaniel 0:e2c7e03219ab 14 #define MMA8451_I2C_ADDRESS (0x1C << 1)
VBDaniel 0:e2c7e03219ab 15
VBDaniel 0:e2c7e03219ab 16 MMA845x acc(SDA, SCL, MMA8451_I2C_ADDRESS, INT1, INT2 );
VBDaniel 0:e2c7e03219ab 17
VBDaniel 0:e2c7e03219ab 18 //Struct to represent the neighbors of an led
VBDaniel 0:e2c7e03219ab 19 //stored clockwise, starting up side
VBDaniel 0:e2c7e03219ab 20 struct circuit
VBDaniel 0:e2c7e03219ab 21 {
VBDaniel 0:e2c7e03219ab 22 int up;
VBDaniel 0:e2c7e03219ab 23 int right;
VBDaniel 0:e2c7e03219ab 24 int down;
VBDaniel 0:e2c7e03219ab 25 int left;
VBDaniel 0:e2c7e03219ab 26 };
VBDaniel 0:e2c7e03219ab 27 struct circuit layout[] = {{-1,-1, 1,-1}, // LED 0
VBDaniel 0:e2c7e03219ab 28 { 0, 3, 2,-1}, // LED 1
VBDaniel 0:e2c7e03219ab 29 { 1,-1,-1,-1}, // LED 2
VBDaniel 0:e2c7e03219ab 30 {-1, 5,-1, 1}, // LED 3
VBDaniel 0:e2c7e03219ab 31 {-1, 7, 5,-1}, // LED 4
VBDaniel 0:e2c7e03219ab 32 { 4, 8, 6, 3}, // LED 5
VBDaniel 0:e2c7e03219ab 33 { 5, 9,-1,-1}, // LED 6
VBDaniel 0:e2c7e03219ab 34 {-1,-1, 8, 4}, // LED 7 , LED0 rigth
VBDaniel 0:e2c7e03219ab 35 { 7,10, 9, 5}, // LED 8
VBDaniel 0:e2c7e03219ab 36 { 8,-1,-1, 6}, // LED 9
VBDaniel 0:e2c7e03219ab 37 {-1,12,-1, 8}, // LED 10
VBDaniel 0:e2c7e03219ab 38 {-1,-1,12,-1}, // LED 11
VBDaniel 0:e2c7e03219ab 39 {11,-1,13,10}, // LED 12
VBDaniel 0:e2c7e03219ab 40 {12,-1,-1,-1}, // LED 13
VBDaniel 0:e2c7e03219ab 41 };
VBDaniel 0:e2c7e03219ab 42
VBDaniel 0:e2c7e03219ab 43 DigitalOut LEDs[] = {(P0_19), (P0_18), (P0_17), (P1_15), (P0_23), (P0_14), (P0_15),
VBDaniel 0:e2c7e03219ab 44 (P0_9), (P0_8), (P0_7), (P0_21), (P0_10), (P0_22), (P0_11)
VBDaniel 0:e2c7e03219ab 45 };// declare 10 LEDs
VBDaniel 0:e2c7e03219ab 46
VBDaniel 0:e2c7e03219ab 47 DigitalOut Buzzer(P0_13);
VBDaniel 0:e2c7e03219ab 48
VBDaniel 0:e2c7e03219ab 49 //catch the blinking LED
VBDaniel 0:e2c7e03219ab 50 volatile int destLed = 13;
VBDaniel 0:e2c7e03219ab 51 void blink()
VBDaniel 0:e2c7e03219ab 52 {
VBDaniel 0:e2c7e03219ab 53 LEDs[destLed] = !LEDs[destLed];
VBDaniel 0:e2c7e03219ab 54 }
VBDaniel 0:e2c7e03219ab 55
VBDaniel 0:e2c7e03219ab 56 void change_dest()
VBDaniel 0:e2c7e03219ab 57 {
VBDaniel 0:e2c7e03219ab 58 int new_dest ;
VBDaniel 0:e2c7e03219ab 59 do {
VBDaniel 0:e2c7e03219ab 60 new_dest = rand() % 14 ;
VBDaniel 0:e2c7e03219ab 61 }
VBDaniel 0:e2c7e03219ab 62 while ( LEDs[new_dest] );
VBDaniel 0:e2c7e03219ab 63
VBDaniel 0:e2c7e03219ab 64 LEDs[destLed] = false;
VBDaniel 0:e2c7e03219ab 65 destLed = new_dest;
VBDaniel 0:e2c7e03219ab 66 }
VBDaniel 0:e2c7e03219ab 67
VBDaniel 0:e2c7e03219ab 68 main()
VBDaniel 0:e2c7e03219ab 69 {
VBDaniel 0:e2c7e03219ab 70 srand((unsigned)(acc.getX()+acc.getY()));
VBDaniel 0:e2c7e03219ab 71 change_dest();
VBDaniel 0:e2c7e03219ab 72
VBDaniel 0:e2c7e03219ab 73 int X1;
VBDaniel 0:e2c7e03219ab 74 int Y1;
VBDaniel 0:e2c7e03219ab 75 int Z1;
VBDaniel 0:e2c7e03219ab 76
VBDaniel 0:e2c7e03219ab 77 int i;
VBDaniel 0:e2c7e03219ab 78
VBDaniel 0:e2c7e03219ab 79 Ticker blink_timer;
VBDaniel 0:e2c7e03219ab 80 blink_timer.attach(&blink,0.15);
VBDaniel 0:e2c7e03219ab 81
VBDaniel 0:e2c7e03219ab 82 Ticker dest_timer;
VBDaniel 0:e2c7e03219ab 83 dest_timer.attach(&change_dest,1.0);
VBDaniel 0:e2c7e03219ab 84
VBDaniel 0:e2c7e03219ab 85 for (i=0;i<14;i++)
VBDaniel 0:e2c7e03219ab 86 {
VBDaniel 0:e2c7e03219ab 87 LEDs[i] = false;
VBDaniel 0:e2c7e03219ab 88 i++;
VBDaniel 0:e2c7e03219ab 89 }
VBDaniel 0:e2c7e03219ab 90 Buzzer = false;
VBDaniel 0:e2c7e03219ab 91
VBDaniel 0:e2c7e03219ab 92 int activeLED = 0;
VBDaniel 0:e2c7e03219ab 93 int nextLED = -1;
VBDaniel 0:e2c7e03219ab 94 LEDs[activeLED] = true;
VBDaniel 0:e2c7e03219ab 95 int speed = 300;
VBDaniel 0:e2c7e03219ab 96 int speedfactor = 4;
VBDaniel 0:e2c7e03219ab 97
VBDaniel 0:e2c7e03219ab 98 i = 0;
VBDaniel 0:e2c7e03219ab 99 do {
VBDaniel 0:e2c7e03219ab 100 i++;
VBDaniel 0:e2c7e03219ab 101
VBDaniel 0:e2c7e03219ab 102 X1=0;
VBDaniel 0:e2c7e03219ab 103 Y1=0;
VBDaniel 0:e2c7e03219ab 104 Z1=0;
VBDaniel 0:e2c7e03219ab 105
VBDaniel 0:e2c7e03219ab 106 for(int j=1;j<10;j++) {
VBDaniel 0:e2c7e03219ab 107 X1 += acc.getX();
VBDaniel 0:e2c7e03219ab 108 Y1 += acc.getY();
VBDaniel 0:e2c7e03219ab 109 Z1 += acc.getZ();
VBDaniel 0:e2c7e03219ab 110 };
VBDaniel 0:e2c7e03219ab 111
VBDaniel 0:e2c7e03219ab 112 X1 /= 10;
VBDaniel 0:e2c7e03219ab 113 Y1 /= 10;
VBDaniel 0:e2c7e03219ab 114 Z1 /= 10;
VBDaniel 0:e2c7e03219ab 115
VBDaniel 0:e2c7e03219ab 116 /* double c = sqrt( pow((double) X1,2.0)+pow((double) Y1,2.0)+pow((double) Z1,2.0) );
VBDaniel 0:e2c7e03219ab 117
VBDaniel 0:e2c7e03219ab 118 serial.printf("Nr : %d \n",i);
VBDaniel 0:e2c7e03219ab 119 serial.printf("X-Axis: %d \n",X1);
VBDaniel 0:e2c7e03219ab 120 serial.printf("Y-Axis: %d \n",Y1);
VBDaniel 0:e2c7e03219ab 121 serial.printf("Z-Axis: %d \n",Z1);
VBDaniel 0:e2c7e03219ab 122 serial.printf("G: %f \n",c); */
VBDaniel 0:e2c7e03219ab 123
VBDaniel 0:e2c7e03219ab 124 nextLED = -1;
VBDaniel 0:e2c7e03219ab 125 if ((abs(X1)>10) or (abs(Y1)>10)) {
VBDaniel 0:e2c7e03219ab 126 if (abs(X1) > abs(Y1)) {
VBDaniel 0:e2c7e03219ab 127 speed = 300 - abs(X1);
VBDaniel 0:e2c7e03219ab 128 if (X1 > 0 ) nextLED = layout[activeLED].right ;
VBDaniel 0:e2c7e03219ab 129 else nextLED = layout[activeLED].left;
VBDaniel 0:e2c7e03219ab 130 }
VBDaniel 0:e2c7e03219ab 131 else {
VBDaniel 0:e2c7e03219ab 132 speed = 300 - abs(Y1);
VBDaniel 0:e2c7e03219ab 133 if (Y1 < 0 ) nextLED = layout[activeLED].up ;
VBDaniel 0:e2c7e03219ab 134 else nextLED = layout[activeLED].down;
VBDaniel 0:e2c7e03219ab 135 };
VBDaniel 0:e2c7e03219ab 136 if (nextLED > -1) {
VBDaniel 0:e2c7e03219ab 137 LEDs[activeLED]= false;
VBDaniel 0:e2c7e03219ab 138 activeLED = nextLED;
VBDaniel 0:e2c7e03219ab 139 LEDs[activeLED]= true;
VBDaniel 0:e2c7e03219ab 140
VBDaniel 0:e2c7e03219ab 141 if ( speed < 150 ) speed=150;
VBDaniel 0:e2c7e03219ab 142 wait_ms(speed/speedfactor);
VBDaniel 0:e2c7e03219ab 143 Buzzer = false;
VBDaniel 0:e2c7e03219ab 144 }
VBDaniel 0:e2c7e03219ab 145 else {
VBDaniel 0:e2c7e03219ab 146 for (int j=0;j<128;j++) {
VBDaniel 0:e2c7e03219ab 147 Buzzer = !Buzzer ;
VBDaniel 0:e2c7e03219ab 148 wait_ms(speed/(speedfactor*32));
VBDaniel 0:e2c7e03219ab 149 }
VBDaniel 0:e2c7e03219ab 150 }
VBDaniel 0:e2c7e03219ab 151 }
VBDaniel 0:e2c7e03219ab 152 else {
VBDaniel 0:e2c7e03219ab 153 speed=300;
VBDaniel 0:e2c7e03219ab 154 wait_ms(speed/speedfactor);
VBDaniel 0:e2c7e03219ab 155 };
VBDaniel 0:e2c7e03219ab 156
VBDaniel 0:e2c7e03219ab 157 }
VBDaniel 0:e2c7e03219ab 158 while (activeLED != destLed);
VBDaniel 0:e2c7e03219ab 159 dest_timer.detach();
VBDaniel 0:e2c7e03219ab 160 blink_timer.detach();
VBDaniel 0:e2c7e03219ab 161 destLed = activeLED;
VBDaniel 0:e2c7e03219ab 162 blink_timer.attach(&blink,0.1);
VBDaniel 0:e2c7e03219ab 163
VBDaniel 0:e2c7e03219ab 164 for (int j=0;j<50;j++) {
VBDaniel 0:e2c7e03219ab 165 Buzzer = !Buzzer ;
VBDaniel 0:e2c7e03219ab 166 wait_ms(50);
VBDaniel 0:e2c7e03219ab 167 }
VBDaniel 0:e2c7e03219ab 168 blink_timer.detach();
VBDaniel 0:e2c7e03219ab 169 LEDs[activeLED] = false;
VBDaniel 0:e2c7e03219ab 170 deepsleep();
VBDaniel 0:e2c7e03219ab 171 }
VBDaniel 0:e2c7e03219ab 172