Dice of Destiny game for 4180 Final Project
Dependencies: mbed wave_player Servo mbed-rtos 4DGL-uLCD-SE SDFileSystem_OldbutworkswithRTOS PinDetect MMA8452
main.cpp@11:f10e4bf76994, 2019-12-09 (annotated)
- Committer:
- Kimbenn
- Date:
- Mon Dec 09 14:25:02 2019 +0000
- Revision:
- 11:f10e4bf76994
- Parent:
- 10:5b1f8be6c4f3
Publishing 4180 Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kimbenn | 10:5b1f8be6c4f3 | 1 | #include "MMA8452.h" |
4180_1 | 0:cfcf73272647 | 2 | #include "mbed.h" |
Kimbenn | 10:5b1f8be6c4f3 | 3 | #include "rtos.h" |
4180_1 | 2:75727e89a717 | 4 | #include "uLCD_4DGL.h" |
Kimbenn | 10:5b1f8be6c4f3 | 5 | #include "Servo.h" |
Kimbenn | 10:5b1f8be6c4f3 | 6 | #include "PinDetect.h" |
Kimbenn | 10:5b1f8be6c4f3 | 7 | #include "SDFileSystem.h" |
Kimbenn | 10:5b1f8be6c4f3 | 8 | #include "Servo.h" |
Kimbenn | 10:5b1f8be6c4f3 | 9 | #include "wave_player.h" |
Kimbenn | 10:5b1f8be6c4f3 | 10 | |
Kimbenn | 10:5b1f8be6c4f3 | 11 | AnalogIn photocell(p15); |
Kimbenn | 10:5b1f8be6c4f3 | 12 | |
Kimbenn | 10:5b1f8be6c4f3 | 13 | Serial pc(USBTX,USBRX); |
Kimbenn | 10:5b1f8be6c4f3 | 14 | Servo myservo(p21); |
Kimbenn | 10:5b1f8be6c4f3 | 15 | // SD CARD & SOUND |
Kimbenn | 10:5b1f8be6c4f3 | 16 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
Kimbenn | 10:5b1f8be6c4f3 | 17 | AnalogOut DACout(p18); |
Kimbenn | 10:5b1f8be6c4f3 | 18 | //SD CARD & SOUND |
4180_1 | 0:cfcf73272647 | 19 | |
4180_1 | 2:75727e89a717 | 20 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
Kimbenn | 10:5b1f8be6c4f3 | 21 | PinDetect pb1(p16); |
Kimbenn | 10:5b1f8be6c4f3 | 22 | PinDetect pb2(p17); |
Kimbenn | 10:5b1f8be6c4f3 | 23 | PinDetect pb3(p26); |
4180_1 | 0:cfcf73272647 | 24 | |
Kimbenn | 10:5b1f8be6c4f3 | 25 | AnalogIn pot1(p19); |
Kimbenn | 10:5b1f8be6c4f3 | 26 | AnalogIn pot2(p20); |
Kimbenn | 10:5b1f8be6c4f3 | 27 | |
Kimbenn | 10:5b1f8be6c4f3 | 28 | DigitalOut led1(p30); |
Kimbenn | 10:5b1f8be6c4f3 | 29 | DigitalOut led2(p29); |
Kimbenn | 10:5b1f8be6c4f3 | 30 | DigitalOut led3(p25); |
Kimbenn | 10:5b1f8be6c4f3 | 31 | DigitalOut led4(p24); |
Kimbenn | 10:5b1f8be6c4f3 | 32 | DigitalOut led5(p23); |
Kimbenn | 10:5b1f8be6c4f3 | 33 | DigitalOut led6(p22); |
Kimbenn | 10:5b1f8be6c4f3 | 34 | |
Kimbenn | 10:5b1f8be6c4f3 | 35 | Mutex lcd_mutex; |
Kimbenn | 10:5b1f8be6c4f3 | 36 | |
Kimbenn | 10:5b1f8be6c4f3 | 37 | MMA8452 acc(p28, p27, 100000); |
Kimbenn | 10:5b1f8be6c4f3 | 38 | wave_player waver(&DACout); |
Kimbenn | 10:5b1f8be6c4f3 | 39 | |
Kimbenn | 10:5b1f8be6c4f3 | 40 | ////////////////// GLOBAL VARIABLES ///////////////////////////// |
Kimbenn | 10:5b1f8be6c4f3 | 41 | int game_state = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 42 | int player_guess = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 43 | float dice_color = 16777215; |
Kimbenn | 10:5b1f8be6c4f3 | 44 | float result_rolled; |
Kimbenn | 10:5b1f8be6c4f3 | 45 | double xg,yg,zg; |
Kimbenn | 10:5b1f8be6c4f3 | 46 | float dot_size; |
Kimbenn | 10:5b1f8be6c4f3 | 47 | |
Kimbenn | 10:5b1f8be6c4f3 | 48 | FILE *audio; |
Kimbenn | 10:5b1f8be6c4f3 | 49 | ///////////////////////////////////////////////////////////////// |
Kimbenn | 10:5b1f8be6c4f3 | 50 | |
Kimbenn | 10:5b1f8be6c4f3 | 51 | void one_led(){ |
Kimbenn | 10:5b1f8be6c4f3 | 52 | led1 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 53 | led2 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 54 | led3 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 55 | led4 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 56 | led5 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 57 | led6 = 0; |
4180_1 | 7:7bd7397ab89f | 58 | } |
Kimbenn | 10:5b1f8be6c4f3 | 59 | |
Kimbenn | 10:5b1f8be6c4f3 | 60 | void two_led(){ |
Kimbenn | 10:5b1f8be6c4f3 | 61 | led1 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 62 | led2 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 63 | led3 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 64 | led4 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 65 | led5 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 66 | led6 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 67 | } |
Kimbenn | 10:5b1f8be6c4f3 | 68 | void three_led(){ |
Kimbenn | 10:5b1f8be6c4f3 | 69 | led1 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 70 | led2 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 71 | led3 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 72 | led4 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 73 | led5 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 74 | led6 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 75 | } |
Kimbenn | 10:5b1f8be6c4f3 | 76 | void four_led(){ |
Kimbenn | 10:5b1f8be6c4f3 | 77 | led1 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 78 | led2 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 79 | led3 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 80 | led4 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 81 | led5 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 82 | led6 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 83 | } |
Kimbenn | 10:5b1f8be6c4f3 | 84 | void five_led(){ |
Kimbenn | 10:5b1f8be6c4f3 | 85 | led1 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 86 | led2 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 87 | led3 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 88 | led4 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 89 | led5 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 90 | led6 = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 91 | } |
Kimbenn | 10:5b1f8be6c4f3 | 92 | void six_led(){ |
Kimbenn | 10:5b1f8be6c4f3 | 93 | led1 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 94 | led2 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 95 | led3 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 96 | led4 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 97 | led5 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 98 | led6 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 99 | } |
Kimbenn | 10:5b1f8be6c4f3 | 100 | |
Kimbenn | 10:5b1f8be6c4f3 | 101 | void guess_up() { |
Kimbenn | 10:5b1f8be6c4f3 | 102 | player_guess++; |
Kimbenn | 10:5b1f8be6c4f3 | 103 | if (player_guess > 6) {player_guess = 6;} |
Kimbenn | 10:5b1f8be6c4f3 | 104 | } |
Kimbenn | 10:5b1f8be6c4f3 | 105 | |
Kimbenn | 10:5b1f8be6c4f3 | 106 | void guess_down() { |
Kimbenn | 10:5b1f8be6c4f3 | 107 | player_guess--; |
Kimbenn | 10:5b1f8be6c4f3 | 108 | if (player_guess < 1) {player_guess = 1;} |
4180_1 | 4:25a266a74a4c | 109 | } |
Kimbenn | 10:5b1f8be6c4f3 | 110 | |
Kimbenn | 10:5b1f8be6c4f3 | 111 | |
Kimbenn | 10:5b1f8be6c4f3 | 112 | void next_state() { |
Kimbenn | 10:5b1f8be6c4f3 | 113 | game_state++; |
Kimbenn | 10:5b1f8be6c4f3 | 114 | if (game_state == 3) {game_state = 0;} |
Kimbenn | 10:5b1f8be6c4f3 | 115 | else if(game_state == 2) {game_state = 1;} |
Kimbenn | 10:5b1f8be6c4f3 | 116 | } |
Kimbenn | 10:5b1f8be6c4f3 | 117 | |
Kimbenn | 10:5b1f8be6c4f3 | 118 | void dice1(){ |
Kimbenn | 10:5b1f8be6c4f3 | 119 | lcd_mutex.lock(); |
Kimbenn | 10:5b1f8be6c4f3 | 120 | uLCD.filled_rectangle(20, 20, 108, 108, dice_color); |
Kimbenn | 10:5b1f8be6c4f3 | 121 | uLCD.filled_circle(64,64,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 122 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 123 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 124 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 125 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 126 | } |
Kimbenn | 10:5b1f8be6c4f3 | 127 | void dice2(){ |
Kimbenn | 10:5b1f8be6c4f3 | 128 | lcd_mutex.lock(); |
Kimbenn | 11:f10e4bf76994 | 129 | uLCD.filled_rectangle(17, 17, 111, 111, dice_color); |
Kimbenn | 11:f10e4bf76994 | 130 | uLCD.filled_circle(92,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 131 | uLCD.filled_circle(36,92,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 132 | Thread::wait(1000); |
4180_1 | 3:454d1f4c8fd7 | 133 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 134 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 135 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 136 | } |
Kimbenn | 10:5b1f8be6c4f3 | 137 | void dice3(){ |
Kimbenn | 10:5b1f8be6c4f3 | 138 | lcd_mutex.lock(); |
Kimbenn | 11:f10e4bf76994 | 139 | |
Kimbenn | 11:f10e4bf76994 | 140 | uLCD.filled_rectangle(17, 17, 111, 111, dice_color); |
Kimbenn | 10:5b1f8be6c4f3 | 141 | uLCD.filled_circle(64,64,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 142 | uLCD.filled_circle(92,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 143 | uLCD.filled_circle(36,92,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 144 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 145 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 146 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 147 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 148 | } |
Kimbenn | 10:5b1f8be6c4f3 | 149 | void dice4(){ |
Kimbenn | 10:5b1f8be6c4f3 | 150 | lcd_mutex.lock(); |
Kimbenn | 11:f10e4bf76994 | 151 | |
Kimbenn | 11:f10e4bf76994 | 152 | uLCD.filled_rectangle(17, 17, 111, 111, dice_color); |
Kimbenn | 11:f10e4bf76994 | 153 | uLCD.filled_circle(92,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 154 | uLCD.filled_circle(36,92,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 155 | uLCD.filled_circle(36,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 156 | uLCD.filled_circle(92,92,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 157 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 158 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 159 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 160 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 161 | } |
Kimbenn | 10:5b1f8be6c4f3 | 162 | void dice5(){ |
Kimbenn | 10:5b1f8be6c4f3 | 163 | lcd_mutex.lock(); |
Kimbenn | 11:f10e4bf76994 | 164 | |
Kimbenn | 11:f10e4bf76994 | 165 | uLCD.filled_rectangle(17, 17, 111, 111, dice_color); |
Kimbenn | 10:5b1f8be6c4f3 | 166 | uLCD.filled_circle(64,64,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 167 | uLCD.filled_circle(92,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 168 | uLCD.filled_circle(36,92,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 169 | uLCD.filled_circle(36,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 170 | uLCD.filled_circle(92,92,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 171 | Thread::wait(1000); |
4180_1 | 5:a1ef40ff0f78 | 172 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 173 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 174 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 175 | } |
Kimbenn | 10:5b1f8be6c4f3 | 176 | void dice6(){ |
Kimbenn | 10:5b1f8be6c4f3 | 177 | lcd_mutex.lock(); |
Kimbenn | 11:f10e4bf76994 | 178 | uLCD.filled_rectangle(17, 17, 111, 111, dice_color); |
Kimbenn | 11:f10e4bf76994 | 179 | uLCD.filled_circle(92,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 180 | uLCD.filled_circle(36,92,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 181 | uLCD.filled_circle(36,36,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 182 | uLCD.filled_circle(92,92,dot_size,BLACK); |
Kimbenn | 11:f10e4bf76994 | 183 | uLCD.filled_circle(36,64,dot_size,BLACK);//Not finished yet |
Kimbenn | 11:f10e4bf76994 | 184 | uLCD.filled_circle(92,64,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 185 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 186 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 187 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 188 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 189 | } |
Kimbenn | 10:5b1f8be6c4f3 | 190 | |
Kimbenn | 10:5b1f8be6c4f3 | 191 | ////////////// SECOND THREAD ////////////////////////////////////////////////// |
Kimbenn | 10:5b1f8be6c4f3 | 192 | void input_guess_change_settings(void const *args){ |
Kimbenn | 10:5b1f8be6c4f3 | 193 | while(true){ |
Kimbenn | 10:5b1f8be6c4f3 | 194 | if (game_state == 1){ |
Kimbenn | 10:5b1f8be6c4f3 | 195 | lcd_mutex.lock(); |
Kimbenn | 10:5b1f8be6c4f3 | 196 | uLCD.printf("Input your guess\nof the dice roll\nwith the buttons\non the breadbaord.\n\n"); |
Kimbenn | 11:f10e4bf76994 | 197 | //Thread::wait(3000); |
Kimbenn | 10:5b1f8be6c4f3 | 198 | uLCD.printf("Pick a number 1-6\nthen roll the die.\n\n"); |
Kimbenn | 11:f10e4bf76994 | 199 | //Thread::wait(2000); |
Kimbenn | 10:5b1f8be6c4f3 | 200 | uLCD.printf("If you want to\nchange the dot\nsize or color,\nyou can with the\npots."); |
Kimbenn | 11:f10e4bf76994 | 201 | Thread::wait(6000); |
Kimbenn | 10:5b1f8be6c4f3 | 202 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 203 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 204 | player_guess = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 205 | led1 = 1; |
Kimbenn | 10:5b1f8be6c4f3 | 206 | while(game_state == 1) { |
Kimbenn | 10:5b1f8be6c4f3 | 207 | |
Kimbenn | 10:5b1f8be6c4f3 | 208 | // if accelerometer changes enough, go to next state |
Kimbenn | 10:5b1f8be6c4f3 | 209 | acc.readXYZGravity(&xg,&yg,&zg); |
Kimbenn | 10:5b1f8be6c4f3 | 210 | zg = abs(zg); |
Kimbenn | 10:5b1f8be6c4f3 | 211 | if(zg>1.1f || zg < 0.75f){ |
Kimbenn | 10:5b1f8be6c4f3 | 212 | game_state = 2; |
Kimbenn | 10:5b1f8be6c4f3 | 213 | } |
Kimbenn | 10:5b1f8be6c4f3 | 214 | dice_color = (floor(100.0f * float(pot1))/100.0f); |
Kimbenn | 10:5b1f8be6c4f3 | 215 | dice_color = dice_color * 16777215.0f; |
Kimbenn | 10:5b1f8be6c4f3 | 216 | if (float(photocell) < 0.4f) { |
Kimbenn | 10:5b1f8be6c4f3 | 217 | dice_color = RED; |
Kimbenn | 10:5b1f8be6c4f3 | 218 | } |
Kimbenn | 10:5b1f8be6c4f3 | 219 | |
Kimbenn | 10:5b1f8be6c4f3 | 220 | dot_size = ((floor(100.0f * float(pot2))/100.0f)*10 )+2; |
Kimbenn | 10:5b1f8be6c4f3 | 221 | |
Kimbenn | 10:5b1f8be6c4f3 | 222 | lcd_mutex.lock(); |
Kimbenn | 10:5b1f8be6c4f3 | 223 | uLCD.filled_rectangle(20, 20, 108, 108, dice_color); |
Kimbenn | 10:5b1f8be6c4f3 | 224 | uLCD.filled_circle(64,64,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 225 | uLCD.filled_circle(88,40,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 226 | uLCD.filled_circle(40,88,dot_size,BLACK); |
Kimbenn | 10:5b1f8be6c4f3 | 227 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 228 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 229 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 230 | |
Kimbenn | 10:5b1f8be6c4f3 | 231 | switch (player_guess){ |
Kimbenn | 10:5b1f8be6c4f3 | 232 | case 1: one_led(); |
Kimbenn | 10:5b1f8be6c4f3 | 233 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 234 | case 2: two_led(); |
Kimbenn | 10:5b1f8be6c4f3 | 235 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 236 | case 3: three_led(); |
Kimbenn | 10:5b1f8be6c4f3 | 237 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 238 | case 4: four_led(); |
Kimbenn | 10:5b1f8be6c4f3 | 239 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 240 | case 5: five_led(); |
Kimbenn | 10:5b1f8be6c4f3 | 241 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 242 | case 6: six_led(); |
Kimbenn | 10:5b1f8be6c4f3 | 243 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 244 | } |
Kimbenn | 10:5b1f8be6c4f3 | 245 | |
Kimbenn | 10:5b1f8be6c4f3 | 246 | } |
4180_1 | 5:a1ef40ff0f78 | 247 | } |
Kimbenn | 10:5b1f8be6c4f3 | 248 | Thread::wait(1000); |
4180_1 | 5:a1ef40ff0f78 | 249 | } |
4180_1 | 6:f752accd632c | 250 | } |
Kimbenn | 10:5b1f8be6c4f3 | 251 | //////////////////////////////////////////////////////////////////////////////// |
4180_1 | 7:7bd7397ab89f | 252 | |
Kimbenn | 10:5b1f8be6c4f3 | 253 | ////////////// THIRD THREAD //////////////////////////////////////////////////// |
Kimbenn | 10:5b1f8be6c4f3 | 254 | void view_result_high_five(void const *args) { |
Kimbenn | 10:5b1f8be6c4f3 | 255 | while(true){ |
Kimbenn | 10:5b1f8be6c4f3 | 256 | if(game_state == 2){ |
Kimbenn | 10:5b1f8be6c4f3 | 257 | |
Kimbenn | 10:5b1f8be6c4f3 | 258 | audio = fopen("/sd/roll.wav","r"); |
Kimbenn | 10:5b1f8be6c4f3 | 259 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 260 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 261 | |
Kimbenn | 10:5b1f8be6c4f3 | 262 | result_rolled = rand()%6 + 1; |
Kimbenn | 10:5b1f8be6c4f3 | 263 | switch(int(result_rolled)){ |
Kimbenn | 10:5b1f8be6c4f3 | 264 | case 1: dice1(); |
Kimbenn | 10:5b1f8be6c4f3 | 265 | audio = fopen("/sd/one.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 266 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 267 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 268 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 269 | case 2: dice2(); |
Kimbenn | 10:5b1f8be6c4f3 | 270 | audio = fopen("/sd/two.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 271 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 272 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 273 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 274 | case 3: dice3(); |
Kimbenn | 10:5b1f8be6c4f3 | 275 | audio = fopen("/sd/three.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 276 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 277 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 278 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 279 | case 4: dice4(); |
Kimbenn | 10:5b1f8be6c4f3 | 280 | audio = fopen("/sd/four.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 281 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 282 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 283 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 284 | case 5: dice5(); |
Kimbenn | 10:5b1f8be6c4f3 | 285 | audio = fopen("/sd/five.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 286 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 287 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 288 | break; |
Kimbenn | 10:5b1f8be6c4f3 | 289 | case 6: dice6(); |
Kimbenn | 10:5b1f8be6c4f3 | 290 | audio = fopen("/sd/six.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 291 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 292 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 293 | break;} |
Kimbenn | 10:5b1f8be6c4f3 | 294 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 295 | |
Kimbenn | 10:5b1f8be6c4f3 | 296 | while(game_state == 2){ |
Kimbenn | 10:5b1f8be6c4f3 | 297 | if(player_guess == result_rolled){ |
Kimbenn | 10:5b1f8be6c4f3 | 298 | audio = fopen("/sd/won.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 299 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 300 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 301 | lcd_mutex.lock(); |
Kimbenn | 10:5b1f8be6c4f3 | 302 | uLCD.printf(" You WIN!!\n\n High five!"); |
Kimbenn | 10:5b1f8be6c4f3 | 303 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 304 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 305 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 306 | for(float p=0; p<1.0; p += 0.01) { |
Kimbenn | 10:5b1f8be6c4f3 | 307 | myservo = p; |
Kimbenn | 10:5b1f8be6c4f3 | 308 | Thread::wait(0.2); |
Kimbenn | 10:5b1f8be6c4f3 | 309 | } |
Kimbenn | 10:5b1f8be6c4f3 | 310 | |
Kimbenn | 10:5b1f8be6c4f3 | 311 | Thread::wait(4000); |
Kimbenn | 10:5b1f8be6c4f3 | 312 | |
Kimbenn | 10:5b1f8be6c4f3 | 313 | for(float p=1; p>0.0; p -= 0.01) { |
Kimbenn | 10:5b1f8be6c4f3 | 314 | myservo = p; |
Kimbenn | 10:5b1f8be6c4f3 | 315 | Thread::wait(0.2); |
Kimbenn | 10:5b1f8be6c4f3 | 316 | } |
Kimbenn | 10:5b1f8be6c4f3 | 317 | /////////////////////////////////////// |
Kimbenn | 10:5b1f8be6c4f3 | 318 | } |
Kimbenn | 10:5b1f8be6c4f3 | 319 | else if(player_guess != result_rolled) { |
Kimbenn | 10:5b1f8be6c4f3 | 320 | audio = fopen("/sd/lost.wav", "r"); |
Kimbenn | 10:5b1f8be6c4f3 | 321 | waver.play(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 322 | fclose(audio); |
Kimbenn | 10:5b1f8be6c4f3 | 323 | lcd_mutex.lock(); |
Kimbenn | 10:5b1f8be6c4f3 | 324 | uLCD.printf(" you lose....."); |
Kimbenn | 10:5b1f8be6c4f3 | 325 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 326 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 327 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 328 | } |
Kimbenn | 10:5b1f8be6c4f3 | 329 | lcd_mutex.lock(); |
Kimbenn | 10:5b1f8be6c4f3 | 330 | uLCD.printf("Press the button\nto play again!"); |
Kimbenn | 10:5b1f8be6c4f3 | 331 | while(game_state == 2){Thread::wait(250);} |
Kimbenn | 10:5b1f8be6c4f3 | 332 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 333 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 334 | } |
Kimbenn | 10:5b1f8be6c4f3 | 335 | } |
Kimbenn | 10:5b1f8be6c4f3 | 336 | } |
Kimbenn | 10:5b1f8be6c4f3 | 337 | } |
Kimbenn | 10:5b1f8be6c4f3 | 338 | /////////////////////////////////////////////////////////////////////////////// |
4180_1 | 8:31e63caf37e2 | 339 | |
Kimbenn | 10:5b1f8be6c4f3 | 340 | int main(){ |
Kimbenn | 10:5b1f8be6c4f3 | 341 | pb1.mode(PullDown); |
Kimbenn | 10:5b1f8be6c4f3 | 342 | pb1.attach_deasserted(&guess_down); |
Kimbenn | 10:5b1f8be6c4f3 | 343 | pb1.setSampleFrequency(); |
Kimbenn | 10:5b1f8be6c4f3 | 344 | |
Kimbenn | 10:5b1f8be6c4f3 | 345 | pb2.mode(PullDown); |
Kimbenn | 10:5b1f8be6c4f3 | 346 | pb2.attach_deasserted(&guess_up); |
Kimbenn | 10:5b1f8be6c4f3 | 347 | pb2.setSampleFrequency(); |
Kimbenn | 10:5b1f8be6c4f3 | 348 | |
Kimbenn | 10:5b1f8be6c4f3 | 349 | pb3.mode(PullDown); |
Kimbenn | 10:5b1f8be6c4f3 | 350 | pb3.attach_deasserted(&next_state); |
Kimbenn | 10:5b1f8be6c4f3 | 351 | pb3.setSampleFrequency(); |
Kimbenn | 10:5b1f8be6c4f3 | 352 | |
Kimbenn | 10:5b1f8be6c4f3 | 353 | Thread t2(input_guess_change_settings); |
Kimbenn | 10:5b1f8be6c4f3 | 354 | Thread t3(view_result_high_five); |
Kimbenn | 11:f10e4bf76994 | 355 | uLCD.baudrate(3000000); |
Kimbenn | 10:5b1f8be6c4f3 | 356 | while(1){ |
Kimbenn | 10:5b1f8be6c4f3 | 357 | if (game_state == 0){ |
Kimbenn | 10:5b1f8be6c4f3 | 358 | myservo = 0; |
Kimbenn | 10:5b1f8be6c4f3 | 359 | lcd_mutex.lock(); |
Kimbenn | 10:5b1f8be6c4f3 | 360 | uLCD.printf("\n\n Welcome!\n\n\nPress the button\nto start."); |
Kimbenn | 10:5b1f8be6c4f3 | 361 | Thread::wait(1000); |
Kimbenn | 10:5b1f8be6c4f3 | 362 | while(game_state == 0){Thread::wait(250);} |
Kimbenn | 10:5b1f8be6c4f3 | 363 | uLCD.cls(); |
Kimbenn | 10:5b1f8be6c4f3 | 364 | lcd_mutex.unlock(); |
Kimbenn | 10:5b1f8be6c4f3 | 365 | } |
Kimbenn | 10:5b1f8be6c4f3 | 366 | Thread::wait(250); |
Kimbenn | 10:5b1f8be6c4f3 | 367 | |
Kimbenn | 10:5b1f8be6c4f3 | 368 | } |
Kimbenn | 10:5b1f8be6c4f3 | 369 | } |