Stephen Tyson / Mbed 2 deprecated BlueToothStuff_v2

Dependencies:   m3pi_ng mbed

Fork of BlueToothStuff by Stephen Tyson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "m3pi_ng.h"
00003 #include "cmath"
00004 #include "iostream"
00005 #include "btbee.h"
00006  
00007  
00008 using namespace std;
00009  
00010 m3pi thinggy;
00011 btbee btbee; 
00012 
00013 DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)};
00014 DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)};
00015 DigitalIn m3pi_pb(p21);
00016  
00017 int main() {
00018     
00019     //..................................................................................................\\
00020     //......BlueTooth Communication.....................................................................\\
00021     //..................................................................................................\\
00022 
00023    // initialization stuff ////////////////////////////////////////////////////////////////////////////////////////////////////
00024    thinggy.locate(0,1);
00025    btbee.reset();
00026    for (int i = 0; i <4; i++) {
00027        mbed_led[i] = 0;
00028    }
00029    for (int i = 0; i <8; i++) {
00030        m3pi_led[i]=0;
00031    }
00032    m3pi_pb.mode(PullUp); // expected would be 1 when pb is pressed, 0 when not, opposite is the case
00033 
00034    // end initialization stuff ////////////////////////////////////////////////////////////////////////////////////////////////
00035 
00036   
00037    thinggy.locate(0,0);
00038    thinggy.printf("%s","ThySeeMe");
00039    thinggy.locate(0,1);
00040    thinggy.printf("%s","Rolling");
00041 
00042    // wait for the user to push P21, should be pressed when the bt link is established (green led "link")
00043    while(m3pi_pb) {
00044        m3pi_led[0]=!m3pi_led[0];
00045        wait(0.1);
00046    }
00047 
00048    int iline=1;
00049    char arr_read[30]; // this should be long enough to store any reply coming in over bt.
00050    int  chars_read;   // number of chars read in a bt reply
00051    
00052    while (true) {
00053        // this writes "Line 001\n" to "Line 005\n" and then "end\n" to the btbee
00054        if ( btbee.writeable() ) {
00055            if (iline==6) {
00056                btbee.printf("Your Turn!\n");
00057                iline++;
00058            }//if
00059            else {
00060                if (iline <6){
00061                btbee.printf("Line %0.3d \n",iline);
00062                m3pi_led[0]=0;
00063                thinggy.locate(0,0);
00064                thinggy.printf("Sent %0.3d",iline);
00065                iline++;
00066                }
00067            }//else
00068        }//if_write
00069 
00070        // check for answers after each write /not write
00071        while ( btbee.readable() ) {
00072            btbee.read_all(arr_read, 30, &chars_read );
00073            thinggy.locate(0,0);
00074            thinggy.cls();
00075            thinggy.printf("IGotMsg!");
00076            if (arr_read[0] == 'l'){
00077                thinggy.locate(0,1);
00078                thinggy.cls();
00079                thinggy.printf("TurningL");
00080                thinggy.left_motor(0.2);
00081                thinggy.right_motor(-0.2);
00082                }
00083            else if (arr_read[0] == 'r'){
00084                thinggy.locate(0,1);
00085                thinggy.cls();
00086                thinggy.printf("TurningR");
00087                thinggy.left_motor(-0.2);
00088                thinggy.right_motor(0.2);
00089                // add code to turn right
00090                }
00091            else if (arr_read[0] == 'f'){
00092                thinggy.locate(0,1);
00093                thinggy.cls();
00094                thinggy.printf("GoingFwd");
00095                thinggy.forward(0.1);
00096                }
00097            else if (arr_read[0] == 's'){
00098                thinggy.locate(0,1);
00099                thinggy.cls();
00100                thinggy.printf("Stopping");
00101                thinggy.stop();
00102                }    
00103        }//while_readable
00104        wait(0.1);
00105    }//while_true
00106 
00107 }//main