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.
Dependencies: Motor PinDetect Servo mbed
main.cpp
00001 #include "mbed.h" 00002 #include "Motor.h" 00003 #include "Servo.h" 00004 #include "PinDetect.h" 00005 00006 Motor m(p25, p27, p28); 00007 00008 DigitalIn sw1(p16); 00009 DigitalIn sw2(p17); 00010 DigitalIn sw3(p18); 00011 DigitalIn sw4(p19); 00012 DigitalIn sw5(p20); 00013 PinDetect push1(p12); 00014 PinDetect push2(p13); 00015 Servo servo1(p22); 00016 Servo servo2(p21); 00017 00018 BusOut lights(p5, p6, p7, p8, p11); 00019 00020 float a; 00021 float b; 00022 00023 int volatile count=0; 00024 00025 // Callback routine is interrupt activated by a debounced pb1 hit 00026 void push1_hit_callback (void) 00027 { 00028 00029 if (a > 0) { 00030 00031 servo1 = 0; 00032 wait(.2); 00033 servo1 = 0.5; 00034 00035 } else if (a < 0) { 00036 00037 servo1 = 1; 00038 wait(.2); 00039 servo1 = 0.5; 00040 00041 } 00042 00043 } 00044 // Callback routine is interrupt activated by a debounced pb2 hit 00045 void push2_hit_callback (void) 00046 { 00047 00048 if (a > 0) { 00049 00050 servo2 = 0; 00051 wait(.2); 00052 servo2 = 0.5; 00053 00054 } 00055 00056 else if (a < 0) { 00057 00058 servo2 = 1; 00059 wait(.2); 00060 servo2 = 0.5; 00061 } 00062 00063 } 00064 int main() 00065 { 00066 00067 servo1 = 0.5; 00068 servo2 = 0.5; 00069 00070 // Use internal pullups for pushbutton 00071 push1.mode(PullUp); 00072 push2.mode(PullUp); 00073 // Delay for initial pullup to take effect 00074 wait(.01); 00075 // Setup Interrupt callback functions for a pb hit 00076 push1.attach_deasserted(&push1_hit_callback); 00077 push2.attach_deasserted(&push2_hit_callback); 00078 // Start sampling pb inputs using interrupts 00079 push1.setSampleFrequency(); 00080 push2.setSampleFrequency(); 00081 //Blink myled in main routine forever while responding to pb changes 00082 // via interrupts that activate the callback counter function 00083 00084 servo1 = 0.5; 00085 servo2 = 0.5; 00086 servo1.calibrate (.0009, 90); 00087 servo2.calibrate (.0009, 90); 00088 00089 while(1) { 00090 00091 a = rand()/(float)RAND_MAX*(-1.0)+0.4; 00092 b = rand()/(float)RAND_MAX*5.0 +3.0; 00093 00094 if(sw1 == 1 && sw2!= 1 && sw3 != 1 && sw4 != 1) { //player1 00095 00096 00097 00098 if(a>=.28 || a <= -.28) { 00099 00100 servo2 = 0; 00101 00102 lights = 31; // LED 1 on 00103 wait(.125); // Wait .4 seconds 00104 lights= 0; 00105 00106 m.speed(a); 00107 wait(b); 00108 00109 00110 00111 } 00112 } else if(sw1 != 1 && sw2== 1 && sw3 != 1 && sw4 != 1) { //player2 00113 if(a>=.28 || a <= -.28) { 00114 lights = 31; // 00115 wait(.125); // 00116 lights = 0; // 00117 00118 m.speed(a); 00119 wait(b); 00120 00121 } 00122 } else if(sw1 != 1 && sw2!= 1 && sw3 == 1 && sw4 != 1 && sw5 != 1) { //player3 00123 if(a>=.28 ) { 00124 00125 lights = 31; // LED 1&2&3&4&5 on 00126 wait(.125); //Wait .4 seconds 00127 lights = 0; //LED 1&2&3&4 on 00128 00129 m.speed(a); 00130 wait(b); 00131 00132 } 00133 } else if(sw1 != 1 && sw2!= 1 && sw3 == 1 && sw5 ==1 && sw4 != 1) { //player3 00134 if(a>=.28 ) { 00135 00136 lights = 31; // LED 1&2&3&4&5 on 00137 wait(.125); //Wait .4 seconds 00138 lights = 0; //LED 1&2&3&4 on 00139 00140 m.speed(-a); 00141 wait(b); 00142 00143 } 00144 } else if(sw4 == 1 && sw1 != 1 && sw2!= 1 && sw3 == 1 && sw5 ==1 || sw4 == 1 && sw1 != 1 && sw2!= 1 && sw3 == 1 || sw4 == 1 && sw1 != 1 && sw2== 1 && sw3 != 1 || sw4 == 1 && sw1 == 1 && sw2!= 1 && sw3 != 1 || sw4 == 1 && sw1 != 1 && sw2!= 1 && sw3 != 1 ) { //player3 00145 00146 m.speed(0); 00147 lights = 31; // LED 1&2&3 on 00148 wait(.125); //Wait .4 seconds 00149 lights = 0; //LED 1&2 on 00150 wait(.125); //Wait .4 seconds 00151 lights = 31; //LED 1 on 00152 wait(.125); //Wait .4 seconds 00153 lights = 0; //LED 1&2 on 00154 wait(.125); //Wait .4 seconds 00155 lights = 31; //LED 1 on 00156 wait(.125); //Wait .4 seconds 00157 lights = 0; //LED 1&2 on 00158 wait(.125); //Wait .4 seconds 00159 00160 } 00161 } 00162 }
Generated on Thu Jul 28 2022 15:04:05 by
1.7.2