Dice of Destiny game for 4180 Final Project

Dependencies:   mbed wave_player Servo mbed-rtos 4DGL-uLCD-SE SDFileSystem_OldbutworkswithRTOS PinDetect MMA8452

Committer:
Kimbenn
Date:
Mon Dec 09 03:56:43 2019 +0000
Revision:
10:5b1f8be6c4f3
Parent:
8:31e63caf37e2
Child:
11:f10e4bf76994
Publish1

Who changed what in which revision?

UserRevisionLine numberNew 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 10:5b1f8be6c4f3 129 uLCD.filled_rectangle(20, 20, 108, 108, dice_color);
Kimbenn 10:5b1f8be6c4f3 130 uLCD.filled_circle(88,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 131 uLCD.filled_circle(40,88,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 10:5b1f8be6c4f3 139 uLCD.filled_rectangle(20, 20, 108, 108, dice_color);
Kimbenn 10:5b1f8be6c4f3 140 uLCD.filled_circle(64,64,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 141 uLCD.filled_circle(88,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 142 uLCD.filled_circle(40,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 143 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 144 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 145 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 146 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 147 }
Kimbenn 10:5b1f8be6c4f3 148 void dice4(){
Kimbenn 10:5b1f8be6c4f3 149 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 150 uLCD.filled_rectangle(20, 20, 108, 108, dice_color);
Kimbenn 10:5b1f8be6c4f3 151 uLCD.filled_circle(88,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 152 uLCD.filled_circle(40,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 153 uLCD.filled_circle(40,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 154 uLCD.filled_circle(88,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 155 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 156 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 157 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 158 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 159 }
Kimbenn 10:5b1f8be6c4f3 160 void dice5(){
Kimbenn 10:5b1f8be6c4f3 161 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 162 uLCD.filled_rectangle(20, 20, 108, 108, dice_color);
Kimbenn 10:5b1f8be6c4f3 163 uLCD.filled_circle(64,64,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 164 uLCD.filled_circle(88,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 165 uLCD.filled_circle(40,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 166 uLCD.filled_circle(40,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 167 uLCD.filled_circle(88,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 168 Thread::wait(1000);
4180_1 5:a1ef40ff0f78 169 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 170 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 171 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 172 }
Kimbenn 10:5b1f8be6c4f3 173 void dice6(){
Kimbenn 10:5b1f8be6c4f3 174 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 175 uLCD.filled_rectangle(20, 20, 108, 108, dice_color);
Kimbenn 10:5b1f8be6c4f3 176 uLCD.filled_circle(88,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 177 uLCD.filled_circle(40,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 178 uLCD.filled_circle(40,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 179 uLCD.filled_circle(88,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 180 uLCD.filled_circle(40,40,dot_size,BLACK);//Not finished yet
Kimbenn 10:5b1f8be6c4f3 181 uLCD.filled_circle(88,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 182 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 183 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 184 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 185 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 186 }
Kimbenn 10:5b1f8be6c4f3 187
Kimbenn 10:5b1f8be6c4f3 188 ////////////// SECOND THREAD //////////////////////////////////////////////////
Kimbenn 10:5b1f8be6c4f3 189 void input_guess_change_settings(void const *args){
Kimbenn 10:5b1f8be6c4f3 190 while(true){
Kimbenn 10:5b1f8be6c4f3 191 if (game_state == 1){
Kimbenn 10:5b1f8be6c4f3 192 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 193 uLCD.printf("Input your guess\nof the dice roll\nwith the buttons\non the breadbaord.\n\n");
Kimbenn 10:5b1f8be6c4f3 194 Thread::wait(300);
Kimbenn 10:5b1f8be6c4f3 195 uLCD.printf("Pick a number 1-6\nthen roll the die.\n\n");
Kimbenn 10:5b1f8be6c4f3 196 Thread::wait(200);
Kimbenn 10:5b1f8be6c4f3 197 uLCD.printf("If you want to\nchange the dot\nsize or color,\nyou can with the\npots.");
Kimbenn 10:5b1f8be6c4f3 198 Thread::wait(600);
Kimbenn 10:5b1f8be6c4f3 199 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 200 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 201 player_guess = 1;
Kimbenn 10:5b1f8be6c4f3 202 led1 = 1;
Kimbenn 10:5b1f8be6c4f3 203 while(game_state == 1) {
Kimbenn 10:5b1f8be6c4f3 204
Kimbenn 10:5b1f8be6c4f3 205 // if accelerometer changes enough, go to next state
Kimbenn 10:5b1f8be6c4f3 206 acc.readXYZGravity(&xg,&yg,&zg);
Kimbenn 10:5b1f8be6c4f3 207 zg = abs(zg);
Kimbenn 10:5b1f8be6c4f3 208 if(zg>1.1f || zg < 0.75f){
Kimbenn 10:5b1f8be6c4f3 209 game_state = 2;
Kimbenn 10:5b1f8be6c4f3 210 }
Kimbenn 10:5b1f8be6c4f3 211 dice_color = (floor(100.0f * float(pot1))/100.0f);
Kimbenn 10:5b1f8be6c4f3 212 dice_color = dice_color * 16777215.0f;
Kimbenn 10:5b1f8be6c4f3 213 if (float(photocell) < 0.4f) {
Kimbenn 10:5b1f8be6c4f3 214 dice_color = RED;
Kimbenn 10:5b1f8be6c4f3 215 }
Kimbenn 10:5b1f8be6c4f3 216
Kimbenn 10:5b1f8be6c4f3 217 dot_size = ((floor(100.0f * float(pot2))/100.0f)*10 )+2;
Kimbenn 10:5b1f8be6c4f3 218
Kimbenn 10:5b1f8be6c4f3 219 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 220 uLCD.filled_rectangle(20, 20, 108, 108, dice_color);
Kimbenn 10:5b1f8be6c4f3 221 uLCD.filled_circle(64,64,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 222 uLCD.filled_circle(88,40,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 223 uLCD.filled_circle(40,88,dot_size,BLACK);
Kimbenn 10:5b1f8be6c4f3 224 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 225 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 226 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 227
Kimbenn 10:5b1f8be6c4f3 228 switch (player_guess){
Kimbenn 10:5b1f8be6c4f3 229 case 1: one_led();
Kimbenn 10:5b1f8be6c4f3 230 break;
Kimbenn 10:5b1f8be6c4f3 231 case 2: two_led();
Kimbenn 10:5b1f8be6c4f3 232 break;
Kimbenn 10:5b1f8be6c4f3 233 case 3: three_led();
Kimbenn 10:5b1f8be6c4f3 234 break;
Kimbenn 10:5b1f8be6c4f3 235 case 4: four_led();
Kimbenn 10:5b1f8be6c4f3 236 break;
Kimbenn 10:5b1f8be6c4f3 237 case 5: five_led();
Kimbenn 10:5b1f8be6c4f3 238 break;
Kimbenn 10:5b1f8be6c4f3 239 case 6: six_led();
Kimbenn 10:5b1f8be6c4f3 240 break;
Kimbenn 10:5b1f8be6c4f3 241 }
Kimbenn 10:5b1f8be6c4f3 242
Kimbenn 10:5b1f8be6c4f3 243 }
4180_1 5:a1ef40ff0f78 244 }
Kimbenn 10:5b1f8be6c4f3 245 Thread::wait(1000);
4180_1 5:a1ef40ff0f78 246 }
4180_1 6:f752accd632c 247 }
Kimbenn 10:5b1f8be6c4f3 248 ////////////////////////////////////////////////////////////////////////////////
4180_1 7:7bd7397ab89f 249
Kimbenn 10:5b1f8be6c4f3 250 ////////////// THIRD THREAD ////////////////////////////////////////////////////
Kimbenn 10:5b1f8be6c4f3 251 void view_result_high_five(void const *args) {
Kimbenn 10:5b1f8be6c4f3 252 while(true){
Kimbenn 10:5b1f8be6c4f3 253 if(game_state == 2){
Kimbenn 10:5b1f8be6c4f3 254
Kimbenn 10:5b1f8be6c4f3 255 audio = fopen("/sd/roll.wav","r");
Kimbenn 10:5b1f8be6c4f3 256 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 257 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 258
Kimbenn 10:5b1f8be6c4f3 259 result_rolled = rand()%6 + 1;
Kimbenn 10:5b1f8be6c4f3 260 switch(int(result_rolled)){
Kimbenn 10:5b1f8be6c4f3 261 case 1: dice1();
Kimbenn 10:5b1f8be6c4f3 262 audio = fopen("/sd/one.wav", "r");
Kimbenn 10:5b1f8be6c4f3 263 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 264 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 265 break;
Kimbenn 10:5b1f8be6c4f3 266 case 2: dice2();
Kimbenn 10:5b1f8be6c4f3 267 audio = fopen("/sd/two.wav", "r");
Kimbenn 10:5b1f8be6c4f3 268 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 269 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 270 break;
Kimbenn 10:5b1f8be6c4f3 271 case 3: dice3();
Kimbenn 10:5b1f8be6c4f3 272 audio = fopen("/sd/three.wav", "r");
Kimbenn 10:5b1f8be6c4f3 273 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 274 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 275 break;
Kimbenn 10:5b1f8be6c4f3 276 case 4: dice4();
Kimbenn 10:5b1f8be6c4f3 277 audio = fopen("/sd/four.wav", "r");
Kimbenn 10:5b1f8be6c4f3 278 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 279 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 280 break;
Kimbenn 10:5b1f8be6c4f3 281 case 5: dice5();
Kimbenn 10:5b1f8be6c4f3 282 audio = fopen("/sd/five.wav", "r");
Kimbenn 10:5b1f8be6c4f3 283 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 284 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 285 break;
Kimbenn 10:5b1f8be6c4f3 286 case 6: dice6();
Kimbenn 10:5b1f8be6c4f3 287 audio = fopen("/sd/six.wav", "r");
Kimbenn 10:5b1f8be6c4f3 288 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 289 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 290 break;}
Kimbenn 10:5b1f8be6c4f3 291 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 292
Kimbenn 10:5b1f8be6c4f3 293 while(game_state == 2){
Kimbenn 10:5b1f8be6c4f3 294 if(player_guess == result_rolled){
Kimbenn 10:5b1f8be6c4f3 295 audio = fopen("/sd/won.wav", "r");
Kimbenn 10:5b1f8be6c4f3 296 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 297 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 298 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 299 uLCD.printf(" You WIN!!\n\n High five!");
Kimbenn 10:5b1f8be6c4f3 300 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 301 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 302 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 303 for(float p=0; p<1.0; p += 0.01) {
Kimbenn 10:5b1f8be6c4f3 304 myservo = p;
Kimbenn 10:5b1f8be6c4f3 305 Thread::wait(0.2);
Kimbenn 10:5b1f8be6c4f3 306 }
Kimbenn 10:5b1f8be6c4f3 307
Kimbenn 10:5b1f8be6c4f3 308 Thread::wait(4000);
Kimbenn 10:5b1f8be6c4f3 309
Kimbenn 10:5b1f8be6c4f3 310 for(float p=1; p>0.0; p -= 0.01) {
Kimbenn 10:5b1f8be6c4f3 311 myservo = p;
Kimbenn 10:5b1f8be6c4f3 312 Thread::wait(0.2);
Kimbenn 10:5b1f8be6c4f3 313 }
Kimbenn 10:5b1f8be6c4f3 314 ///////////////////////////////////////
Kimbenn 10:5b1f8be6c4f3 315 }
Kimbenn 10:5b1f8be6c4f3 316 else if(player_guess != result_rolled) {
Kimbenn 10:5b1f8be6c4f3 317 audio = fopen("/sd/lost.wav", "r");
Kimbenn 10:5b1f8be6c4f3 318 waver.play(audio);
Kimbenn 10:5b1f8be6c4f3 319 fclose(audio);
Kimbenn 10:5b1f8be6c4f3 320 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 321 uLCD.printf(" you lose.....");
Kimbenn 10:5b1f8be6c4f3 322 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 323 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 324 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 325 }
Kimbenn 10:5b1f8be6c4f3 326 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 327 uLCD.printf("Press the button\nto play again!");
Kimbenn 10:5b1f8be6c4f3 328 while(game_state == 2){Thread::wait(250);}
Kimbenn 10:5b1f8be6c4f3 329 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 330 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 331 }
Kimbenn 10:5b1f8be6c4f3 332 }
Kimbenn 10:5b1f8be6c4f3 333 }
Kimbenn 10:5b1f8be6c4f3 334 }
Kimbenn 10:5b1f8be6c4f3 335 ///////////////////////////////////////////////////////////////////////////////
4180_1 8:31e63caf37e2 336
Kimbenn 10:5b1f8be6c4f3 337 int main(){
Kimbenn 10:5b1f8be6c4f3 338 pb1.mode(PullDown);
Kimbenn 10:5b1f8be6c4f3 339 pb1.attach_deasserted(&guess_down);
Kimbenn 10:5b1f8be6c4f3 340 pb1.setSampleFrequency();
Kimbenn 10:5b1f8be6c4f3 341
Kimbenn 10:5b1f8be6c4f3 342 pb2.mode(PullDown);
Kimbenn 10:5b1f8be6c4f3 343 pb2.attach_deasserted(&guess_up);
Kimbenn 10:5b1f8be6c4f3 344 pb2.setSampleFrequency();
Kimbenn 10:5b1f8be6c4f3 345
Kimbenn 10:5b1f8be6c4f3 346 pb3.mode(PullDown);
Kimbenn 10:5b1f8be6c4f3 347 pb3.attach_deasserted(&next_state);
Kimbenn 10:5b1f8be6c4f3 348 pb3.setSampleFrequency();
Kimbenn 10:5b1f8be6c4f3 349
Kimbenn 10:5b1f8be6c4f3 350 Thread t2(input_guess_change_settings);
Kimbenn 10:5b1f8be6c4f3 351 Thread t3(view_result_high_five);
Kimbenn 10:5b1f8be6c4f3 352
Kimbenn 10:5b1f8be6c4f3 353 while(1){
Kimbenn 10:5b1f8be6c4f3 354 if (game_state == 0){
Kimbenn 10:5b1f8be6c4f3 355 myservo = 0;
Kimbenn 10:5b1f8be6c4f3 356 lcd_mutex.lock();
Kimbenn 10:5b1f8be6c4f3 357 uLCD.printf("\n\n Welcome!\n\n\nPress the button\nto start.");
Kimbenn 10:5b1f8be6c4f3 358 Thread::wait(1000);
Kimbenn 10:5b1f8be6c4f3 359 while(game_state == 0){Thread::wait(250);}
Kimbenn 10:5b1f8be6c4f3 360 uLCD.cls();
Kimbenn 10:5b1f8be6c4f3 361 lcd_mutex.unlock();
Kimbenn 10:5b1f8be6c4f3 362 }
Kimbenn 10:5b1f8be6c4f3 363 Thread::wait(250);
Kimbenn 10:5b1f8be6c4f3 364
Kimbenn 10:5b1f8be6c4f3 365 }
Kimbenn 10:5b1f8be6c4f3 366 }