LED chaser, like Knightrider sequence. Speed of sequence is controlled using serial interrupts.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /****************************************************************************** 00002 ¬ Knightrider.c ¬ 00003 ¬ Uses the onboard LEDS on mBED to continuously perform LED chaser sequence ¬ 00004 ¬ LED Chaser sequence timimg can be changed by using the serial comms and ¬ 00005 ¬ activity on digital port. ¬ 00006 ¬ ¬ 00007 ¬ Written by RandomSingh. 05/2010 ¬ 00008 ¬ ¬ 00009 ******************************************************************************/ 00010 00011 /* Libraries */ 00012 #include "mbed.h" 00013 00014 /* Output Assignements */ 00015 DigitalOut myled(LED1); 00016 DigitalOut myled2(LED2); 00017 DigitalOut myled3(LED3); 00018 DigitalOut myled4(LED4); 00019 00020 /* Input Assignments */ 00021 DigitalIn enable(p5); 00022 00023 /* Interrupt Assignemnts */ 00024 InterruptIn event(USBRX); 00025 InterruptIn event2(p16); 00026 00027 /* Serial Port Assignment */ 00028 Serial pc(USBTX, USBRX); // tx, rx 00029 00030 00031 /* Macro Definitions */ 00032 #define LONGER 0.9 00033 #define LONG 0.4 00034 #define SHORT 0.05 00035 #define FWD 0 00036 #define REV 1 00037 #define SLOW 0 00038 #define MEDIUM 1 00039 #define FAST 2 00040 #define ON 1 00041 #define OFF 0 00042 00043 /* Prototype Function declartions */ 00044 void initialise_LEDS(); 00045 void trigger(); 00046 void LED_chaser_sequence(); 00047 00048 /* External Variables */ 00049 int timer; 00050 short speed; 00051 int direction = FWD; 00052 int phase; 00053 00054 00055 00056 int main() { 00057 00058 event.rise(&trigger); // Set up interrupts 00059 event2.rise(&trigger); 00060 initialise_LEDS(); 00061 pc.printf("Speed is slow\r\n"); // Default speed is slow 00062 00063 while(1) 00064 {// Start of while 00065 00066 LED_chaser_sequence(); 00067 00068 }// End of while 00069 }// End of main 00070 00071 00072 /****************************************************************************** 00073 ¬ trigger function - ISR ¬ 00074 ¬ If activity on the rx line of the serial port then this ISR executes ¬ 00075 ¬ This ISR changes the external variable 'speed' value, which is used for ¬ 00076 ¬ timing of the sequence. ¬ 00077 ¬ ¬ 00078 ¬ The objective of this ISR is to change the 'speed' value & transmit a ¬ 00079 ¬ a message to the serial port. ¬ 00080 ¬ ¬ 00081 ******************************************************************************/ 00082 void trigger() 00083 {// Start of trigger 00084 if(speed > FAST) // This bit of the code limits value of speed to 2 00085 {// Start of if // as there are only three levels of timing 00086 speed = SLOW; 00087 pc.printf("Speed is slow\r\n"); 00088 }// End of if 00089 else 00090 {// Start of else 00091 speed++; 00092 }// End of else 00093 00094 if(speed == MEDIUM) 00095 pc.printf("Speed is medium\r\n"); 00096 00097 if(speed == FAST) 00098 pc.printf("Speed is fast\r\n"); 00099 00100 }// End of trigger 00101 00102 void LED_chaser_sequence() 00103 {// Start of LED_chaser_sequence 00104 switch(phase) 00105 {// Start of switch 00106 case 0: myled = OFF; 00107 myled2 = OFF; 00108 myled3 = OFF; 00109 myled4 = OFF; 00110 wait(LONG); 00111 wait(SHORT); 00112 direction = FWD; 00113 phase++; 00114 break; 00115 case 1: myled = ON; 00116 myled2 = OFF; 00117 myled3 = OFF; 00118 myled4 = OFF; 00119 wait(SHORT); 00120 if(direction == FWD) 00121 {// Start of if 00122 phase++; 00123 }// End of if 00124 else 00125 {// Start of else 00126 phase--; 00127 }// End of else 00128 break; 00129 case 2: myled = OFF; 00130 myled2 = ON; 00131 myled3 = OFF; 00132 myled4 = OFF; 00133 wait(SHORT); 00134 if(direction == FWD) 00135 {// Start of if 00136 phase++; 00137 }// End of if 00138 else 00139 {// Start of else 00140 phase--; 00141 }// End of else 00142 break; 00143 case 3: myled = OFF; 00144 myled2 = OFF; 00145 myled3 = ON; 00146 myled4 = OFF; 00147 wait(SHORT); 00148 if(direction == FWD) 00149 {// Start of if 00150 phase++; 00151 }// End of if 00152 else 00153 {// Start of else 00154 phase--; 00155 }// End of else 00156 break; 00157 case 4: myled = OFF; 00158 myled2 = OFF; 00159 myled3 = OFF; 00160 myled4 = ON; 00161 wait(SHORT); 00162 direction = REV; 00163 phase--; 00164 break; 00165 default : 00166 wait(SHORT); 00167 break; 00168 00169 }// End of switch 00170 00171 switch(speed) 00172 {// Start of switch 00173 case SLOW : wait(LONGER); 00174 break; 00175 case MEDIUM : wait(LONG); 00176 break; 00177 case FAST : wait(SHORT); 00178 break; 00179 default : 00180 wait(SHORT); 00181 break; 00182 }// End of switch 00183 00184 }// End of LED_chaser_sequence 00185 00186 void initialise_LEDS() 00187 {// Start of initialise_LEDS 00188 00189 myled = OFF; 00190 myled2 = OFF; 00191 myled3 = OFF; 00192 myled4 = OFF; 00193 00194 }// End of initialise_LEDS
Generated on Fri Aug 5 2022 06:47:13 by
1.7.2