Last active
March 29, 2023 12:51
-
-
Save jbutcher5/7dd5c0b5d4f3fa1ac7b70ef47e5a80c7 to your computer and use it in GitHub Desktop.
A traffic light simulator for an arduino uno
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
typedef enum { | |
Pedestrian, | |
Traffic | |
} LightType; | |
typedef struct { | |
int lights[2]; | |
int phase; | |
} PedestrianLights; | |
typedef struct { | |
int lights[3]; | |
PedestrianLights *pedestrian_lights; | |
int phase; | |
} TrafficLights; | |
typedef struct { | |
LightType type; | |
void *content; | |
} Lights; | |
void set_phase(Lights lights, int phase) { | |
if (lights.type == Pedestrian) { | |
PedestrianLights *content = lights.content; | |
content->phase = phase; | |
if (!phase) { | |
digitalWrite(content->lights[0], HIGH); | |
digitalWrite(content->lights[1], LOW); | |
} else if (phase) { | |
digitalWrite(content->lights[0], LOW); | |
digitalWrite(content->lights[1], HIGH); | |
} | |
} else if (lights.type == Traffic) { | |
TrafficLights *content = lights.content; | |
content->phase = phase; | |
if (!phase) { | |
digitalWrite(content->lights[0], HIGH); | |
digitalWrite(content->lights[1], LOW); | |
digitalWrite(content->lights[2], LOW); | |
} else if (phase == 1) { | |
digitalWrite(content->lights[0], HIGH); | |
digitalWrite(content->lights[1], HIGH); | |
digitalWrite(content->lights[2], LOW); | |
} else if (phase == 2) { | |
digitalWrite(content->lights[0], LOW); | |
digitalWrite(content->lights[1], LOW); | |
digitalWrite(content->lights[2], HIGH); | |
} else if (phase == 3) { | |
digitalWrite(content->lights[0], LOW); | |
digitalWrite(content->lights[1], HIGH); | |
digitalWrite(content->lights[2], LOW); | |
} | |
} | |
} | |
void next_phase(Lights lights) { | |
if (lights.type == Pedestrian) { | |
PedestrianLights *content = lights.content; | |
set_phase(lights, !content->phase); | |
} else if (lights.type == Traffic) { | |
TrafficLights *content = lights.content; | |
int phase = content->phase + 1; | |
if (phase > 3) phase = 0; | |
set_phase(lights, phase); | |
} | |
} | |
PedestrianLights first_p_content; | |
Lights first_p; | |
TrafficLights first_t_content; | |
Lights first_t; | |
void setup() { | |
first_p_content = { | |
.lights = {13, 12}, | |
.phase = 0 | |
}; | |
first_p = { | |
.type = Pedestrian, | |
.content = (void *)&first_p_content | |
}; | |
first_t_content = { | |
.lights = {9, 10, 11}, | |
.pedestrian_lights = &first_p_content, | |
.phase = 0 | |
}; | |
first_t = { | |
.type = Traffic, | |
.content = (void *)&first_t_content | |
}; | |
pinMode(13, OUTPUT); | |
pinMode(12, OUTPUT); | |
pinMode(11, OUTPUT); | |
pinMode(10, OUTPUT); | |
pinMode(9, OUTPUT); | |
//set_phase(first_p, 0); | |
set_phase(first_t, 0); | |
} | |
void loop() { | |
//next_phase(first_p); | |
delay(1000); | |
next_phase(first_t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment