/*
Interview project for Lexington Medical
Prepared 2025-10-27
STM32 Arduino Core (stm32duino)
Specification:
1. With no use of the crosswalk button the traffic lights should cycle between
red/yellow/green per the following sequence:
13 seconds: Mass Ave Green, Beacon St Red
2 seconds: Mass Ave Yellow, Beacon St Red
13 seconds: Mass Ave Red, Beacon St Green
2 seconds: Mass Ave Red, Beacon St Yellow
2. If ever both traffic lights are red, pedestrian light can be green,
otherwise pedestrian light should be red.
3. If the crosswalk button is pressed then whichever traffic light is
currently green (or yellow) should transition if needed to the yellow
state and then after finishing the 2 second yellow state, both traffic
lights should be red for 20 seconds before resuming green on whichever
traffic light holds the longest elapsed time since last being green.
4. Once the crosswalk button has been pressed, further presses should be
queued until one full cycle has finished (meaning 20 seconds for the
pedestrian cycle and 60 seconds for one full traffic cycle).
For reference guides see https://docs.wokwi.com/
*/
extern "C" {
#include "traffic_ctrl.h"
#include "seg_display.h"
#include "buzzer.h"
}
// Pin map per diagram.json
constexpr uint8_t PIN_MASS_AVE_RED = A0;
constexpr uint8_t PIN_MASS_AVE_YELLOW = A1;
constexpr uint8_t PIN_MASS_AVE_GREEN = A2;
constexpr uint8_t PIN_BEACON_ST_RED = A5;
constexpr uint8_t PIN_BEACON_ST_YELLOW = A6;
constexpr uint8_t PIN_BEACON_ST_GREEN = A7;
constexpr uint8_t PIN_PED_RED = A3;
constexpr uint8_t PIN_PED_GREEN = A4;
constexpr uint8_t PIN_BTN = PB0;
//Extension pins
constexpr uint8_t PIN_SEG[7] = {PB5, PB6, PB7, PB8, PB9, PB10, PB11};
constexpr uint8_t PIN_DIG[2] = {PB12, PB13};
constexpr uint8_t PIN_PIR = PB14;
constexpr uint8_t PIN_BUZZER = PA8;
constexpr unsigned int PRESENCE_HZ = 1300;
constexpr unsigned int WALK_HZ = 1800;
constexpr unsigned long BUZZER_MS = 1000;
constexpr uint32_t DEBOUNCE_MS = 25;
static trafficController tc;
static segDisplay display;
static buzzer buzz;
static controllerState lastLoggedState;
static uint8_t lastLoggedPending;
static const char *stateName(controllerState s) {
switch (s) {
case TC_MASS_GREEN:
return "Mass GREEN / Beacon RED";
case TC_MASS_YELLOW:
return "Mass YELLOW / Beacon RED";
case TC_BEACON_GREEN:
return "Mass RED / Beacon GREEN";
case TC_BEACON_YELLOW:
return "Mass RED / Beacon YELLOW";
case TC_CROSSWALK:
return "ALL RED, PED GREEN";
}
return "?";
}
static bool buttonPressed(uint32_t now) {
static bool rawLast = HIGH;
static bool stable = HIGH;
static uint32_t lastChange = 0;
bool raw = digitalRead(PIN_BTN);
if (raw != rawLast) {
rawLast = raw;
lastChange = now;
}
if ((now - lastChange) >= DEBOUNCE_MS && raw != stable) {
stable = raw;
if (stable == LOW) return true;
}
return false;
}
void setup() {
pinMode(PIN_MASS_AVE_RED, OUTPUT);
pinMode(PIN_MASS_AVE_YELLOW, OUTPUT);
pinMode(PIN_MASS_AVE_GREEN, OUTPUT);
pinMode(PIN_BEACON_ST_RED, OUTPUT);
pinMode(PIN_BEACON_ST_YELLOW, OUTPUT);
pinMode(PIN_BEACON_ST_GREEN, OUTPUT);
pinMode(PIN_PED_RED, OUTPUT);
pinMode(PIN_PED_GREEN, OUTPUT);
pinMode(PIN_BTN, INPUT_PULLUP);
pinMode(PIN_PIR, INPUT);
pinMode(PIN_BUZZER, OUTPUT);
Serial.begin(115200);
trafficLight massAve = {PIN_MASS_AVE_RED, PIN_MASS_AVE_YELLOW, PIN_MASS_AVE_GREEN};
trafficLight beaconSt = {PIN_BEACON_ST_RED, PIN_BEACON_ST_YELLOW, PIN_BEACON_ST_GREEN};
crosswalkLight cw = {PIN_PED_RED, PIN_PED_GREEN};
uint32_t now = millis();
TC_init(&tc, massAve, beaconSt, cw, now);
SEG_init(&display, PIN_SEG, PIN_DIG, now);
BUZZ_init(&buzz);
lastLoggedState = tc.state;
lastLoggedPending = 0;
Serial.println(stateName(tc.state));
}
void loop() {
uint32_t now = millis();
if (buttonPressed(now)) {
TC_crosswalkPress(&tc);
}
TC_tick(&tc, now);
bool walk = TC_isCrosswalkGreen(&tc);
//Timer Code
SEG_set(&display, walk ? (int16_t)TC_crosswalkSecondsLeft(&tc, now) : -1);
SEG_refresh(&display, now);
//Timer Code End
//Buzzer & Motion Detector Code
bool personNearby = (digitalRead(PIN_PIR) == HIGH);
switch (BUZZ_tick(&buzz, walk, personNearby, now)) {
case BUZZ_WALK:
noTone(PIN_BUZZER);
tone(PIN_BUZZER, WALK_HZ, BUZZER_MS);
Serial.println(" [sound] walk");
break;
case BUZZ_WAIT:
tone(PIN_BUZZER, PRESENCE_HZ, BUZZER_MS);
Serial.println(" [sound] presence beep");
break;
case BUZZ_NONE:
break;
}
//Buzzer & Motion Detector End
if (tc.state != lastLoggedState || tc.cwRequests != lastLoggedPending) {
Serial.print(now / 1000);
Serial.print("s ");
Serial.print(stateName(tc.state));
Serial.print(" (queued presses: ");
Serial.print(tc.cwRequests);
Serial.println(")");
lastLoggedState = tc.state;
lastLoggedPending = tc.cwRequests;
}
}Loading
stm32-bluepill
stm32-bluepill