Finished!

Dependencies:   m3pi_ng mbed

Fork of BlueToothStuff by der Roboter

Committer:
satyson
Date:
Tue May 27 13:20:56 2014 +0000
Revision:
3:bf9a0cd9750c
Parent:
2:cc7b01c5a9b0
Finished I believe

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmpeter 0:386c250325ce 1 #include "mbed.h"
mmpeter 0:386c250325ce 2 #include "m3pi_ng.h"
mmpeter 0:386c250325ce 3 #include "cmath"
mmpeter 0:386c250325ce 4 #include "iostream"
satyson 1:6402638c6f6d 5 #include "btbee.h"
mmpeter 0:386c250325ce 6
satyson 3:bf9a0cd9750c 7
mmpeter 0:386c250325ce 8 using namespace std;
mmpeter 0:386c250325ce 9
mmpeter 0:386c250325ce 10 m3pi thinggy;
satyson 1:6402638c6f6d 11 btbee btbee;
satyson 1:6402638c6f6d 12
satyson 1:6402638c6f6d 13 DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)};
satyson 1:6402638c6f6d 14 DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)};
satyson 1:6402638c6f6d 15 DigitalIn m3pi_pb(p21);
mmpeter 0:386c250325ce 16
mmpeter 0:386c250325ce 17 int main() {
mmpeter 0:386c250325ce 18
satyson 1:6402638c6f6d 19 //..................................................................................................\\
satyson 1:6402638c6f6d 20 //......BlueTooth Communication.....................................................................\\
satyson 1:6402638c6f6d 21 //..................................................................................................\\
satyson 3:bf9a0cd9750c 22
satyson 3:bf9a0cd9750c 23 // initialization stuff ////////////////////////////////////////////////////////////////////////////////////////////////////
satyson 1:6402638c6f6d 24 thinggy.locate(0,1);
satyson 1:6402638c6f6d 25 btbee.reset();
satyson 1:6402638c6f6d 26 for (int i = 0; i <4; i++) {
satyson 1:6402638c6f6d 27 mbed_led[i] = 0;
satyson 1:6402638c6f6d 28 }
satyson 1:6402638c6f6d 29 for (int i = 0; i <8; i++) {
satyson 1:6402638c6f6d 30 m3pi_led[i]=0;
satyson 1:6402638c6f6d 31 }
satyson 1:6402638c6f6d 32 m3pi_pb.mode(PullUp); // expected would be 1 when pb is pressed, 0 when not, opposite is the case
satyson 1:6402638c6f6d 33
satyson 1:6402638c6f6d 34 // end initialization stuff ////////////////////////////////////////////////////////////////////////////////////////////////
satyson 1:6402638c6f6d 35
satyson 3:bf9a0cd9750c 36
satyson 1:6402638c6f6d 37 thinggy.locate(0,0);
satyson 3:bf9a0cd9750c 38 thinggy.printf("%s","ThySeeMe");
satyson 1:6402638c6f6d 39 thinggy.locate(0,1);
satyson 3:bf9a0cd9750c 40 thinggy.printf("%s","Rolling");
satyson 1:6402638c6f6d 41
satyson 1:6402638c6f6d 42 // wait for the user to push P21, should be pressed when the bt link is established (green led "link")
satyson 1:6402638c6f6d 43 while(m3pi_pb) {
satyson 1:6402638c6f6d 44 m3pi_led[0]=!m3pi_led[0];
satyson 1:6402638c6f6d 45 wait(0.1);
satyson 1:6402638c6f6d 46 }
satyson 1:6402638c6f6d 47
satyson 1:6402638c6f6d 48 int iline=1;
satyson 1:6402638c6f6d 49 char arr_read[30]; // this should be long enough to store any reply coming in over bt.
satyson 1:6402638c6f6d 50 int chars_read; // number of chars read in a bt reply
satyson 3:bf9a0cd9750c 51
satyson 3:bf9a0cd9750c 52 while (true) {
satyson 1:6402638c6f6d 53 // this writes "Line 001\n" to "Line 005\n" and then "end\n" to the btbee
satyson 1:6402638c6f6d 54 if ( btbee.writeable() ) {
satyson 1:6402638c6f6d 55 if (iline==6) {
satyson 1:6402638c6f6d 56 btbee.printf("Your Turn!\n");
satyson 1:6402638c6f6d 57 iline++;
satyson 1:6402638c6f6d 58 }//if
satyson 1:6402638c6f6d 59 else {
satyson 1:6402638c6f6d 60 if (iline <6){
satyson 1:6402638c6f6d 61 btbee.printf("Line %0.3d \n",iline);
satyson 3:bf9a0cd9750c 62 m3pi_led[0]=0;
satyson 1:6402638c6f6d 63 thinggy.locate(0,0);
satyson 1:6402638c6f6d 64 thinggy.printf("Sent %0.3d",iline);
satyson 1:6402638c6f6d 65 iline++;
satyson 1:6402638c6f6d 66 }
satyson 1:6402638c6f6d 67 }//else
satyson 1:6402638c6f6d 68 }//if_write
satyson 3:bf9a0cd9750c 69
satyson 1:6402638c6f6d 70 // check for answers after each write /not write
satyson 1:6402638c6f6d 71 while ( btbee.readable() ) {
satyson 1:6402638c6f6d 72 btbee.read_all(arr_read, 30, &chars_read );
satyson 2:cc7b01c5a9b0 73 thinggy.locate(0,0);
satyson 2:cc7b01c5a9b0 74 thinggy.cls();
satyson 2:cc7b01c5a9b0 75 thinggy.printf("IGotMsg!");
satyson 2:cc7b01c5a9b0 76 if (arr_read[0] == 'l'){
satyson 2:cc7b01c5a9b0 77 thinggy.locate(0,1);
satyson 2:cc7b01c5a9b0 78 thinggy.cls();
satyson 2:cc7b01c5a9b0 79 thinggy.printf("TurningL");
satyson 3:bf9a0cd9750c 80 thinggy.left_motor(0.2);
satyson 3:bf9a0cd9750c 81 thinggy.right_motor(-0.2);
satyson 2:cc7b01c5a9b0 82 }
satyson 2:cc7b01c5a9b0 83 else if (arr_read[0] == 'r'){
satyson 2:cc7b01c5a9b0 84 thinggy.locate(0,1);
satyson 2:cc7b01c5a9b0 85 thinggy.cls();
satyson 2:cc7b01c5a9b0 86 thinggy.printf("TurningR");
satyson 3:bf9a0cd9750c 87 thinggy.left_motor(-0.2);
satyson 3:bf9a0cd9750c 88 thinggy.right_motor(0.2);
satyson 2:cc7b01c5a9b0 89 // add code to turn right
satyson 2:cc7b01c5a9b0 90 }
satyson 2:cc7b01c5a9b0 91 else if (arr_read[0] == 'f'){
satyson 2:cc7b01c5a9b0 92 thinggy.locate(0,1);
satyson 2:cc7b01c5a9b0 93 thinggy.cls();
satyson 2:cc7b01c5a9b0 94 thinggy.printf("GoingFwd");
satyson 3:bf9a0cd9750c 95 thinggy.forward(0.1);
satyson 2:cc7b01c5a9b0 96 }
satyson 3:bf9a0cd9750c 97 else if (arr_read[0] == 's'){
satyson 3:bf9a0cd9750c 98 thinggy.locate(0,1);
satyson 3:bf9a0cd9750c 99 thinggy.cls();
satyson 3:bf9a0cd9750c 100 thinggy.printf("Stopping");
satyson 3:bf9a0cd9750c 101 thinggy.stop();
satyson 3:bf9a0cd9750c 102 }
satyson 1:6402638c6f6d 103 }//while_readable
satyson 1:6402638c6f6d 104 wait(0.1);
satyson 1:6402638c6f6d 105 }//while_true
satyson 3:bf9a0cd9750c 106
satyson 3:bf9a0cd9750c 107 }//main