Created
December 3, 2024 17:50
-
-
Save jrelo/c5d315fe5209988511fa57191035e4c9 to your computer and use it in GitHub Desktop.
traffic light state machine example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(¤tState); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment