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 led_fsm first mbed (lpc1768) program 00003 Simple finite state machine for 00004 mbed's onboard LEDs sequencing 00005 00006 author: Ivan Vengust 'vitrzin' 00007 last change: 14. oct. 2010 00008 *********************************************************************/ 00009 #include "mbed.h" 00010 00011 // Function prototypes 00012 int fsm(void); 00013 int fsm1(void); 00014 00015 // Global variables 00016 DigitalOut led1(LED1); 00017 DigitalOut led2(LED2); 00018 DigitalOut led3(LED3); 00019 DigitalOut led4(LED4); 00020 00021 // main ************************************************************ 00022 int main() 00023 { 00024 int c(0), status; 00025 00026 while(1) 00027 { 00028 if (c > 2) // decide which sequence 00029 status = fsm1(); // call sequence change 00030 else 00031 status = fsm(); 00032 00033 if (status == 0) 00034 { 00035 c = ++c % 5; // count finished sequences 00036 } 00037 } 00038 } 00039 00040 /********************************************************************** 00041 fsm() Simple finite state machine 00042 Each call to 'fsm' will advance state machine to next state 00043 return value: current state 00044 **********************************************************************/ 00045 int fsm(void) // light each led on turn - 4 states 00046 { 00047 static int st(0); // sequence state 00048 const int dly(200); // base delay - 200 ms 00049 00050 switch(st) 00051 { 00052 case 0: // state 0 00053 led4 = 0; // change leds 00054 led3 = 0; 00055 led2 = 0; 00056 led1 = 1; 00057 wait_ms(dly*3); // wait longer in initial state 00058 ++st; // switch to next state 00059 break; 00060 case 1: // state 1 00061 led1 = 0; 00062 led2 = 1; 00063 ++st; 00064 break; 00065 case 2: // state 2 00066 led2 = 0; 00067 led3 = 1; 00068 ++st; 00069 break; 00070 case 3: // state 3 00071 led3 = 0; 00072 led4 = 1; 00073 st=0; // switch to initial state 00074 break; 00075 } 00076 wait_ms(dly); 00077 return st; 00078 } 00079 00080 int fsm1(void) // light led column - 8 states 00081 { 00082 static int st(0); // sequence state 00083 const int dly(400); // base delay - 200 ms 00084 00085 switch(st) 00086 { 00087 case 0: // state 0 00088 led4 = 0; // change leds 00089 led3 = 0; 00090 led2 = 0; 00091 led1 = 0; 00092 wait_ms(dly*2); // wait longer in initial state 00093 ++st; // switch to next state 00094 break; 00095 case 1: // state 1 00096 led4 = 1; 00097 ++st; 00098 break; 00099 case 2: // state 2 00100 led3 = 1; 00101 ++st; 00102 break; 00103 case 3: // state 3 00104 led2 = 1; 00105 ++st; 00106 break; 00107 case 4: // state 4 00108 led1 = 1; 00109 ++st; 00110 break; 00111 case 5: // state 5 00112 led1 = 0; 00113 ++st; 00114 break; 00115 case 6: // state 6 00116 led2 = 0; 00117 ++st; 00118 break; 00119 case 7: // state 7 00120 led3 = 0; 00121 ++st; 00122 break; 00123 case 8: // state 8 00124 led4 = 0; 00125 st=0; // switch to initial state 00126 break; 00127 } 00128 wait_ms(dly); 00129 return st; 00130 }
Generated on Tue Aug 23 2022 05:32:45 by
1.7.2