This program simulates a State machine with a Switch Case. Just an Exercise
Revision 0:7cd6baddb768, committed 2015-05-28
- Comitter:
- Jamess
- Date:
- Thu May 28 18:37:18 2015 +0000
- Commit message:
- How to use a state machine with Switch Case;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu May 28 18:37:18 2015 +0000
@@ -0,0 +1,64 @@
+/*****************************************************************/
+/* How to use State Machines in C */
+/* Switch|Case */
+/*****************************************************************/
+
+// Librarys:
+#include "mbed.h"
+
+// Configuration:
+DigitalOut led(LED1);
+Serial pc(USBTX,USBRX);
+
+//Functions
+
+
+int main(){
+/************STATES**************/
+ enum states{
+ LED_OF_1S,
+ LED_ON_2S,
+ LED_OF_3S,
+ LED_ON_4S
+ };
+ enum states state = LED_OF_1S;
+while(1){
+ switch(state){
+ case LED_OF_1S:
+
+ //Led off for 1 second:
+ led=0;
+ pc.printf("LED_OF");
+ wait(1);
+ state = LED_ON_2S;
+
+ break;
+ case LED_ON_2S:
+
+ //Led on for 2 seconds:
+ led=1;
+ pc.printf("LED_ON");
+ wait(2);
+ state = LED_OF_3S;
+
+ break;
+ case LED_OF_3S:
+
+ //Led off for 3 seconds:
+ led=0;
+ pc.printf("LED_OF");
+ wait(3);
+ state = LED_ON_4S;
+ break;
+ case LED_ON_4S:
+
+ //Led on for 4 seconds:
+ led=1;
+ pc.printf("LED_ON");
+ wait(4);
+ state = LED_OF_1S;
+ break;
+ }
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu May 28 18:37:18 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/cbbeb26dbd92 \ No newline at end of file
Thiago .