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 * This detects a winner in a two person race by * 00003 * detecting when there is a break in the signal * 00004 * sent to two different pins. When the winner is * 00005 * detected, the corresponding LED is lit. * 00006 * * 00007 * Created by: Michael Dushkoff (mad1841@rit.edu) * 00008 * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00009 #include "mbed.h" 00010 00011 /* This is a print button that allows the time to be outputted */ 00012 DigitalIn button(p30); 00013 00014 /* Tells when to stop the timer */ 00015 int end1 = 0; 00016 int end2 = 0; 00017 00018 /* Rise detection */ 00019 int rise = 0; 00020 00021 /* Create two individual timers */ 00022 Timer t1; 00023 Timer t2; 00024 00025 /* Recieving pin */ 00026 InterruptIn rxo(p10); 00027 00028 /* Interrupt sensors */ 00029 InterruptIn sEnd1(p20); 00030 InterruptIn sEnd2(p21); 00031 00032 /* Indicator LEDs */ 00033 DigitalOut iled1(p22); 00034 DigitalOut iled2(p23); 00035 00036 /* Status LEDs */ 00037 DigitalOut sled1(LED1); 00038 DigitalOut sled2(LED2); 00039 DigitalOut sled3(LED3); 00040 DigitalOut sled4(LED4); 00041 00042 /* Winner LEDs */ 00043 DigitalOut wled11(p11); // Gate 1 Win 00044 DigitalOut wled12(p12); // Gate 1 Win 00045 DigitalOut wled13(p13); // Gate 1 Win 00046 DigitalOut wled21(p36); // Gate 2 Win 00047 DigitalOut wled22(p35); // Gate 2 Win 00048 DigitalOut wled23(p34); // Gate 2 Win 00049 00050 /* Tells when there is a winner */ 00051 int win = 0; 00052 00053 /* Pulsed recieving */ 00054 int rxc = 0; 00055 00056 /* This detects a transmitted signal */ 00057 void recieve(){ 00058 if (rxc >= 1){ 00059 /* Set status LEDs */ 00060 sled2 = 1; 00061 sled3 = 1; 00062 00063 /* Start timers */ 00064 t1.start(); 00065 t2.start(); 00066 } 00067 rxc += 1; 00068 } 00069 00070 /* This detects interrupt 1 */ 00071 void l1(){ 00072 if (win == 0){ 00073 iled1 = 1; 00074 win = 1; 00075 wled11 = 1; 00076 wled12 = 1; 00077 wled13 = 1; 00078 } 00079 end1 = 1; 00080 sled1 = 1; // Set status LED 00081 t1.stop(); // Stop timer 1 00082 } 00083 00084 /* This detects interrupt 2 */ 00085 void l2(){ 00086 if (win == 0){ 00087 iled2 = 1; 00088 win = 1; 00089 wled21 = 1; 00090 wled22 = 1; 00091 wled23 = 1; 00092 } 00093 end2 = 1; 00094 sled4 = 1; // Set status LED 00095 t2.stop(); // Stop timer 2 00096 } 00097 00098 int main() { 00099 /* Set the LEDs off */ 00100 iled1 = 0; 00101 iled2 = 0; 00102 sled1 = 0; 00103 sled2 = 0; 00104 sled3 = 0; 00105 sled4 = 0; 00106 wled11 = 0; 00107 wled12 = 0; 00108 wled13 = 0; 00109 wled21 = 0; 00110 wled22 = 0; 00111 wled23 = 0; 00112 00113 /* Attach interrupt service routine to pins */ 00114 sEnd1.fall(&l1); 00115 sEnd2.fall(&l2); 00116 rxo.rise(&recieve); 00117 00118 printf("Ready!\n\r"); 00119 while(1) { 00120 if (button.read() == 1 && rise == 0){ 00121 /* Set rise detector to high */ 00122 rise = 1; 00123 00124 /* Check Track 1 */ 00125 if (end1 == 1){ 00126 printf("========== Track 1 Time ==========\n\r"); 00127 printf(" Finished in: %lf seconds\n\r", t1.read()); 00128 printf("==================================\n\r\n\r"); 00129 } 00130 else { 00131 printf("Track 1 has not finished\n\r\n\r"); 00132 } 00133 00134 /* Check Track 2 */ 00135 if (end2 == 1){ 00136 printf("========== Track 2 Time ==========\n\r"); 00137 printf(" Finished in: %lf seconds\n\r", t2.read()); 00138 printf("==================================\n\r\n\r"); 00139 } 00140 else { 00141 printf("Track 2 has not finished\n\r\n\r"); 00142 } 00143 } 00144 else if (button.read() == 0 && rise == 1){ 00145 /* Reset rise detection */ 00146 rise = 2; 00147 wait (0.001); //Debounce 00148 } 00149 else if (button.read() == 0 && rise == 2){ 00150 rise = 0; 00151 } 00152 } 00153 }
Generated on Tue Jul 12 2022 23:03:04 by
1.7.2