/********* 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: "Traffic Toggle" - Source Code compiled for: Arduino Uno - Source Code created on: 2024-06-08 13:57:28 ********* 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 //https://github.com/evert-arias/EasyButton /****** FUNCTION PROTOTYPES *****/ void setup(void); void loop(void); void updateOutputs(void); void onPressed(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 with the button pin /****** TRAFFIC LIGHT STATES *****/ enum TrafficLightState { CAR_GREEN, PEDESTRIAN_GREEN }; TrafficLightState currentState = CAR_GREEN; void setup(void) { // put your setup code here, to run once: Serial.begin(115200); Serial.println(); Serial.println(">>> EasyButton pressed 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(onPressed); // Attach the onPressed function to the button // Initialize traffic light to initial state Red_LED_PIN_D3_rawData = LOW; // Car Red Light OFF Green_LED_PIN_D4_rawData = HIGH; // Car Green Light ON Red_LED_PIN_D5_rawData = HIGH; // Pedestrian Red Light ON Green_LED_PIN_D6_rawData = LOW; // Pedestrian Green Light OFF updateOutputs(); // Update the initial state of the LEDs } void loop(void) { // put your main code here, to run repeatedly: button.read(); // Read the button state } void updateOutputs() { digitalWrite(Red_LED_PIN_D3, Red_LED_PIN_D3_rawData); digitalWrite(Green_LED_PIN_D4, Green_LED_PIN_D4_rawData); digitalWrite(Red_LED_PIN_D5, Red_LED_PIN_D5_rawData); digitalWrite(Green_LED_PIN_D6, Green_LED_PIN_D6_rawData); } void onPressed() { Serial.println("Button pressed"); // Toggle traffic light state if (currentState == CAR_GREEN) { currentState = PEDESTRIAN_GREEN; Red_LED_PIN_D3_rawData = HIGH; // Car Red Light ON Green_LED_PIN_D4_rawData = LOW; // Car Green Light OFF Red_LED_PIN_D5_rawData = LOW; // Pedestrian Red Light OFF Green_LED_PIN_D6_rawData = HIGH; // Pedestrian Green Light ON } else { currentState = CAR_GREEN; Red_LED_PIN_D3_rawData = LOW; // Car Red Light OFF Green_LED_PIN_D4_rawData = HIGH; // Car Green Light ON Red_LED_PIN_D5_rawData = HIGH; // Pedestrian Red Light ON Green_LED_PIN_D6_rawData = LOW; // Pedestrian Green Light OFF } updateOutputs(); // Refresh output data }