Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "Button-Controlled Trafficlight"
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-06-08 13:29:02
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Develop a car and pedestrian traffic light */
- /* simulation with EasyButton. Utilize pin D2 for */
- /* button input, and pins D3, D4, D5, and D6 for LED */
- /* outputs. Ensure LEDs change states correctly in */
- /* the loop function, reflecting typical traffic */
- /* light behavior. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void onButtonPressed(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Button_PushButton_PIN_D2 = 2;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Red_LED_PIN_D3 = 3;
- const uint8_t Green_LED_PIN_D4 = 4;
- const uint8_t Red_LED_PIN_D5 = 5;
- const uint8_t Green_LED_PIN_D6 = 6;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool Red_LED_PIN_D3_rawData = 0;
- bool Green_LED_PIN_D4_rawData = 0;
- bool Red_LED_PIN_D5_rawData = 0;
- bool Green_LED_PIN_D6_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float Red_LED_PIN_D3_phyData = 0.0;
- float Green_LED_PIN_D4_phyData = 0.0;
- float Red_LED_PIN_D5_phyData = 0.0;
- float Green_LED_PIN_D6_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- EasyButton button(Button_PushButton_PIN_D2); // Initialize EasyButton object
- /****** TRAFFIC LIGHT STATES *****/
- enum TrafficLightState {
- CAR_GREEN_PEDESTRIAN_RED,
- CAR_YELLOW_PEDESTRIAN_RED,
- CAR_RED_PEDESTRIAN_GREEN
- };
- TrafficLightState currentState = CAR_GREEN_PEDESTRIAN_RED;
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200); // Initialize Serial for debugging
- Serial.println();
- Serial.println(">>> EasyButton example <<<");
- pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP);
- pinMode(Red_LED_PIN_D3, OUTPUT);
- pinMode(Green_LED_PIN_D4, OUTPUT);
- pinMode(Red_LED_PIN_D5, OUTPUT);
- pinMode(Green_LED_PIN_D6, OUTPUT);
- button.begin(); // Initialize the button
- button.onPressed(onButtonPressed); // Attach the callback function
- updateOutputs(); // Initialize the traffic light outputs
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- button.read(); // Continuously read the status of the button
- }
- void onButtonPressed()
- {
- Serial.println("Button pressed"); // Print message when button is pressed
- // Change traffic light state
- switch (currentState) {
- case CAR_GREEN_PEDESTRIAN_RED:
- currentState = CAR_YELLOW_PEDESTRIAN_RED;
- break;
- case CAR_YELLOW_PEDESTRIAN_RED:
- currentState = CAR_RED_PEDESTRIAN_GREEN;
- break;
- case CAR_RED_PEDESTRIAN_GREEN:
- currentState = CAR_GREEN_PEDESTRIAN_RED;
- break;
- }
- updateOutputs(); // Update the outputs based on the new state
- }
- void updateOutputs()
- {
- // Update the LED outputs based on the current state
- switch (currentState) {
- case CAR_GREEN_PEDESTRIAN_RED:
- digitalWrite(Red_LED_PIN_D3, LOW);
- digitalWrite(Green_LED_PIN_D4, HIGH);
- digitalWrite(Red_LED_PIN_D5, HIGH);
- digitalWrite(Green_LED_PIN_D6, LOW);
- break;
- case CAR_YELLOW_PEDESTRIAN_RED:
- digitalWrite(Red_LED_PIN_D3, LOW);
- digitalWrite(Green_LED_PIN_D4, LOW);
- digitalWrite(Red_LED_PIN_D5, HIGH);
- digitalWrite(Green_LED_PIN_D6, LOW);
- break;
- case CAR_RED_PEDESTRIAN_GREEN:
- digitalWrite(Red_LED_PIN_D3, HIGH);
- digitalWrite(Green_LED_PIN_D4, LOW);
- digitalWrite(Red_LED_PIN_D5, LOW);
- digitalWrite(Green_LED_PIN_D6, HIGH);
- break;
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement