Team Ascent / Mbed 2 deprecated FinalBluetoothFunction

Dependencies:   mbed

Revision:
1:c4af0bbe20b9
Parent:
0:f54028dab58f
Child:
2:054c41590f3f
--- a/main.cpp	Wed Mar 11 21:52:14 2015 +0000
+++ b/main.cpp	Wed Mar 11 22:41:53 2015 +0000
@@ -8,88 +8,93 @@
         if input is no then try again
  ********************************************************** */
 
-#include "mbed.h"              
- 
+#include "mbed.h"
+
 Serial blue(PTC4,PTC3);
 DigitalOut led(LED1);
 
-// which target are we at?        
-int TargetChoice(){
-    
+// which target are we at?
+int TargetChoice()
+{
+
     blue.printf("Which target am I at?");           // ask user question
     char TargetIn = blue.getc();                    // assign user input to TargetIn
     int command=0, choice = 0;
-    
-    while(command==0) {                             // loop until a command has been given  
-        switch (TargetIn) {                         // input is either 1 or 2 in DECIMAL    
+
+    while(command==0) {                             // loop until a command has been given
+        switch (TargetIn) {                         // input is either 1 or 2 in DECIMAL
             case 0x31:                              // if target 1
-                choice=1;                           // 
+                choice=1;                           //
                 command = 1;                        // exit while
-            break;
+                break;
             case 0x32:                              // if target 2
-                choice=2;                            // return 2 to ball firing or call ball fire 2 
+                choice=2;                            // return 2 to ball firing or call ball fire 2
                 command = 1;                        // exit while
-            break;    }
-                                      
+                break;
+        }
+
     }
     return choice;                                  // return which target to fire at
 }
 
 // did we hit the target
-int SuccessCheck(){
-        
+int SuccessCheck()
+{
+
     blue.printf("Did I hit the target?");           // ask user question
     char SuccessIn = blue.getc();                   // assign user input to SuccessIn
     int command=0, success=0;
-    
+
     while(command==0) {                             // loop until a command has been given
         switch (SuccessIn) {                        // input either y (0x79) or n (0x6E)
-            case 0x79:                              // if input is y           
-                success=1;                          // return 1 directs to go back to the maze and locate next target                              
+            case 0x79:                              // if input is y
+                success=1;                          // return 1 directs to go back to the maze and locate next target
                 command = 1;
-            break;  
-            
+                break;
+
             case 0x6E:                              // if input is n
-                success=0;                          // return 0 to go back to trying to hit the target                                
+                success=0;                          // return 0 to go back to trying to hit the target
                 command = 1;
-            break;    }
-         
+                break;
+        }
+
     }
- return success;
- }
-        
+    return success;
+}
+
 //main bluetooth operation function
-void MazeBluetoothing() {
-    
-  //  int bluetooth=0;  does this still initialise int bluetooth to 0? for the while loop?
-    while(int bluetooth=0){
-        
-             int target = TargetChoice();                                 // call target choosing function
-             
-              // if target = 1 then call function to fire at target 1, if target = 2 then call fire function 2
-            switch (target) {
-                    case 1:
-                    //call target 1 function
-                     break;
-            
-                     case 2:
-                     //call target 2 function
-                    break;   
-             }
-            int success = SuccessCheck();
+void MazeBluetoothing()
+{
+
+    //  int bluetooth=0;  does this still initialise int bluetooth to 0? for the while loop?
+    while(int bluetooth=0) {
+
+        int target = TargetChoice();                                 // call target choosing function
+
+        // if target = 1 then call function to fire at target 1, if target = 2 then call fire function 2
+        switch (target) {
+            case 1:
+                //call target 1 function
+                break;
 
-            // if success is 0, then reposition & go back to start of bluetoothing()
-            switch (success) {
-                    case 0:
-                    // call repositioning functionthen break to go back to loop of bluetoothing  
-                    break;
-            
-                    case 1:
-                    bluetooth=1;
-                    break;
-            
-            }
-     }
+            case 2:
+                //call target 2 function
+                break;
+        }
+        int success = SuccessCheck();
+
+        // if success is 0, then reposition & go back to start of bluetoothing()
+        switch (success) {
+            case 0:
+                // call repositioning functionthen break to go back to loop of bluetoothing
+                break;
+
+            case 1:
+                bluetooth=1;
+                break;
+
+        }
+    }
 }
 
 
@@ -176,7 +181,8 @@
 }
 
 
-void SpinFunct() {
+void SpinFunct()
+{
     //Set PWM frequency to 1000Hz
     motor_l.period( 1.0f / (float) PWM_FREQ);
     motor_r.period( 1.0f / (float) PWM_FREQ);
@@ -184,15 +190,17 @@
     motor_rf=0;
     motor_rb=0;
     motor_lf=0;
-    motor_lb=0;    
+    motor_lb=0;
     led=1;
     set_direction(0x10, 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
-    wait(5); led=0;
-    
+    wait(5);
+    led=0;
+
 }
 
-void JiveFunct() {
-    
+void JiveFunct()
+{
+
     //Set PWM frequency to 1000Hz
     motor_l.period( 1.0f / (float) PWM_FREQ);
     motor_r.period( 1.0f / (float) PWM_FREQ);
@@ -201,140 +209,146 @@
     motor_rb=0;
     motor_lf=0;
     motor_lb=0;
-           
-       //forward + right spin
-        wait(2);
-        myled = !myled;
-        set_direction(0x11, 0.3, 0.5); //robot pivots forard, to the right , at 80% speed (80% right; 0% left)
-       
-       // back spin
-        wait(2);
-        myled = !myled;
-        set_direction(0x00, 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)
-       
-       //
-        wait(2);
-        myled = !myled;
-        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)
-        
-        wait(2);
-        myled = !myled;
-        set_direction(0x10, 0.5, 0);   //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%
-       
-       
-       /*
-        wait(2);
-        led = !led;
-        set_direction(0x11, 0.5, 0);  //This would set the robot to go forward, at half speed, straight
-        
-        wait(2);
-        led = !led;
-        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)
+
+    //forward + right spin
+    wait(2);
+    myled = !myled;
+    set_direction(0x11, 0.3, 0.5); //robot pivots forard, to the right , at 80% speed (80% right; 0% left)
+
+    // back spin
+    wait(2);
+    myled = !myled;
+    set_direction(0x00, 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)
+
+    //
+    wait(2);
+    myled = !myled;
+    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)
+
+    wait(2);
+    myled = !myled;
+    set_direction(0x10, 0.5, 0);   //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%
+
+
+    /*
+     wait(2);
+     led = !led;
+     set_direction(0x11, 0.5, 0);  //This would set the robot to go forward, at half speed, straight
 
-        wait(2);
-        led = !led;
-        set_direction(0x11, 0.5, 0.5); // robot pivots forward, to the right, full speed (100% duty right ; 0% duty left)
+     wait(2);
+     led = !led;
+     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)
 
-        wait(2);
-        led = !led;
-        set_direction(0x11, 0.5, -0.5); //robot pivots forward, to the left,  full speed  (0% duty right ; 100% duty left)
+     wait(2);
+     led = !led;
+     set_direction(0x11, 0.5, 0.5); // robot pivots forward, to the right, full speed (100% duty right ; 0% duty left)
 
-       
+     wait(2);
+     led = !led;
+     set_direction(0x11, 0.5, -0.5); //robot pivots forward, to the left,  full speed  (0% duty right ; 100% duty left)
+
+
 
-        wait(2);
-        led = !led;
-        set_direction(0x11, -0.3, 0.5); //robot pivotrs foward, to the right, AT 20% SPEED (20%, 0%)
+     wait(2);
+     led = !led;
+     set_direction(0x11, -0.3, 0.5); //robot pivotrs foward, to the right, AT 20% SPEED (20%, 0%)
 
-        wait(2);
-        led = !led;
-        set_direction(0x10, 0.5, 0);   //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%
-        
-        wait(2);
-        led = !led;
-        set_direction(0x10, 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
-        */
-        
+     wait(2);
+     led = !led;
+     set_direction(0x10, 0.5, 0);   //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%
+
+     wait(2);
+     led = !led;
+     set_direction(0x10, 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
+     */
+
 }
 
 
 // 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
 // at every point but it's too annoying to get it to work... so fuk dat
 
-void RemoteControl(){  char dir[2]; /*speed=0, angle=0, time=0, */ int quit=0;
+void RemoteControl()
+{
+    char dir[2]; /*speed=0, angle=0, time=0, */
+    int quit=0;
 
     while(quit==0) {
         wait(1);
         //dir
         blue.printf("Direction?\n");
-        while ( blue.readable() ) {}               // loops until there is an input from the user
-        
-        for(int i=0; i<2; i++) { dir[2] = blue.getc(); } //Store incoming response into array 
+        while ( blue.readable()==0 ) {}               // loops until there is an input from the user
+
+        for(int i=0; i<2; i++) {
+            dir[i] = blue.getc();    //Store incoming response into array
+        }
         blue.printf("%c dir\n", dir);
-        
+
     }
 }
-        //for(int i=0; i<4; i++) { quitarray[i]= blue.getc();  blue.printf("get %c", quitarray[i]); } //Store incoming response into array 
-        //quit=Quitter(quitarray);
-        
-        /*
-        //speed
-        blue.printf("Speed?");
-        while ( blue.readable() ) {}               // loops until there is an input from the user
-            speed=blue.getc();
-            quit=Quitter();
-           
-        
-        //angle
-        blue.printf("Angle?");
-        while (blue.readable()==0) {}               // loops until there is an input from the user
-            angle=blue.getc();
-            quit=Quitter();
-            
-        
-        //time
-        blue.printf("How long?");
-        while (blue.readable()==0) {}               // loops until there is an input from the user
-            time=blue.getc();
-            quit=Quitter();
-    */
-        //set_direction(dir, speed, angle); //also find a way to do time too
-        //wait(time); 
-    
+//for(int i=0; i<4; i++) { quitarray[i]= blue.getc();  blue.printf("get %c", quitarray[i]); } //Store incoming response into array
+//quit=Quitter(quitarray);
+
+/*
+//speed
+blue.printf("Speed?");
+while ( blue.readable() ) {}               // loops until there is an input from the user
+    speed=blue.getc();
+    quit=Quitter();
+
+
+//angle
+blue.printf("Angle?");
+while (blue.readable()==0) {}               // loops until there is an input from the user
+    angle=blue.getc();
+    quit=Quitter();
+
+
+//time
+blue.printf("How long?");
+while (blue.readable()==0) {}               // loops until there is an input from the user
+    time=blue.getc();
+    quit=Quitter();
+*/
+//set_direction(dir, speed, angle); //also find a way to do time too
+//wait(time);
+
+
+/*
 
-    /*
-    
-    send 11, 0.5, 0.2 in one string
-    for the floats, it will send (num)(point)(num) ef 0.3 = 30, 2E, 33
-    all in ASCII remember
-    
-    so for:
-    11 0.5 0.2
-    it will send:
-    31 31 20 30 2e 35 20 30 2e 32
+send 11, 0.5, 0.2 in one string
+for the floats, it will send (num)(point)(num) ef 0.3 = 30, 2E, 33
+all in ASCII remember
+
+so for:
+11 0.5 0.2
+it will send:
+31 31 20 30 2e 35 20 30 2e 32
+
+sending 313120302e3520302e32 would cause a stack overload
+
+going to have to split the user into 3 different lines of tx
+
+so it'd be simpler too but more difficult for the user to control.
+might need to add in a "for how long for?" function that will only drive the motors for a short period of time
 
-    sending 313120302e3520302e32 would cause a stack overload
-    
-    going to have to split the user into 3 different lines of tx
-    
-    so it'd be simpler too but more difficult for the user to control.
-    might need to add in a "for how long for?" function that will only drive the motors for a short period of time
-    
-    user would have to be knowledgeable of the set direction function  aka get daniel to do the demonstration
-    
-    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;
-    
-    yes it is slow, but it allows for exact computation of distance, speed and angle
-    
-    so 1 function with 4 different while loops
-    
-    while user has not input a direction, loop again
-    
-    one function : remote control    
-    */
+user would have to be knowledgeable of the set direction function  aka get daniel to do the demonstration
+
+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;
+
+yes it is slow, but it allows for exact computation of distance, speed and angle
+
+so 1 function with 4 different while loops
 
-int main(){ 
+while user has not input a direction, loop again
+
+one function : remote control
+*/
 
-    int spin[4] = {0x73, 0x70, 0x69, 0x6e};       
+int main()
+{
+
+    int spin[4] = {0x73, 0x70, 0x69, 0x6e};
     int jive[4] = {0x6a, 0x69, 0x76, 0x65};
     int ctrl[4] = {0x63, 0x74, 0x72, 0x6c};
     int maze[4] = {0x6d, 0x61, 0x7a, 0x65};
@@ -343,22 +357,42 @@
     while (1) {
         blue.printf("What will I do?\n");           // ask user question
         while (blue.readable()==0) {}               // loops until there is an input from the user
-        
-        for(int i=0; i<4; i++) {  x[i] = blue.getc();  } //Store incoming response into array
-        
-       int FindSpin=0, FindJive=0, FindCtrl=0, FindMaze=0;
+
+        for(int i=0; i<4; i++) {
+            x[i] = blue.getc();     //Store incoming response into array
+        }
+
+        int FindSpin=0, FindJive=0, FindCtrl=0, FindMaze=0;
 
-        for(int s=0; s<4; s++) {   
-          if (x[s]==spin[s]) { FindSpin++; }
-          if (x[s]==jive[s]) { FindJive++; }
-          if (x[s]==ctrl[s]) { FindCtrl++; }
-          if (x[s]==maze[s]) { FindMaze++; }
+        for(int s=0; s<4; s++) {
+            if (x[s]==spin[s]) {
+                FindSpin++;
+            }
+            if (x[s]==jive[s]) {
+                FindJive++;
+            }
+            if (x[s]==ctrl[s]) {
+                FindCtrl++;
+            }
+            if (x[s]==maze[s]) {
+                FindMaze++;
+            }
         }
-        
-        if (FindSpin==4) { SpinFunct();}
-        if (FindJive==4) { JiveFunct();}
-        if (FindCtrl==4) { RemoteControl() ;}
-        if (FindMaze==4) {led=1; wait(4); led=0;}
+
+        if (FindSpin==4) {
+            SpinFunct();
+        }
+        if (FindJive==4) {
+            JiveFunct();
+        }
+        if (FindCtrl==4) {
+            RemoteControl() ;
+        }
+        if (FindMaze==4) {
+            led=1;
+            wait(4);
+            led=0;
+        }
     }
 }
 
@@ -373,10 +407,10 @@
 
 /*
 int Quitter(char x[4]) {     int quit=0; char quitting[4]={0x71, 0x75, 0x69, 0x74};
-    
+
     blue.printf ("works");
-    for(int i=0; i<4; i++) {  x[i] = blue.getc();  } //Store incoming response into array 
-    for(int s=0; s<4; s++) {   
+    for(int i=0; i<4; i++) {  x[i] = blue.getc();  } //Store incoming response into array
+    for(int s=0; s<4; s++) {
         if (x[s]==quitting[s]) { quit++; }
     }
     if (quit==4) { quit=1;}