Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 ES200 3321 Project 2 Team Crab Race 00003 MIDN 3/c Pak, Tamas, etc 00004 */ 00005 #include "mbed.h" 00006 #include "stdio.h" 00007 #include "Motor.h" 00008 #include "Servo.h" 00009 00010 // declare global hardware objects 00011 Serial pc(USBTX,USBRX); // for printing out stuff during debugging 00012 Motor knife_motor(p26, p30, p29); 00013 Servo finish_servo(p22); 00014 Servo start_servo(p21); 00015 00016 // where are your switches? you need a few 00017 DigitalIn start_sw(p19); 00018 00019 // need LEDs (they're supposed to be external LEDs!) 00020 DigitalOut led[4]= {LED1, LED2, LED3, LED4}; 00021 00022 00023 00024 00025 00026 int main(void) 00027 { 00028 int x; // for random outcome of race 00029 int i; // a counter variable 00030 00031 // setup 00032 pc.printf("ES200 3321 Project 2 Team Crab Race\r\n"); 00033 pc.printf("main() thread running\r\n"); 00034 pc.printf("main() thread calibrating servos\r\n"); 00035 start_servo.calibrate(0.0009, 90.0); // HiTec HS422 servos for ES200/202 00036 finish_servo.calibrate(0.0009, 90.0); 00037 start_servo.write(0.0); // set start gate initial position 00038 finish_servo.write(0.5); // set finish gate initial position 00039 knife_motor.speed(0.0); // stop knife motor 00040 00041 // main loop 00042 while(1) { 00043 00044 // STATE: IDLE 00045 pc.printf("main() thread idle state\r\n"); 00046 start_servo.write(0.0); 00047 finish_servo.write(0.5); 00048 while (!start_sw.read()) { 00049 ThisThread::sleep_for(200); // works like wait() 00050 } 00051 00052 00053 // STATE: START RACE 00054 x = (rand()%10)+1; 00055 pc.printf("main() thread starting a race, x = %d\r\n",x); 00056 start_servo.write(0.25); // lower start gate to begin race 00057 ThisThread::sleep_for(1000); //gives time for crab to get down track 00058 00059 00060 // STATE: END RACE depending on value of x 00061 pc.printf("main() thread preparing to end race\r\n"); 00062 start_servo.write(0.0); //set initial start gate position 00063 if (x <= 7) { 00064 pc.printf("main() thread for x = %d, victory display\r\n",x); 00065 finish_servo = 1.0; //gate has a 70% chance to go one way 00066 pc.printf("main() thread waiting for start_sw to stop display\r\n"); 00067 while (start_sw.read()) { 00068 for (i=0; i<5; i++) { 00069 // blink lights for victory display 00070 led[i] = 1; 00071 ThisThread::sleep_for(150); 00072 led[i]=0; 00073 } // for 00074 } // while(start_sw.read()) 00075 } // if (x<=7) 00076 else { //30% chance the gate will go the other way 00077 pc.printf("main() thread for x = %d, fatality display\r\n",x); 00078 finish_servo.write(0.0); 00079 pc.printf("main() thread waiting for start_sw to stop display\r\n"); 00080 while (start_sw.read()) { 00081 knife_motor.speed (0.5); 00082 ThisThread::sleep_for(500); 00083 knife_motor.speed (-0.5); 00084 ThisThread::sleep_for(500); 00085 } // while(start_sw.read()) 00086 knife_motor.speed(0); // stop knife 00087 } 00088 pc.printf("main() thread returning to idle state, reset the racer positions\r\n"); 00089 finish_servo.write(0.5); //set finish gate to halfway point 00090 00091 00092 } // while(1) 00093 } // main()
Generated on Sun Jul 17 2022 12:14:44 by
1.7.2