It works. But I didn't do it the way they said to. #yolo

Dependencies:   m3pi_ng mbed

Fork of GradientSolver by der Roboter

main.cpp

Committer:
mmpeter
Date:
2014-06-05
Revision:
1:48163ae9fe4d
Parent:
0:6b067dcbab9d
Child:
2:49565c1ff0bf

File content as of revision 1:48163ae9fe4d:

#include "mbed.h"
#include "m3pi_ng.h"
#include "cmath"
#include "iostream"
#include <string>

m3pi thinggy;

void turn_right();
void turn_left();
void victors();
int darkest=0;
int dark;
int previousdark;
int v1=0,v2=0,v3=0,v4=0,v5=0,v6=0;
bool found;
int x;


using namespace std;
int sensor[5];

int main() { 
thinggy.raw_sensor(sensor);
while(1){
      
      while(found!=1){
          //Reading on north
          thinggy.cls();
          thinggy.raw_sensor(sensor);
          thinggy.printf("1: ");
          thinggy.printf("%i",sensor[2]);
          v1=sensor[2];
          thinggy.stop();
          wait(0.5);
          turn_right();
          
          //Turn 60 degrees from north
          thinggy.cls();
          thinggy.raw_sensor(sensor);
          thinggy.printf("2: ");
          thinggy.printf("%i",sensor[2]);
          v2=sensor[2];
          thinggy.stop();
          wait(0.5);
          turn_right();
          
          //120 degrees from north
          thinggy.cls();
          thinggy.raw_sensor(sensor);
          thinggy.printf("3: ");
          thinggy.printf("%i",sensor[2]);
          v3=sensor[2];
          thinggy.stop();
          wait(0.5);
          turn_right();
          
          //180 degrees from north
          thinggy.cls();
          thinggy.raw_sensor(sensor);
          thinggy.printf("4: ");
          thinggy.printf("%i",sensor[2]);
          v4=sensor[2];
          thinggy.stop();
          wait(0.5);
          turn_right();
          
          //240 degrees from north
          thinggy.cls();
          thinggy.raw_sensor(sensor);
          thinggy.printf("5: ");
          thinggy.printf("%i",sensor[2]);
          v5=sensor[2];
          thinggy.stop();
          wait(0.5);
          turn_right();
          
          //300 degrees from north
          thinggy.cls();
          thinggy.raw_sensor(sensor);
          thinggy.printf("6: ");
          thinggy.printf("%i",sensor[2]);
          v6=sensor[2];
          thinggy.stop();
          wait(0.5);
          
          //Chill the fuck out
          thinggy.cls();
          thinggy.printf("Relax");
          wait(1);
          thinggy.cls();
          thinggy.printf("I got it");
          thinggy.stop();
          wait(1);
          
    //Determine which direction is darkest
        //If the darkest spot is found, stop, play victory sound
        if(v1==2000){
          thinggy.stop();
          thinggy.cls();
          thinggy.printf("Found it");
          thinggy.forward(0.2);
          wait(0.1);
          thinggy.stop();
          wait(1);
          victors();
          return found=1;
          }
         if(v2==2000){
          thinggy.stop();
          thinggy.cls();
          thinggy.printf("Found it");
          turn_right();
          thinggy.forward(0.2);
          wait(0.1);
          thinggy.stop();
          wait(1);
          victors();
          return found=1;
          }
          
          if(v3==2000){
          thinggy.stop();
          thinggy.cls();
          thinggy.printf("Found it");
          turn_right();
          turn_right();
          thinggy.forward(0.2);
          wait(0.1);
          thinggy.stop();
          wait(1);
          victors();
          return found=1;
          }
          
        if(v4==2000){
          thinggy.stop();
          thinggy.cls();
          thinggy.printf("Found it");
          turn_right();
          turn_right();
          turn_right();
          thinggy.forward(0.2);
          wait(0.1);
          thinggy.stop();
          wait(1);
          victors();
          return found=1;
          }
          
        if(v5==2000){
          thinggy.stop();
          thinggy.cls();
          thinggy.printf("Found it");
          turn_left();
          turn_left();
          thinggy.forward(0.2);
          wait(0.1);
          thinggy.stop();
          wait(1);
          victors();
          return found=1;
          }
        
         if(v6==2000){
          thinggy.stop();
          thinggy.cls();
          thinggy.printf("Found it");
          turn_left();
          thinggy.forward(0.2);
          wait(0.1);
          thinggy.stop();
          wait(1);
          victors();
          return found=1;
          }
        //Continue the search
          //North is darkest
          if(v1>v2 and v1>v3 and v1>v4 and v1>v5 and v1>v6){
              x=1;
              dark=v1;
              thinggy.cls();
              thinggy.printf("It's 1");
              } 
          //60 degrees from north is darkest
          else if(v2>v1 and v2>v3 and v2>v4 and v2>v5 and v2>v6){
             x=2;
             dark=v2;
             thinggy.cls();
             thinggy.printf("It's 2");
              } 
          //120 degrees from north is darkest
          else if(v3>v1 and v3>v2 and v3>v4 and v3>v5 and v3>v6){
             x=3;
             dark=v3;
             thinggy.cls();
             thinggy.printf("It's 3");
              } 
          //180 degrees from north is darkest
          else if(v4>v1 and v4>v2 and v4>v3 and v4>v5 and v4>v6){
             x=4;
             dark=v4;
             thinggy.cls();
             thinggy.printf("It's 4");
              } 
          //240 degrees from north is darkest
          else if(v5>v1 and v5>v2 and v5>v3 and v5>v4 and v5>v6){
             x=5;
             dark=v5;
             thinggy.cls();
             thinggy.printf("It's 5");
              }
          //300 degrees from north is darkest
          else if(v6>v1 and v6>v2 and v6>v3 and v6>v4 and v6>v5){
             x=6;
             dark=v6;
             thinggy.cls();
             thinggy.printf("It's 6");
              }
          
          switch(x){
              case 1:
                //Go forward
                thinggy.forward(0.3);
                wait(0.1);
                break;
              case 2:
                //Go 60 degrees from north
                turn_right();
                thinggy.forward(0.3);
                wait(0.1);
                break;
              case 3:
                //Go 120 degrees from north
                turn_right();
                turn_right();
                thinggy.forward(0.3);
                wait(0.1);
                break;
              case 4:
                //Go 180 degrees from north
                turn_right();
                turn_right();
                turn_right();
                thinggy.forward(0.3);
                wait(0.1);
                break;
              case 5:
                //Go 240 degrees from north
                turn_left();
                turn_left();
                thinggy.forward(0.3);
                wait(0.1);
                break;
              case 6:
                //Go 300 degrees from north
                turn_left();
                thinggy.forward(0.3);
                wait(0.1);
                break;
                }
                
            if(dark>darkest){
              darkest=dark;
              }  
            //Condition for exiting the loop: darkest spot found  
            if(previousdark>dark){
                //Go 180 degrees
                found=1;
                thinggy.printf("Got it!");
                }
            dark=previousdark; 
      
      }
          }
          }
          
//EFFECTS: turns the robot right 
void turn_right(){
    int n=0;
    wait(.08); 
    while (n<400){
        thinggy.left_motor(0.2);
        thinggy.right_motor(-0.2);
        n++;
        }
    wait(0.15);      
}
//EFFECTS: turns the robot left
void turn_left(){
  int n=0;
  wait(.08); 
    while (n<400){
        thinggy.left_motor(-0.2);
        thinggy.right_motor(0.2);
        n++;
        }
    thinggy.forward(0.2);
    wait(0.15);          
}

//EFFECTS: Play victory tune
void victors(){
            // V=volume max is 15::O5=octave5::G16=G16th note::R24=rest24th note...a rest is needed between any two note that are the same
       char hail[]={'V','1','0',
       //hail to the victors valiant
       'O','5',
       'C','2',
       
       'O','4',
       'G','#','4',
       'A','#','4',
       
       'O','5',
       'C','4',
       
       'O','4',
       'G','#','4',
       'A','#','4',
       
       'O','5',
       'C','4',
      
       
       //hail to the conquering heros
       'C', '#','2',
       'O','4',
       'A','#','4',
       
       'O','5',
       'C','4',
       'C','#','4',
       
       'O','4',
       'A','#','4',
       
       'O','5',
       'C','4',
       'C','#','4',
       };
       //the number of characters in the array
       int numb=strlen(hail);
       thinggy.playtune(hail,numb);
       thinggy.cls();
          thinggy.printf("I win!");
          wait(3);
       }