Narendra Joshi / Mbed 2 deprecated robots

Dependencies:   Motor Robo mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Robo.h"
00003 
00004 
00005 // robot object
00006 Robo roger(p20, p19, p18,
00007            p21, p22, p23,
00008            p24, p25, p27, p28, p29, p30);
00009 
00010 DigitalOut myled(LED2); 
00011 DigitalOut left(LED3);  // status left LED 
00012 DigitalOut right(LED4); // status right LED
00013 
00014 // a buzzer when a life decreses
00015 DigitalOut buzzer(p7);
00016 
00017 //the rf receiver Serial object
00018 Serial rx(p9, p10);
00019 
00020 //a rise on this pin reduces a life of the robot
00021 InterruptIn heart(p6);
00022 
00023 /* a wrapper function for roger.devLives
00024  * which can be passed as argument to some other funtion
00025  */
00026 void decRoboLives() {
00027     roger.decLives();
00028     buzzer = 0;
00029  }
00030 
00031 int main() {
00032     char command = 'S';
00033     rx.baud(2400);
00034     myled = 1; // test led. test: program loaded successfully
00035     heart.rise(decRoboLives);
00036 
00037 /* main while loop 
00038  * 1. wait for instructions
00039  * 1. read instruction
00040  * 2. do the required action
00041  * 3. empty the rx object by reading further
00042  * 4. go to 1
00043  *  we introduced redundancy in sending the instructions so as to make
00044  * sure they really end up being received by the robots. that's the raison d'etre
00045  * for step 3.
00046  */
00047     while(1) {
00048         if (rx.readable()) {
00049             command = rx.getc();
00050             switch(command) {
00051                 case 'L' :
00052                     left = 1;
00053                     right = 0;
00054                     roger.goLeft();
00055                     break;
00056                 case 'R':
00057                     right = 1;
00058                     left = 0;
00059                     roger.moveRight();
00060                     break;
00061                 case 'M':
00062                     roger.goAhead();
00063                     break;
00064                 case 'B':
00065                     roger.moveBack();
00066                     break;
00067                 case 'S':
00068                     roger.stop();
00069                     break;
00070                 case 'D':
00071                     roger.printlcd("Hi");
00072                     wait(1);
00073                     roger.goLeft();
00074                     roger.moveRight();
00075                     roger.goLeft();
00076                     roger.moveRight();
00077                     roger.goAhead();
00078                     wait(1);
00079                     roger.moveBack();
00080                     wait(1);
00081                     roger.stop();
00082                 default:
00083                     roger.printlcd("Waiting...      ");
00084                     break;
00085              
00086             }
00087             while (rx.readable())
00088                 command = rx.getc();
00089         }
00090     }
00091  }