Advertisement
pleasedontcode

"Button-Controlled Trafficlight" rev_01

Jun 8th, 2024
357
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: "Button-Controlled Trafficlight"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-08 13:29:02
  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 onButtonPressed(void);
  35. void updateOutputs(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
  62.  
  63. /****** TRAFFIC LIGHT STATES *****/
  64. enum TrafficLightState {
  65.   CAR_GREEN_PEDESTRIAN_RED,
  66.   CAR_YELLOW_PEDESTRIAN_RED,
  67.   CAR_RED_PEDESTRIAN_GREEN
  68. };
  69.  
  70. TrafficLightState currentState = CAR_GREEN_PEDESTRIAN_RED;
  71.  
  72. void setup(void)
  73. {
  74.   // put your setup code here, to run once:
  75.  
  76.   Serial.begin(115200);  // Initialize Serial for debugging
  77.   Serial.println();
  78.   Serial.println(">>> EasyButton example <<<");
  79.  
  80.   pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP);
  81.  
  82.   pinMode(Red_LED_PIN_D3, OUTPUT);
  83.   pinMode(Green_LED_PIN_D4, OUTPUT);
  84.   pinMode(Red_LED_PIN_D5, OUTPUT);
  85.   pinMode(Green_LED_PIN_D6, OUTPUT);
  86.  
  87.   button.begin();  // Initialize the button
  88.   button.onPressed(onButtonPressed);  // Attach the callback function
  89.  
  90.   updateOutputs();  // Initialize the traffic light outputs
  91. }
  92.  
  93. void loop(void)
  94. {
  95.   // put your main code here, to run repeatedly:
  96.  
  97.   button.read();  // Continuously read the status of the button
  98. }
  99.  
  100. void onButtonPressed()
  101. {
  102.   Serial.println("Button pressed");  // Print message when button is pressed
  103.  
  104.   // Change traffic light state
  105.   switch (currentState) {
  106.     case CAR_GREEN_PEDESTRIAN_RED:
  107.       currentState = CAR_YELLOW_PEDESTRIAN_RED;
  108.       break;
  109.     case CAR_YELLOW_PEDESTRIAN_RED:
  110.       currentState = CAR_RED_PEDESTRIAN_GREEN;
  111.       break;
  112.     case CAR_RED_PEDESTRIAN_GREEN:
  113.       currentState = CAR_GREEN_PEDESTRIAN_RED;
  114.       break;
  115.   }
  116.  
  117.   updateOutputs();  // Update the outputs based on the new state
  118. }
  119.  
  120. void updateOutputs()
  121. {
  122.   // Update the LED outputs based on the current state
  123.   switch (currentState) {
  124.     case CAR_GREEN_PEDESTRIAN_RED:
  125.       digitalWrite(Red_LED_PIN_D3, LOW);
  126.       digitalWrite(Green_LED_PIN_D4, HIGH);
  127.       digitalWrite(Red_LED_PIN_D5, HIGH);
  128.       digitalWrite(Green_LED_PIN_D6, LOW);
  129.       break;
  130.     case CAR_YELLOW_PEDESTRIAN_RED:
  131.       digitalWrite(Red_LED_PIN_D3, LOW);
  132.       digitalWrite(Green_LED_PIN_D4, LOW);
  133.       digitalWrite(Red_LED_PIN_D5, HIGH);
  134.       digitalWrite(Green_LED_PIN_D6, LOW);
  135.       break;
  136.     case CAR_RED_PEDESTRIAN_GREEN:
  137.       digitalWrite(Red_LED_PIN_D3, HIGH);
  138.       digitalWrite(Green_LED_PIN_D4, LOW);
  139.       digitalWrite(Red_LED_PIN_D5, LOW);
  140.       digitalWrite(Green_LED_PIN_D6, HIGH);
  141.       break;
  142.   }
  143. }
  144.  
  145. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement