Se Hoon Shon / Mbed 2 deprecated Card_activated_robot

Dependencies:   Motordriver TextLCD mbed

Fork of TextLCD_HelloWorld by Simon Ford

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h" 
00002 #include "TextLCD.h"
00003 #include <string.h>
00004 #include "motordriver.h"
00005 #include "mpr121.h"
00006 DigitalOut led1(LED1);
00007 DigitalOut led2(LED2);
00008 DigitalOut led3(LED3);
00009 DigitalOut led4(LED4);
00010 DigitalIn a(p5);
00011 DigitalIn b(p6);
00012 DigitalIn c(p7);
00013 DigitalIn d(p8);
00014 
00015 InterruptIn interrupt(p11);
00016 // Setup the i2c bus on pins 28 and 27
00017 I2C i2c(p9, p10);
00018 // Setup the Mpr121:
00019 // constructor(i2c object, i2c address of the mpr121)
00020 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00021 
00022 //Motor
00023 Motor left(p21, p22, p23, 1); // pwm, fwd, rev, has brake feature
00024 Motor right(p26, p25, p24, 1);
00025 
00026 //TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
00027 TextLCD lcd(p15, p16, p17, p18, p19, p20);
00028 Serial pc(USBTX, USBRX); // tx, rx 
00029 Serial device(p13, p14);  // tx, rx 
00030 
00031 //forward or in reverse mode
00032 int forward;
00033 
00034 //stores directions
00035 int direction[10];
00036 
00037 //stores speeds
00038 float speed[10];
00039 
00040 //stores durations
00041 int duration[10];
00042 
00043 //speed of the car
00044 float car_speed;
00045 
00046 //duration of the car
00047 int car_duration;
00048 
00049 //stores the command of the touchpad
00050 int _command;
00051 
00052 //check if there has been input to the touchpad
00053 bool no_command;
00054 
00055 //count variable for card reader
00056 int count=0; 
00057 
00058 //Number of total moves 
00059 int num_moves;
00060 
00061 //stores buzzcard information
00062 char output[50];
00063 
00064 //number of card swipes on the reader
00065 int num_swipe=0;
00066 int o_a, o_b;
00067 //int count,ch, ignore_count;
00068 int command[12];
00069 int count_1=0; 
00070 
00071 
00072 //password variables
00073 int p_1, p_2,p_3,p_4;
00074 
00075 //interrupt routine for touchpad
00076 void fallInterrupt() {
00077     no_command = true;
00078     int s2=0;
00079     int key_code=0;
00080     int i=0;
00081     int value=mpr121.read(0x00);
00082     value +=mpr121.read(0x01)<<8;
00083     // LED demo mod by J. Hamblen
00084     //pc.printf("MPR value: %x \r\n", value);
00085     i=0;
00086     // puts key number out to LEDs for demo
00087     for (i=0; i<12; i++) {
00088         if (((value>>i)&0x01)==1) key_code=i+1;
00089         }
00090        
00091     s2 = key_code - 1;
00092     if(s2 > 0){
00093     
00094     _command=s2;
00095     no_command = false;
00096     led4=s2 & 0x01;
00097     led3=(s2>>1) & 0x01;
00098     led2=(s2>>2) & 0x01;
00099     led1=(s2>>3) & 0x01;
00100     }
00101     
00102     count_1++;
00103     //pc.printf("hello %f\n",s);
00104 
00105 }
00106 
00107 //function to turn left
00108 void turnLeft(){
00109   lcd.cls();
00110   lcd.printf("Turning Left");
00111   left.speed(0);
00112   right.speed(0.4);
00113   wait(2);
00114   right.speed(0);
00115   wait(1);
00116 }
00117  
00118 //function to turn right
00119 void turnRight(){
00120   lcd.cls();
00121   lcd.printf("Turning Right");
00122   left.speed(0.4);
00123   right.speed(0);
00124   wait(2);
00125   left.speed(0);
00126   wait(1);
00127 }
00128 
00129 //function to turn around
00130 void turnAround(){
00131   lcd.cls();
00132   lcd.printf("Turning Around");
00133   
00134   left.speed(0.4);
00135   right.speed(0);
00136   wait(4);
00137   left.speed(0);
00138   wait(1);
00139 }
00140 
00141 
00142 //to move with input speed and duration of time
00143 void go(float _speed, int _time){
00144     lcd.cls();
00145     lcd.printf("Speed %f Duration %d sec", _speed, _time);
00146     left.speed(_speed);
00147     right.speed(_speed);
00148     wait(_time);
00149     left.speed(0);
00150     right.speed(0);
00151     wait(1);
00152 }
00153 
00154 //starts the card reader and verifies the user
00155 int cardReader(){
00156     num_swipe++;  
00157     int count=0;
00158     int ch=0;
00159     int ignore_count=0;
00160    // char output[50];
00161     while(1){
00162         if(device.readable()){
00163 
00164             ch=device.getc();
00165             if(ignore_count>5){
00166             output[count]=ch;
00167             count++;
00168             }
00169             ignore_count++;
00170             pc.putc(ch);
00171             }
00172             if (ignore_count>14){
00173                 lcd.cls();
00174                 
00175                  if (strcmp(output,"902512409")==0){
00176                    lcd.printf("Confirmed\n");
00177                    lcd.printf("Welcome Sehoon\n");
00178                    for (int i=0; i<1000; i++){
00179                         if(device.readable())
00180                         device.getc();
00181                         }
00182                    return 1;               
00183                   }
00184                   else if (strcmp(output,"902536564")==0){
00185                    lcd.printf("Confirmed\n");
00186                    lcd.printf("Welcome Tanvir\n");
00187                    for (int i=0; i<1000; i++){
00188                         if(device.readable())
00189                         device.getc();
00190                         }
00191                    return 1;   
00192                    }       
00193                   else {
00194                        for (int i=0; i<100; i++){
00195                              if(device.readable())
00196                              ch=device.getc();
00197                              wait(.01);
00198                         }
00199                 if (num_swipe==5){
00200                     lcd.printf("Too many tries\nEnter password");
00201                    
00202                     _command=0;
00203                     no_command=true;
00204                     while(1){
00205                     while(no_command){
00206                         wait(0.1);
00207                         }
00208                         p_1=_command;
00209                         _command=0;
00210                         no_command=true;
00211                         while(no_command){
00212                         wait(0.1);
00213                         }
00214                         p_2=_command;
00215                         _command=0;
00216                         no_command=true;
00217                         while(no_command){
00218                         wait(0.1);
00219                         }
00220                         p_3=_command;
00221                         _command=0;
00222                         no_command=true;
00223                         while(no_command){
00224                         wait(0.1);
00225                         }
00226                         p_4=_command;
00227                         _command=0;
00228                         no_command=true;
00229                         
00230                         if (p_1==1&&p_2==3&&p_3==5&&p_4==7)
00231                         {
00232                         lcd.cls();
00233                         lcd.printf("Correct!!!");
00234                         wait(2);
00235                         lcd.cls();
00236                         lcd.printf("Try swiping AGAIN!!");
00237                            break;
00238                            }
00239                            else{
00240                            lcd.cls();
00241                            lcd.printf("WRONG WRONG wROGNWRWEWERWER!@#!@");
00242                            wait(2);
00243                            lcd.cls();
00244                            } 
00245                            
00246                         }
00247                         
00248                         
00249                     }
00250                 else
00251                 lcd.printf("Try again\n");
00252                 return 0;
00253                 }
00254         }
00255     }    
00256     
00257 }
00258 
00259 //number of moves for the car
00260 int number_of_moves(){
00261       //select duration
00262    //Selecting number of moves
00263    lcd.printf("Select Number of Moves");
00264    num_moves=100;
00265    _command=100;
00266    no_command = true;
00267    while(no_command){
00268     wait(0.1);
00269 
00270    }
00271 
00272    num_moves = _command;
00273 
00274     
00275  
00276     lcd.cls();
00277     lcd.printf("Number of Moves: %d", num_moves); 
00278     wait(2);
00279     return 0;
00280     }
00281     
00282 int main() { 
00283 
00284     interrupt.fall(&fallInterrupt);   
00285   interrupt.mode(PullUp); 
00286   //Swiping buzz card 
00287    lcd.printf("Please Swipe your Buzzcard");
00288   while(!cardReader());
00289    wait(2);
00290    lcd.cls();
00291 
00292 
00293 
00294     number_of_moves();
00295    if (a||b||c||d){
00296         lcd.cls();
00297         lcd.printf("please turn off all switches");
00298         while(1){
00299             if (!(a||b||c||d))
00300             break;
00301         }
00302     }
00303  
00304     lcd.cls();
00305     
00306    
00307     for (int i=0; i<num_moves; i++){
00308     
00309     //select direction
00310    lcd.printf("Select Direction2=L 10=R 7=F 5=B");
00311    no_command = true;
00312    _command=0;
00313    while(_command!=2&&_command!=10&&_command!=7&&_command!=5){
00314     wait(0.1);
00315    }
00316         if(_command==2){
00317             direction[i]=1;
00318             lcd.cls();
00319             lcd.printf("Left");
00320   
00321             }
00322         else if (_command==10){
00323             direction[i]=2;
00324             lcd.cls();
00325             lcd.printf("Right");
00326 
00327             
00328             }
00329         else if (_command==7){
00330             direction[i]=3;
00331             lcd.cls();
00332             lcd.printf("Forward");
00333 
00334             }
00335         else if (_command==5){
00336             direction[i]=4;
00337             lcd.cls();
00338             lcd.printf("Reverse");
00339   
00340             }
00341             
00342        
00343    
00344    wait(2);
00345     lcd.cls();
00346     
00347     
00348     //select speed
00349    lcd.printf("Move Forward-1 or in Reverse-2");
00350    car_speed=0;
00351    _command=0;
00352    forward=0;;
00353    no_command = true;
00354    while(_command!=1&&_command!=2){
00355     wait(0.1);
00356    }
00357    if (_command==1){
00358         forward=1;
00359         lcd.cls();
00360         lcd.printf("Car moves forward");
00361         }
00362         else if (_command==2){
00363         forward=-1;
00364         lcd.cls();
00365         lcd.printf("Car moves in reverse");
00366         }
00367         wait(2);
00368         lcd.cls();
00369         _command=0;
00370    lcd.printf("Select Car Speed 1-10");
00371    while(_command!=1&&_command!=2&&_command!=3&&_command!=4&&_command!=5&&_command!=6&&_command!=7&&_command!=8&&_command!=9&&_command!=10){
00372         wait(0.1);
00373         }
00374         car_speed=forward*_command*0.1;
00375    
00376    speed[i]=car_speed;
00377    lcd.cls();
00378    lcd.printf("car speed: %d\n", _command);
00379    wait(2);
00380    
00381     lcd.cls();
00382     
00383     
00384      //select duration
00385  
00386    lcd.printf("Select Car duration 1-10 seconds");
00387    car_duration=0;
00388    _command=0;
00389    while(_command!=1&&_command!=2&&_command!=3&&_command!=4&&_command!=5&&_command!=6&&_command!=7&&_command!=8&&_command!=9&&_command!=10){
00390         wait(0.1);
00391         }
00392     car_duration=_command;
00393     duration[i]=car_duration;
00394     lcd.cls();
00395    lcd.printf("car duration: %d seconds\n", _command);
00396    wait(2);
00397    
00398 
00399  
00400     lcd.cls();
00401     
00402     }
00403     
00404     for (int i=10; i>0; i--){
00405         lcd.printf("Car will start in %d seconds",i);
00406         wait(1);
00407         lcd.cls();
00408     }
00409     lcd.printf("GO GO GO GO GO GO GO GO GO !!!");
00410     wait(1);
00411     for (int i=0; i<num_moves; i++){
00412  
00413        switch (direction[i]){
00414             case 1:
00415                 turnLeft();
00416                 break;
00417             case 2:
00418                 turnRight();
00419                 break;
00420             case 3:
00421                 break;
00422             case 4:
00423                 turnAround();
00424                 break;
00425                 }
00426       go(speed[i], duration[i]);
00427       }
00428       lcd.cls();
00429       lcd.printf("Finished");
00430 }