// Setting up embedded environment
#![no_std]
#![no_main]
// Error handler
use esp_backtrace as _;
// Accessing the embedded hal for embedded development
use esp_hal::{
clock::CpuClock,
delay::Delay,
gpio::{Io, Level, Output},
main,
};
enum TrafficLights {
Red,
Amber,
Green,
Hold,
}
// Creating the main function
#[main]
fn main() -> ! {
// Accessing the in built clock component
let clocks = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
// Linking peripeharels to the clock
let components = esp_hal::init(clocks);
// Setting the pins i want to access
let mut Go = Output::new(components.GPIO4, Level::Low);
let mut Stop = Output::new(components.GPIO26, Level::Low);
let mut SlowDown = Output::new(components.GPIO22, Level::Low);
// If a pedestrian wants to walk because of heavy traffic
// let mut hold = Input::new(components.GPIO2, Pull::Down);
// Setting up a new dalay
let delay = Delay::new();
// Creating ticks to time the led controlls
let mut TrafficLightStatus = TrafficLights::Red;
let mut TrafficLightTimeDelay;
// Looping the led
// State transition machine controls the Traffic Lights
// Stop => Go => SlowDown => Stop...
loop {
// if hold.is_low() {
// TrafficLightStatus = TrafficLights::Hold;
// }
match TrafficLightStatus {
TrafficLights::Red => {
Stop.set_high();
SlowDown.set_low();
Go.set_low();
TrafficLightTimeDelay = 3000;
TrafficLightStatus = TrafficLights::Green;
},
TrafficLights::Amber => {
Stop.set_low();
SlowDown.set_high();
Go.set_low();
TrafficLightTimeDelay = 1000;
TrafficLightStatus = TrafficLights::Red;
},
TrafficLights::Green => {
Stop.set_low();
SlowDown.set_low();
Go.set_high();
TrafficLightTimeDelay = 5000;
TrafficLightStatus = TrafficLights::Amber;
},
TrafficLights::Hold => {
stop.set_high();
SlowDown.set_high();
go.set_high();
TrafficLightTimeDelay = 1000;
TrafficLightStatus = TrafficLights::Red;
}
}
delay.delay_millis(TrafficLightTimeDelay);
}
}