Team Ascent / Mbed 2 deprecated FinalBluetoothFunction

Dependencies:   mbed

Committer:
fraserhmbed
Date:
Wed Mar 11 21:52:14 2015 +0000
Revision:
0:f54028dab58f
Child:
1:c4af0bbe20b9
Not completely finished yet, still needs work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fraserhmbed 0:f54028dab58f 1
fraserhmbed 0:f54028dab58f 2 /* what target am i at?
fraserhmbed 0:f54028dab58f 3 wait for input from phone
fraserhmbed 0:f54028dab58f 4 if input is 1 or 2 then return number to choose target choice function which then fires at target
fraserhmbed 0:f54028dab58f 5 did i hit the target?
fraserhmbed 0:f54028dab58f 6 wait for input from phone
fraserhmbed 0:f54028dab58f 7 if input is yes then go back to maze navigation function
fraserhmbed 0:f54028dab58f 8 if input is no then try again
fraserhmbed 0:f54028dab58f 9 ********************************************************** */
fraserhmbed 0:f54028dab58f 10
fraserhmbed 0:f54028dab58f 11 #include "mbed.h"
fraserhmbed 0:f54028dab58f 12
fraserhmbed 0:f54028dab58f 13 Serial blue(PTC4,PTC3);
fraserhmbed 0:f54028dab58f 14 DigitalOut led(LED1);
fraserhmbed 0:f54028dab58f 15
fraserhmbed 0:f54028dab58f 16 // which target are we at?
fraserhmbed 0:f54028dab58f 17 int TargetChoice(){
fraserhmbed 0:f54028dab58f 18
fraserhmbed 0:f54028dab58f 19 blue.printf("Which target am I at?"); // ask user question
fraserhmbed 0:f54028dab58f 20 char TargetIn = blue.getc(); // assign user input to TargetIn
fraserhmbed 0:f54028dab58f 21 int command=0, choice = 0;
fraserhmbed 0:f54028dab58f 22
fraserhmbed 0:f54028dab58f 23 while(command==0) { // loop until a command has been given
fraserhmbed 0:f54028dab58f 24 switch (TargetIn) { // input is either 1 or 2 in DECIMAL
fraserhmbed 0:f54028dab58f 25 case 0x31: // if target 1
fraserhmbed 0:f54028dab58f 26 choice=1; //
fraserhmbed 0:f54028dab58f 27 command = 1; // exit while
fraserhmbed 0:f54028dab58f 28 break;
fraserhmbed 0:f54028dab58f 29 case 0x32: // if target 2
fraserhmbed 0:f54028dab58f 30 choice=2; // return 2 to ball firing or call ball fire 2
fraserhmbed 0:f54028dab58f 31 command = 1; // exit while
fraserhmbed 0:f54028dab58f 32 break; }
fraserhmbed 0:f54028dab58f 33
fraserhmbed 0:f54028dab58f 34 }
fraserhmbed 0:f54028dab58f 35 return choice; // return which target to fire at
fraserhmbed 0:f54028dab58f 36 }
fraserhmbed 0:f54028dab58f 37
fraserhmbed 0:f54028dab58f 38 // did we hit the target
fraserhmbed 0:f54028dab58f 39 int SuccessCheck(){
fraserhmbed 0:f54028dab58f 40
fraserhmbed 0:f54028dab58f 41 blue.printf("Did I hit the target?"); // ask user question
fraserhmbed 0:f54028dab58f 42 char SuccessIn = blue.getc(); // assign user input to SuccessIn
fraserhmbed 0:f54028dab58f 43 int command=0, success=0;
fraserhmbed 0:f54028dab58f 44
fraserhmbed 0:f54028dab58f 45 while(command==0) { // loop until a command has been given
fraserhmbed 0:f54028dab58f 46 switch (SuccessIn) { // input either y (0x79) or n (0x6E)
fraserhmbed 0:f54028dab58f 47 case 0x79: // if input is y
fraserhmbed 0:f54028dab58f 48 success=1; // return 1 directs to go back to the maze and locate next target
fraserhmbed 0:f54028dab58f 49 command = 1;
fraserhmbed 0:f54028dab58f 50 break;
fraserhmbed 0:f54028dab58f 51
fraserhmbed 0:f54028dab58f 52 case 0x6E: // if input is n
fraserhmbed 0:f54028dab58f 53 success=0; // return 0 to go back to trying to hit the target
fraserhmbed 0:f54028dab58f 54 command = 1;
fraserhmbed 0:f54028dab58f 55 break; }
fraserhmbed 0:f54028dab58f 56
fraserhmbed 0:f54028dab58f 57 }
fraserhmbed 0:f54028dab58f 58 return success;
fraserhmbed 0:f54028dab58f 59 }
fraserhmbed 0:f54028dab58f 60
fraserhmbed 0:f54028dab58f 61 //main bluetooth operation function
fraserhmbed 0:f54028dab58f 62 void MazeBluetoothing() {
fraserhmbed 0:f54028dab58f 63
fraserhmbed 0:f54028dab58f 64 // int bluetooth=0; does this still initialise int bluetooth to 0? for the while loop?
fraserhmbed 0:f54028dab58f 65 while(int bluetooth=0){
fraserhmbed 0:f54028dab58f 66
fraserhmbed 0:f54028dab58f 67 int target = TargetChoice(); // call target choosing function
fraserhmbed 0:f54028dab58f 68
fraserhmbed 0:f54028dab58f 69 // if target = 1 then call function to fire at target 1, if target = 2 then call fire function 2
fraserhmbed 0:f54028dab58f 70 switch (target) {
fraserhmbed 0:f54028dab58f 71 case 1:
fraserhmbed 0:f54028dab58f 72 //call target 1 function
fraserhmbed 0:f54028dab58f 73 break;
fraserhmbed 0:f54028dab58f 74
fraserhmbed 0:f54028dab58f 75 case 2:
fraserhmbed 0:f54028dab58f 76 //call target 2 function
fraserhmbed 0:f54028dab58f 77 break;
fraserhmbed 0:f54028dab58f 78 }
fraserhmbed 0:f54028dab58f 79 int success = SuccessCheck();
fraserhmbed 0:f54028dab58f 80
fraserhmbed 0:f54028dab58f 81 // if success is 0, then reposition & go back to start of bluetoothing()
fraserhmbed 0:f54028dab58f 82 switch (success) {
fraserhmbed 0:f54028dab58f 83 case 0:
fraserhmbed 0:f54028dab58f 84 // call repositioning functionthen break to go back to loop of bluetoothing
fraserhmbed 0:f54028dab58f 85 break;
fraserhmbed 0:f54028dab58f 86
fraserhmbed 0:f54028dab58f 87 case 1:
fraserhmbed 0:f54028dab58f 88 bluetooth=1;
fraserhmbed 0:f54028dab58f 89 break;
fraserhmbed 0:f54028dab58f 90
fraserhmbed 0:f54028dab58f 91 }
fraserhmbed 0:f54028dab58f 92 }
fraserhmbed 0:f54028dab58f 93 }
fraserhmbed 0:f54028dab58f 94
fraserhmbed 0:f54028dab58f 95
fraserhmbed 0:f54028dab58f 96 //motor select pins
fraserhmbed 0:f54028dab58f 97 DigitalOut motor_lf(PTE2);
fraserhmbed 0:f54028dab58f 98 DigitalOut motor_lb(PTE3);
fraserhmbed 0:f54028dab58f 99 DigitalOut motor_rf(PTE4);
fraserhmbed 0:f54028dab58f 100 DigitalOut motor_rb(PTE5);
fraserhmbed 0:f54028dab58f 101
fraserhmbed 0:f54028dab58f 102 //Frequency of Pulse Width Modulated signal in Hz
fraserhmbed 0:f54028dab58f 103 #define PWM_FREQ 1000
fraserhmbed 0:f54028dab58f 104
fraserhmbed 0:f54028dab58f 105 //PWM pin (Enable 1 and 2)
fraserhmbed 0:f54028dab58f 106 PwmOut motor_l (PTC2);
fraserhmbed 0:f54028dab58f 107 PwmOut motor_r (PTE29);
fraserhmbed 0:f54028dab58f 108
fraserhmbed 0:f54028dab58f 109 //LED to test
fraserhmbed 0:f54028dab58f 110 DigitalOut myled(LED_BLUE);
fraserhmbed 0:f54028dab58f 111
fraserhmbed 0:f54028dab58f 112 void set_direction( int direction, float speed, float angle)
fraserhmbed 0:f54028dab58f 113 {
fraserhmbed 0:f54028dab58f 114 /* *******************Example Use*****************
fraserhmbed 0:f54028dab58f 115 set_direction("11", 0.5, 0); //This would set the robot to go forward, at half speed, straight
fraserhmbed 0:f54028dab58f 116
fraserhmbed 0:f54028dab58f 117 set_directin( "11", 0.8, 0.2); //this would set robot to go forward, at 100% to the right and 60% to the left (turn right)
fraserhmbed 0:f54028dab58f 118
fraserhmbed 0:f54028dab58f 119 set_direction("00", 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)
fraserhmbed 0:f54028dab58f 120
fraserhmbed 0:f54028dab58f 121 set_direction("11", 0.5, 0.5); // robot pivots forward, to the right, full speed (100% duty right ; 0% duty left)
fraserhmbed 0:f54028dab58f 122
fraserhmbed 0:f54028dab58f 123 set_direction("11", 0.5, -0.5); //robot pivots forward, to the left, full speed (0% duty right ; 100% duty left)
fraserhmbed 0:f54028dab58f 124
fraserhmbed 0:f54028dab58f 125 set_direction("11", 0.3, 0.5); //robot pivots forard, to the right , at 80% speed (80% right; 0% left)
fraserhmbed 0:f54028dab58f 126
fraserhmbed 0:f54028dab58f 127 set_direction("11", -0.3, 0.5); //robot pivotrs foward, to the right, AT 20% SPEED (20%, 0%)
fraserhmbed 0:f54028dab58f 128
fraserhmbed 0:f54028dab58f 129 set_direction("10", 0.5, 0); //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%
fraserhmbed 0:f54028dab58f 130
fraserhmbed 0:f54028dab58f 131 set_direction("10", 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
fraserhmbed 0:f54028dab58f 132
fraserhmbed 0:f54028dab58f 133 */
fraserhmbed 0:f54028dab58f 134
fraserhmbed 0:f54028dab58f 135 float r_result = (speed + angle); //where angle can be postive and negative and will TURN the robot
fraserhmbed 0:f54028dab58f 136 float l_result = (speed - angle); //if turning left, you'd make the angle postive (right motor goes faster left motor goes slwoer)
fraserhmbed 0:f54028dab58f 137 switch( direction ) {
fraserhmbed 0:f54028dab58f 138 case 0x11: { //forward
fraserhmbed 0:f54028dab58f 139 motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result);
fraserhmbed 0:f54028dab58f 140 //Look up ternary statements -- {condition ? value_if_true : value_if_false} This makes 0<result<1
fraserhmbed 0:f54028dab58f 141 motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result);
fraserhmbed 0:f54028dab58f 142
fraserhmbed 0:f54028dab58f 143 motor_rf=1;
fraserhmbed 0:f54028dab58f 144 motor_rb=0;
fraserhmbed 0:f54028dab58f 145 motor_lf=1;
fraserhmbed 0:f54028dab58f 146 motor_lb=0;
fraserhmbed 0:f54028dab58f 147 }
fraserhmbed 0:f54028dab58f 148 case 0x00: { //backward
fraserhmbed 0:f54028dab58f 149 motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result);
fraserhmbed 0:f54028dab58f 150 motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result);
fraserhmbed 0:f54028dab58f 151
fraserhmbed 0:f54028dab58f 152 motor_rf=0;
fraserhmbed 0:f54028dab58f 153 motor_rb=1;
fraserhmbed 0:f54028dab58f 154 motor_lf=0;
fraserhmbed 0:f54028dab58f 155 motor_lb=1;
fraserhmbed 0:f54028dab58f 156 }
fraserhmbed 0:f54028dab58f 157 case 0x01: { //spin left -- Right forward, left backward
fraserhmbed 0:f54028dab58f 158 motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result);
fraserhmbed 0:f54028dab58f 159 motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result);
fraserhmbed 0:f54028dab58f 160
fraserhmbed 0:f54028dab58f 161 motor_rf=1;
fraserhmbed 0:f54028dab58f 162 motor_rb=0;
fraserhmbed 0:f54028dab58f 163 motor_lf=0;
fraserhmbed 0:f54028dab58f 164 motor_lb=1;
fraserhmbed 0:f54028dab58f 165 }
fraserhmbed 0:f54028dab58f 166 case 0x10: { //spin right
fraserhmbed 0:f54028dab58f 167 motor_r.write( r_result < 0 ? 0 : r_result > 1 ? 1 : r_result);
fraserhmbed 0:f54028dab58f 168 motor_l.write( l_result < 0 ? 0 : l_result > 1 ? 1 : l_result);
fraserhmbed 0:f54028dab58f 169
fraserhmbed 0:f54028dab58f 170 motor_rf=0;
fraserhmbed 0:f54028dab58f 171 motor_rb=1;
fraserhmbed 0:f54028dab58f 172 motor_lf=1;
fraserhmbed 0:f54028dab58f 173 motor_lb=0;
fraserhmbed 0:f54028dab58f 174 }
fraserhmbed 0:f54028dab58f 175 }
fraserhmbed 0:f54028dab58f 176 }
fraserhmbed 0:f54028dab58f 177
fraserhmbed 0:f54028dab58f 178
fraserhmbed 0:f54028dab58f 179 void SpinFunct() {
fraserhmbed 0:f54028dab58f 180 //Set PWM frequency to 1000Hz
fraserhmbed 0:f54028dab58f 181 motor_l.period( 1.0f / (float) PWM_FREQ);
fraserhmbed 0:f54028dab58f 182 motor_r.period( 1.0f / (float) PWM_FREQ);
fraserhmbed 0:f54028dab58f 183 //Initialise direction to nothing.
fraserhmbed 0:f54028dab58f 184 motor_rf=0;
fraserhmbed 0:f54028dab58f 185 motor_rb=0;
fraserhmbed 0:f54028dab58f 186 motor_lf=0;
fraserhmbed 0:f54028dab58f 187 motor_lb=0;
fraserhmbed 0:f54028dab58f 188 led=1;
fraserhmbed 0:f54028dab58f 189 set_direction(0x10, 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
fraserhmbed 0:f54028dab58f 190 wait(5); led=0;
fraserhmbed 0:f54028dab58f 191
fraserhmbed 0:f54028dab58f 192 }
fraserhmbed 0:f54028dab58f 193
fraserhmbed 0:f54028dab58f 194 void JiveFunct() {
fraserhmbed 0:f54028dab58f 195
fraserhmbed 0:f54028dab58f 196 //Set PWM frequency to 1000Hz
fraserhmbed 0:f54028dab58f 197 motor_l.period( 1.0f / (float) PWM_FREQ);
fraserhmbed 0:f54028dab58f 198 motor_r.period( 1.0f / (float) PWM_FREQ);
fraserhmbed 0:f54028dab58f 199 //Initialise direction to nothing.
fraserhmbed 0:f54028dab58f 200 motor_rf=0;
fraserhmbed 0:f54028dab58f 201 motor_rb=0;
fraserhmbed 0:f54028dab58f 202 motor_lf=0;
fraserhmbed 0:f54028dab58f 203 motor_lb=0;
fraserhmbed 0:f54028dab58f 204
fraserhmbed 0:f54028dab58f 205 //forward + right spin
fraserhmbed 0:f54028dab58f 206 wait(2);
fraserhmbed 0:f54028dab58f 207 myled = !myled;
fraserhmbed 0:f54028dab58f 208 set_direction(0x11, 0.3, 0.5); //robot pivots forard, to the right , at 80% speed (80% right; 0% left)
fraserhmbed 0:f54028dab58f 209
fraserhmbed 0:f54028dab58f 210 // back spin
fraserhmbed 0:f54028dab58f 211 wait(2);
fraserhmbed 0:f54028dab58f 212 myled = !myled;
fraserhmbed 0:f54028dab58f 213 set_direction(0x00, 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)
fraserhmbed 0:f54028dab58f 214
fraserhmbed 0:f54028dab58f 215 //
fraserhmbed 0:f54028dab58f 216 wait(2);
fraserhmbed 0:f54028dab58f 217 myled = !myled;
fraserhmbed 0:f54028dab58f 218 set_direction(0x01, 0.3, 0.4); //robot spins to teh left, at half speed. Left forward 70% ; Right 0% (technically -30%, but 0% is min)
fraserhmbed 0:f54028dab58f 219
fraserhmbed 0:f54028dab58f 220 wait(2);
fraserhmbed 0:f54028dab58f 221 myled = !myled;
fraserhmbed 0:f54028dab58f 222 set_direction(0x10, 0.5, 0); //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%
fraserhmbed 0:f54028dab58f 223
fraserhmbed 0:f54028dab58f 224
fraserhmbed 0:f54028dab58f 225 /*
fraserhmbed 0:f54028dab58f 226 wait(2);
fraserhmbed 0:f54028dab58f 227 led = !led;
fraserhmbed 0:f54028dab58f 228 set_direction(0x11, 0.5, 0); //This would set the robot to go forward, at half speed, straight
fraserhmbed 0:f54028dab58f 229
fraserhmbed 0:f54028dab58f 230 wait(2);
fraserhmbed 0:f54028dab58f 231 led = !led;
fraserhmbed 0:f54028dab58f 232 set_direction(0x11, 0.8, 0.2); //this would set robot to go forward, at 100% to the right and 60% to the left (turn right)
fraserhmbed 0:f54028dab58f 233
fraserhmbed 0:f54028dab58f 234 wait(2);
fraserhmbed 0:f54028dab58f 235 led = !led;
fraserhmbed 0:f54028dab58f 236 set_direction(0x11, 0.5, 0.5); // robot pivots forward, to the right, full speed (100% duty right ; 0% duty left)
fraserhmbed 0:f54028dab58f 237
fraserhmbed 0:f54028dab58f 238 wait(2);
fraserhmbed 0:f54028dab58f 239 led = !led;
fraserhmbed 0:f54028dab58f 240 set_direction(0x11, 0.5, -0.5); //robot pivots forward, to the left, full speed (0% duty right ; 100% duty left)
fraserhmbed 0:f54028dab58f 241
fraserhmbed 0:f54028dab58f 242
fraserhmbed 0:f54028dab58f 243
fraserhmbed 0:f54028dab58f 244 wait(2);
fraserhmbed 0:f54028dab58f 245 led = !led;
fraserhmbed 0:f54028dab58f 246 set_direction(0x11, -0.3, 0.5); //robot pivotrs foward, to the right, AT 20% SPEED (20%, 0%)
fraserhmbed 0:f54028dab58f 247
fraserhmbed 0:f54028dab58f 248 wait(2);
fraserhmbed 0:f54028dab58f 249 led = !led;
fraserhmbed 0:f54028dab58f 250 set_direction(0x10, 0.5, 0); //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%
fraserhmbed 0:f54028dab58f 251
fraserhmbed 0:f54028dab58f 252 wait(2);
fraserhmbed 0:f54028dab58f 253 led = !led;
fraserhmbed 0:f54028dab58f 254 set_direction(0x10, 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
fraserhmbed 0:f54028dab58f 255 */
fraserhmbed 0:f54028dab58f 256
fraserhmbed 0:f54028dab58f 257 }
fraserhmbed 0:f54028dab58f 258
fraserhmbed 0:f54028dab58f 259
fraserhmbed 0:f54028dab58f 260 // will just make a question at the end to say quit? yes or no. it's possible to make a quitter function and test the input for quit
fraserhmbed 0:f54028dab58f 261 // at every point but it's too annoying to get it to work... so fuk dat
fraserhmbed 0:f54028dab58f 262
fraserhmbed 0:f54028dab58f 263 void RemoteControl(){ char dir[2]; /*speed=0, angle=0, time=0, */ int quit=0;
fraserhmbed 0:f54028dab58f 264
fraserhmbed 0:f54028dab58f 265 while(quit==0) {
fraserhmbed 0:f54028dab58f 266 wait(1);
fraserhmbed 0:f54028dab58f 267 //dir
fraserhmbed 0:f54028dab58f 268 blue.printf("Direction?\n");
fraserhmbed 0:f54028dab58f 269 while ( blue.readable() ) {} // loops until there is an input from the user
fraserhmbed 0:f54028dab58f 270
fraserhmbed 0:f54028dab58f 271 for(int i=0; i<2; i++) { dir[2] = blue.getc(); } //Store incoming response into array
fraserhmbed 0:f54028dab58f 272 blue.printf("%c dir\n", dir);
fraserhmbed 0:f54028dab58f 273
fraserhmbed 0:f54028dab58f 274 }
fraserhmbed 0:f54028dab58f 275 }
fraserhmbed 0:f54028dab58f 276 //for(int i=0; i<4; i++) { quitarray[i]= blue.getc(); blue.printf("get %c", quitarray[i]); } //Store incoming response into array
fraserhmbed 0:f54028dab58f 277 //quit=Quitter(quitarray);
fraserhmbed 0:f54028dab58f 278
fraserhmbed 0:f54028dab58f 279 /*
fraserhmbed 0:f54028dab58f 280 //speed
fraserhmbed 0:f54028dab58f 281 blue.printf("Speed?");
fraserhmbed 0:f54028dab58f 282 while ( blue.readable() ) {} // loops until there is an input from the user
fraserhmbed 0:f54028dab58f 283 speed=blue.getc();
fraserhmbed 0:f54028dab58f 284 quit=Quitter();
fraserhmbed 0:f54028dab58f 285
fraserhmbed 0:f54028dab58f 286
fraserhmbed 0:f54028dab58f 287 //angle
fraserhmbed 0:f54028dab58f 288 blue.printf("Angle?");
fraserhmbed 0:f54028dab58f 289 while (blue.readable()==0) {} // loops until there is an input from the user
fraserhmbed 0:f54028dab58f 290 angle=blue.getc();
fraserhmbed 0:f54028dab58f 291 quit=Quitter();
fraserhmbed 0:f54028dab58f 292
fraserhmbed 0:f54028dab58f 293
fraserhmbed 0:f54028dab58f 294 //time
fraserhmbed 0:f54028dab58f 295 blue.printf("How long?");
fraserhmbed 0:f54028dab58f 296 while (blue.readable()==0) {} // loops until there is an input from the user
fraserhmbed 0:f54028dab58f 297 time=blue.getc();
fraserhmbed 0:f54028dab58f 298 quit=Quitter();
fraserhmbed 0:f54028dab58f 299 */
fraserhmbed 0:f54028dab58f 300 //set_direction(dir, speed, angle); //also find a way to do time too
fraserhmbed 0:f54028dab58f 301 //wait(time);
fraserhmbed 0:f54028dab58f 302
fraserhmbed 0:f54028dab58f 303
fraserhmbed 0:f54028dab58f 304 /*
fraserhmbed 0:f54028dab58f 305
fraserhmbed 0:f54028dab58f 306 send 11, 0.5, 0.2 in one string
fraserhmbed 0:f54028dab58f 307 for the floats, it will send (num)(point)(num) ef 0.3 = 30, 2E, 33
fraserhmbed 0:f54028dab58f 308 all in ASCII remember
fraserhmbed 0:f54028dab58f 309
fraserhmbed 0:f54028dab58f 310 so for:
fraserhmbed 0:f54028dab58f 311 11 0.5 0.2
fraserhmbed 0:f54028dab58f 312 it will send:
fraserhmbed 0:f54028dab58f 313 31 31 20 30 2e 35 20 30 2e 32
fraserhmbed 0:f54028dab58f 314
fraserhmbed 0:f54028dab58f 315 sending 313120302e3520302e32 would cause a stack overload
fraserhmbed 0:f54028dab58f 316
fraserhmbed 0:f54028dab58f 317 going to have to split the user into 3 different lines of tx
fraserhmbed 0:f54028dab58f 318
fraserhmbed 0:f54028dab58f 319 so it'd be simpler too but more difficult for the user to control.
fraserhmbed 0:f54028dab58f 320 might need to add in a "for how long for?" function that will only drive the motors for a short period of time
fraserhmbed 0:f54028dab58f 321
fraserhmbed 0:f54028dab58f 322 user would have to be knowledgeable of the set direction function aka get daniel to do the demonstration
fraserhmbed 0:f54028dab58f 323
fraserhmbed 0:f54028dab58f 324 so then user sends: direction 11,10,01; speed 0.2,0.5,0.7; angle 0.2,0.4,0.8; time 1,2,3,4;
fraserhmbed 0:f54028dab58f 325
fraserhmbed 0:f54028dab58f 326 yes it is slow, but it allows for exact computation of distance, speed and angle
fraserhmbed 0:f54028dab58f 327
fraserhmbed 0:f54028dab58f 328 so 1 function with 4 different while loops
fraserhmbed 0:f54028dab58f 329
fraserhmbed 0:f54028dab58f 330 while user has not input a direction, loop again
fraserhmbed 0:f54028dab58f 331
fraserhmbed 0:f54028dab58f 332 one function : remote control
fraserhmbed 0:f54028dab58f 333 */
fraserhmbed 0:f54028dab58f 334
fraserhmbed 0:f54028dab58f 335 int main(){
fraserhmbed 0:f54028dab58f 336
fraserhmbed 0:f54028dab58f 337 int spin[4] = {0x73, 0x70, 0x69, 0x6e};
fraserhmbed 0:f54028dab58f 338 int jive[4] = {0x6a, 0x69, 0x76, 0x65};
fraserhmbed 0:f54028dab58f 339 int ctrl[4] = {0x63, 0x74, 0x72, 0x6c};
fraserhmbed 0:f54028dab58f 340 int maze[4] = {0x6d, 0x61, 0x7a, 0x65};
fraserhmbed 0:f54028dab58f 341 unsigned char x[4];
fraserhmbed 0:f54028dab58f 342
fraserhmbed 0:f54028dab58f 343 while (1) {
fraserhmbed 0:f54028dab58f 344 blue.printf("What will I do?\n"); // ask user question
fraserhmbed 0:f54028dab58f 345 while (blue.readable()==0) {} // loops until there is an input from the user
fraserhmbed 0:f54028dab58f 346
fraserhmbed 0:f54028dab58f 347 for(int i=0; i<4; i++) { x[i] = blue.getc(); } //Store incoming response into array
fraserhmbed 0:f54028dab58f 348
fraserhmbed 0:f54028dab58f 349 int FindSpin=0, FindJive=0, FindCtrl=0, FindMaze=0;
fraserhmbed 0:f54028dab58f 350
fraserhmbed 0:f54028dab58f 351 for(int s=0; s<4; s++) {
fraserhmbed 0:f54028dab58f 352 if (x[s]==spin[s]) { FindSpin++; }
fraserhmbed 0:f54028dab58f 353 if (x[s]==jive[s]) { FindJive++; }
fraserhmbed 0:f54028dab58f 354 if (x[s]==ctrl[s]) { FindCtrl++; }
fraserhmbed 0:f54028dab58f 355 if (x[s]==maze[s]) { FindMaze++; }
fraserhmbed 0:f54028dab58f 356 }
fraserhmbed 0:f54028dab58f 357
fraserhmbed 0:f54028dab58f 358 if (FindSpin==4) { SpinFunct();}
fraserhmbed 0:f54028dab58f 359 if (FindJive==4) { JiveFunct();}
fraserhmbed 0:f54028dab58f 360 if (FindCtrl==4) { RemoteControl() ;}
fraserhmbed 0:f54028dab58f 361 if (FindMaze==4) {led=1; wait(4); led=0;}
fraserhmbed 0:f54028dab58f 362 }
fraserhmbed 0:f54028dab58f 363 }
fraserhmbed 0:f54028dab58f 364
fraserhmbed 0:f54028dab58f 365
fraserhmbed 0:f54028dab58f 366
fraserhmbed 0:f54028dab58f 367
fraserhmbed 0:f54028dab58f 368
fraserhmbed 0:f54028dab58f 369
fraserhmbed 0:f54028dab58f 370
fraserhmbed 0:f54028dab58f 371
fraserhmbed 0:f54028dab58f 372
fraserhmbed 0:f54028dab58f 373
fraserhmbed 0:f54028dab58f 374 /*
fraserhmbed 0:f54028dab58f 375 int Quitter(char x[4]) { int quit=0; char quitting[4]={0x71, 0x75, 0x69, 0x74};
fraserhmbed 0:f54028dab58f 376
fraserhmbed 0:f54028dab58f 377 blue.printf ("works");
fraserhmbed 0:f54028dab58f 378 for(int i=0; i<4; i++) { x[i] = blue.getc(); } //Store incoming response into array
fraserhmbed 0:f54028dab58f 379 for(int s=0; s<4; s++) {
fraserhmbed 0:f54028dab58f 380 if (x[s]==quitting[s]) { quit++; }
fraserhmbed 0:f54028dab58f 381 }
fraserhmbed 0:f54028dab58f 382 if (quit==4) { quit=1;}
fraserhmbed 0:f54028dab58f 383 return quit;
fraserhmbed 0:f54028dab58f 384 }
fraserhmbed 0:f54028dab58f 385 */