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: Traffic Controller
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-11-23 07:01:06
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Implement a scheduling system for traffic lights */
- /* (yellow, red, green, Don't Walk, Walk) */
- /* synchronized with a push button to activate Walk */
- /* mode during yellow or red lights. Use the */
- /* EasyButton library and RTC for precise timing. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- // SYSTEM REQUIREMENTS:
- // Implement a scheduling system for traffic lights
- // (yellow, red, green, Don't Walk, Walk)
- // synchronized with a push button to activate Walk mode during yellow or red lights.
- // Use EasyButton library and RTC for precise timing.
- #include <EasyButton.h>
- #include <PCF85063A-SOLDERED.h>
- // Define pins for traffic lights
- const uint8_t pinYellow = 12;
- const uint8_t pinRed = 11;
- const uint8_t pinGreen = 10;
- // Define pins for pedestrian signals
- const uint8_t pinDontWalk = 9;
- const uint8_t pinWalk = 8;
- // Define push button pin to activate Walk mode
- const uint8_t buttonPin = 2;
- // Timing variables in milliseconds
- unsigned long yellowDuration = 4000;
- unsigned long redDuration = 4000;
- unsigned long greenDuration = 4000;
- unsigned long walkDuration = 2000;
- unsigned long flashInterval = 250;
- // Define button instance
- EasyButton walkButton(buttonPin);
- // Define RTC instance
- PCF85063A rtc;
- // State enumeration for traffic light states
- enum State {
- S_YELLOW,
- S_RED,
- S_GREEN,
- S_DONT_WALK,
- S_WALK_STEADY,
- S_WALK_FLASH
- };
- State currentState = S_YELLOW;
- unsigned long stateStartTime = 0;
- bool walkModeActivated = false;
- bool walkButtonPressed = false;
- void setup() {
- // Initialize serial for debugging
- Serial.begin(115200);
- // Initialize pins
- pinMode(pinYellow, OUTPUT);
- pinMode(pinRed, OUTPUT);
- pinMode(pinGreen, OUTPUT);
- pinMode(pinDontWalk, OUTPUT);
- pinMode(pinWalk, OUTPUT);
- // Initialize RTC
- rtc.begin();
- // Set initial time and date
- rtc.setTime(6, 0, 0); // 6:00:00
- rtc.setDate(6, 15, 5, 2020); // Saturday, 15.05.2020
- // Set RTC alarm for scheduled transition if needed
- // (Optional, not used in current logic)
- // Initialize button
- walkButton.begin();
- walkButton.onPressed([](){
- walkButtonPressed = true;
- });
- // Start with yellow light
- currentState = S_YELLOW;
- stateStartTime = millis();
- }
- void loop() {
- // Read button state
- walkButton.read();
- // Check if walk button was pressed
- if (walkButtonPressed) {
- walkModeActivated = true;
- walkButtonPressed = false;
- }
- unsigned long currentTime = millis();
- switch (currentState) {
- case S_YELLOW:
- digitalWrite(pinYellow, HIGH);
- digitalWrite(pinRed, LOW);
- digitalWrite(pinGreen, LOW);
- digitalWrite(pinDontWalk, HIGH);
- digitalWrite(pinWalk, LOW);
- if (currentTime - stateStartTime >= yellowDuration) {
- currentState = S_RED;
- stateStartTime = currentTime;
- }
- break;
- case S_RED:
- digitalWrite(pinRed, HIGH);
- digitalWrite(pinYellow, LOW);
- digitalWrite(pinGreen, LOW);
- digitalWrite(pinDontWalk, HIGH);
- digitalWrite(pinWalk, LOW);
- if (currentTime - stateStartTime >= redDuration || walkModeActivated) {
- currentState = S_GREEN;
- stateStartTime = currentTime;
- walkModeActivated = false;
- }
- break;
- case S_GREEN:
- digitalWrite(pinGreen, HIGH);
- digitalWrite(pinYellow, LOW);
- digitalWrite(pinRed, LOW);
- digitalWrite(pinDontWalk, HIGH);
- digitalWrite(pinWalk, LOW);
- if (currentTime - stateStartTime >= greenDuration) {
- currentState = S_DONT_WALK;
- stateStartTime = currentTime;
- }
- break;
- case S_DONT_WALK:
- digitalWrite(pinDontWalk, HIGH);
- digitalWrite(pinWalk, LOW);
- if (walkModeActivated) {
- currentState = S_WALK_STEADY;
- stateStartTime = currentTime;
- } else if (currentTime - stateStartTime >= redDuration) {
- currentState = S_YELLOW;
- stateStartTime = currentTime;
- }
- break;
- case S_WALK_STEADY:
- digitalWrite(pinDontWalk, LOW);
- digitalWrite(pinWalk, HIGH);
- if (currentTime - stateStartTime >= walkDuration) {
- currentState = S_WALK_FLASH;
- stateStartTime = currentTime;
- }
- break;
- case S_WALK_FLASH:
- if ((currentTime / flashInterval) % 2 == 0) {
- digitalWrite(pinWalk, HIGH);
- } else {
- digitalWrite(pinWalk, LOW);
- }
- if (currentTime - stateStartTime >= walkDuration) {
- digitalWrite(pinWalk, LOW);
- digitalWrite(pinDontWalk, HIGH);
- walkModeActivated = false;
- currentState = S_YELLOW;
- stateStartTime = currentTime;
- }
- break;
- }
- // Optional: Use RTC to schedule precise transitions or for logging
- // Not used directly in this implementation
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment