Advertisement
pleasedontcode

"Traffic Toggle" rev_02

Jun 8th, 2024
346
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 NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-08 13:52:57
  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. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void onPressed(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t Button_PushButton_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t Red_LED_PIN_D3 = 3;
  42. const uint8_t Green_LED_PIN_D4 = 4;
  43. const uint8_t Red_LED_PIN_D5 = 5;
  44. const uint8_t Green_LED_PIN_D6 = 6;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool Red_LED_PIN_D3_rawData = 0;
  49. bool Green_LED_PIN_D4_rawData = 0;
  50. bool Red_LED_PIN_D5_rawData = 0;
  51. bool Green_LED_PIN_D6_rawData = 0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. /***** used to store data after characteristic curve transformation *****/
  55. float Red_LED_PIN_D3_phyData = 0.0;
  56. float Green_LED_PIN_D4_phyData = 0.0;
  57. float Red_LED_PIN_D5_phyData = 0.0;
  58. float Green_LED_PIN_D6_phyData = 0.0;
  59.  
  60. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  61. EasyButton button(Button_PushButton_PIN_D2); // Initialize EasyButton object with the button pin
  62.  
  63. /****** TRAFFIC LIGHT STATES *****/
  64. enum TrafficLightState {
  65.   CAR_GREEN,
  66.   PEDESTRIAN_GREEN
  67. };
  68.  
  69. TrafficLightState currentState = CAR_GREEN;
  70.  
  71. void setup(void)
  72. {
  73.     // put your setup code here, to run once:
  74.     Serial.begin(115200);
  75.     Serial.println();
  76.     Serial.println(">>> EasyButton pressed example <<<");
  77.  
  78.     pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP);
  79.  
  80.     pinMode(Red_LED_PIN_D3, OUTPUT);
  81.     pinMode(Green_LED_PIN_D4, OUTPUT);
  82.     pinMode(Red_LED_PIN_D5, OUTPUT);
  83.     pinMode(Green_LED_PIN_D6, OUTPUT);
  84.  
  85.     button.begin(); // Initialize the button
  86.     button.onPressed(onPressed); // Attach the onPressed function to the button
  87.  
  88.     // Initialize traffic light to initial state
  89.     Red_LED_PIN_D3_rawData = LOW; // Car Red Light OFF
  90.     Green_LED_PIN_D4_rawData = HIGH; // Car Green Light ON
  91.     Red_LED_PIN_D5_rawData = HIGH; // Pedestrian Red Light ON
  92.     Green_LED_PIN_D6_rawData = LOW; // Pedestrian Green Light OFF
  93.  
  94.     updateOutputs(); // Update the initial state of the LEDs
  95. }
  96.  
  97. void loop(void)
  98. {
  99.     // put your main code here, to run repeatedly:
  100.     button.read(); // Read the button state
  101. }
  102.  
  103. void updateOutputs()
  104. {
  105.     digitalWrite(Red_LED_PIN_D3, Red_LED_PIN_D3_rawData);
  106.     digitalWrite(Green_LED_PIN_D4, Green_LED_PIN_D4_rawData);
  107.     digitalWrite(Red_LED_PIN_D5, Red_LED_PIN_D5_rawData);
  108.     digitalWrite(Green_LED_PIN_D6, Green_LED_PIN_D6_rawData);
  109. }
  110.  
  111. void onPressed()
  112. {
  113.     Serial.println("Button pressed");
  114.  
  115.     // Toggle traffic light state
  116.     if (currentState == CAR_GREEN) {
  117.         currentState = PEDESTRIAN_GREEN;
  118.         Red_LED_PIN_D3_rawData = HIGH; // Car Red Light ON
  119.         Green_LED_PIN_D4_rawData = LOW; // Car Green Light OFF
  120.         Red_LED_PIN_D5_rawData = LOW; // Pedestrian Red Light OFF
  121.         Green_LED_PIN_D6_rawData = HIGH; // Pedestrian Green Light ON
  122.     } else {
  123.         currentState = CAR_GREEN;
  124.         Red_LED_PIN_D3_rawData = LOW; // Car Red Light OFF
  125.         Green_LED_PIN_D4_rawData = HIGH; // Car Green Light ON
  126.         Red_LED_PIN_D5_rawData = HIGH; // Pedestrian Red Light ON
  127.         Green_LED_PIN_D6_rawData = LOW; // Pedestrian Green Light OFF
  128.     }
  129.  
  130.     updateOutputs(); // Refresh output data
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement