Skip to content

Instantly share code, notes, and snippets.

@jrelo
Created December 3, 2024 17:50
Show Gist options
  • Save jrelo/c5d315fe5209988511fa57191035e4c9 to your computer and use it in GitHub Desktop.
Save jrelo/c5d315fe5209988511fa57191035e4c9 to your computer and use it in GitHub Desktop.
traffic light state machine example
#include <stdio.h>
enum TrafficLightState {
RED,
GREEN,
YELLOW
};
void transition(enum TrafficLightState *state) {
switch (*state) {
case RED:
*state = GREEN;
break;
case GREEN:
*state = YELLOW;
break;
case YELLOW:
*state = RED;
break;
}
}
int main() {
enum TrafficLightState currentState = RED;
while (1) {
switch (currentState) {
case RED:
printf("Red light\n");
break;
case GREEN:
printf("Green light\n");
break;
case YELLOW:
printf("Yellow light\n");
break;
}
// simulate a timer or event triggering state transition
transition(&currentState);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment