Devices & Components
Arduino Uno Rev3
Breadboard (generic)
Resistor 221 ohm
LED (generic)
Software & Tools
Arduino IDE
Project description
Code
OO State Machine
c_cpp
Simple example of an object oriented state machine with two states.
1 2class State { 3 public: 4 virtual ~State(){} 5 virtual void enter(){}; 6 virtual State* run() = 0; 7}; 8 9class White: public State { 10 public: 11 virtual void enter() override; 12 virtual State* run() override; 13}; 14 15class Green: public State { 16 public: 17 virtual void enter() override; 18 virtual State* run() override; 19}; 20 21static White white; 22static Green green; 23unsigned long lastMillis = 0; 24 25void flash(int pin) { 26 for(int i = 0; i<20; ++i) { 27 digitalWrite(pin,!digitalRead(pin)); 28 delay(30); 29 } 30} 31 32State* White::run() { 33 if (millis() < lastMillis + 1000) { 34 digitalWrite(13,HIGH); 35 return this; 36 } 37 else { 38 lastMillis = millis(); 39 digitalWrite(13,LOW); 40 return &green; 41 } 42} 43 44void White::enter() { 45 flash(13); 46 // update the timer 47 lastMillis = millis(); 48} 49 50State* Green::run() { 51 if (millis() < lastMillis + 2000) { 52 digitalWrite(12,HIGH); 53 return this; 54 } 55 else { 56 lastMillis = millis(); 57 digitalWrite(12,LOW); 58 return &white; 59 } 60} 61 62void Green::enter() { 63 flash(12); 64 // update the timer 65 lastMillis = millis(); 66} 67 68void setup() { 69 pinMode(13,OUTPUT); 70 pinMode(12,OUTPUT); 71} 72 73bool isChanged = false; 74State* state = &white; 75State* lastState = state; 76void loop() { 77 // if state changed last time, call enter 78 if (isChanged) 79 lastState->enter(); 80 // call run repeatedly 81 state = lastState->run(); 82 // if state changed, record it; 83 isChanged = (state != lastState); 84 // reset lastState to current 85 lastState = state; 86} 87 88
OO State Machine
c_cpp
Simple example of an object oriented state machine with two states.
1 2class State { 3 public: 4 virtual ~State(){} 5 virtual void enter(){}; 6 virtual State* run() = 0; 7}; 8 9class White: public State { 10 public: 11 virtual void enter() override; 12 virtual State* run() override; 13}; 14 15class Green: public State { 16 public: 17 virtual void enter() override; 18 virtual State* run() override; 19}; 20 21static White white; 22static Green green; 23unsigned long lastMillis = 0; 24 25void flash(int pin) { 26 for(int i = 0; i<20; ++i) { 27 digitalWrite(pin,!digitalRead(pin)); 28 delay(30); 29 } 30} 31 32State* White::run() { 33 if (millis() < lastMillis + 1000) { 34 digitalWrite(13,HIGH); 35 return this; 36 } 37 else { 38 lastMillis = millis(); 39 digitalWrite(13,LOW); 40 return &green; 41 } 42} 43 44void White::enter() { 45 flash(13); 46 // update the timer 47 lastMillis = millis(); 48} 49 50State* Green::run() { 51 if (millis() < lastMillis + 2000) { 52 digitalWrite(12,HIGH); 53 return this; 54 } 55 else { 56 lastMillis = millis(); 57 digitalWrite(12,LOW); 58 return &white; 59 } 60} 61 62void Green::enter() { 63 flash(12); 64 // update the timer 65 lastMillis = millis(); 66} 67 68void setup() { 69 pinMode(13,OUTPUT); 70 pinMode(12,OUTPUT); 71} 72 73bool isChanged = false; 74State* state = &white; 75State* lastState = state; 76void loop() { 77 // if state changed last time, call enter 78 if (isChanged) 79 lastState->enter(); 80 // call run repeatedly 81 state = lastState->run(); 82 // if state changed, record it; 83 isChanged = (state != lastState); 84 // reset lastState to current 85 lastState = state; 86} 87 88
Downloadable files
Fritzing diagram of state machine
Fritzing diagram of state machine

Comments
Only logged in users can leave comments