Advertisement
pleasedontcode

"Traffic Toggle" rev_03

Jun 8th, 2024
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Traffic Toggle"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-08 13:57:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop a car and pedestrian traffic light */
  21.     /* simulation with EasyButton. Utilize pin D2 for */
  22.     /* button input, and pins D3, D4, D5, and D6 for LED */
  23.     /* outputs. Ensure LEDs change states correctly in */
  24.     /* the loop function, reflecting typical traffic */
  25.     /* light behavior. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36. void onPressed(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t Button_PushButton_PIN_D2 = 2;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t Red_LED_PIN_D3 = 3;
  43. const uint8_t Green_LED_PIN_D4 = 4;
  44. const uint8_t Red_LED_PIN_D5 = 5;
  45. const uint8_t Green_LED_PIN_D6 = 6;
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. /***** used to store raw data *****/
  49. bool Red_LED_PIN_D3_rawData = 0;
  50. bool Green_LED_PIN_D4_rawData = 0;
  51. bool Red_LED_PIN_D5_rawData = 0;
  52. bool Green_LED_PIN_D6_rawData = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float Red_LED_PIN_D3_phyData = 0.0;
  57. float Green_LED_PIN_D4_phyData = 0.0;
  58. float Red_LED_PIN_D5_phyData = 0.0;
  59. float Green_LED_PIN_D6_phyData = 0.0;
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. EasyButton button(Button_PushButton_PIN_D2); // Initialize EasyButton object with the button pin
  63.  
  64. /****** TRAFFIC LIGHT STATES *****/
  65. enum TrafficLightState {
  66.   CAR_GREEN,
  67.   PEDESTRIAN_GREEN
  68. };
  69.  
  70. TrafficLightState currentState = CAR_GREEN;
  71.  
  72. void setup(void)
  73. {
  74.     // put your setup code here, to run once:
  75.     Serial.begin(115200);
  76.     Serial.println();
  77.     Serial.println(">>> EasyButton pressed example <<<");
  78.  
  79.     pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP);
  80.  
  81.     pinMode(Red_LED_PIN_D3, OUTPUT);
  82.     pinMode(Green_LED_PIN_D4, OUTPUT);
  83.     pinMode(Red_LED_PIN_D5, OUTPUT);
  84.     pinMode(Green_LED_PIN_D6, OUTPUT);
  85.  
  86.     button.begin(); // Initialize the button
  87.     button.onPressed(onPressed); // Attach the onPressed function to the button
  88.  
  89.     // Initialize traffic light to initial state
  90.     Red_LED_PIN_D3_rawData = LOW; // Car Red Light OFF
  91.     Green_LED_PIN_D4_rawData = HIGH; // Car Green Light ON
  92.     Red_LED_PIN_D5_rawData = HIGH; // Pedestrian Red Light ON
  93.     Green_LED_PIN_D6_rawData = LOW; // Pedestrian Green Light OFF
  94.  
  95.     updateOutputs(); // Update the initial state of the LEDs
  96. }
  97.  
  98. void loop(void)
  99. {
  100.     // put your main code here, to run repeatedly:
  101.     button.read(); // Read the button state
  102. }
  103.  
  104. void updateOutputs()
  105. {
  106.     digitalWrite(Red_LED_PIN_D3, Red_LED_PIN_D3_rawData);
  107.     digitalWrite(Green_LED_PIN_D4, Green_LED_PIN_D4_rawData);
  108.     digitalWrite(Red_LED_PIN_D5, Red_LED_PIN_D5_rawData);
  109.     digitalWrite(Green_LED_PIN_D6, Green_LED_PIN_D6_rawData);
  110. }
  111.  
  112. void onPressed()
  113. {
  114.     Serial.println("Button pressed");
  115.  
  116.     // Toggle traffic light state
  117.     if (currentState == CAR_GREEN) {
  118.         currentState = PEDESTRIAN_GREEN;
  119.         Red_LED_PIN_D3_rawData = HIGH; // Car Red Light ON
  120.         Green_LED_PIN_D4_rawData = LOW; // Car Green Light OFF
  121.         Red_LED_PIN_D5_rawData = LOW; // Pedestrian Red Light OFF
  122.         Green_LED_PIN_D6_rawData = HIGH; // Pedestrian Green Light ON
  123.     } else {
  124.         currentState = CAR_GREEN;
  125.         Red_LED_PIN_D3_rawData = LOW; // Car Red Light OFF
  126.         Green_LED_PIN_D4_rawData = HIGH; // Car Green Light ON
  127.         Red_LED_PIN_D5_rawData = HIGH; // Pedestrian Red Light ON
  128.         Green_LED_PIN_D6_rawData = LOW; // Pedestrian Green Light OFF
  129.     }
  130.  
  131.     updateOutputs(); // Refresh output data
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement