Advertisement
pleasedontcode

"LED Control" rev_04

Apr 7th, 2024
49
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: "LED Control"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-07 10:07:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when the easy button was pressed, the green led */
  21.     /* flashed 10 times */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* when the green led stops turn on the red led for */
  24.     /* 15 seconds */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void onPressed(void);
  34. void updateOutputs(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t tilt_PushButton_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t red_LED_PIN_D3 = 3;
  41. const uint8_t green_LED_PIN_D4 = 4;
  42.  
  43. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  44. /***** used to store raw data *****/
  45. bool red_LED_PIN_D3_rawData = LOW;
  46. bool green_LED_PIN_D4_rawData = LOW;
  47.  
  48. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  49. /***** used to store data after characteristic curve transformation *****/
  50. float red_LED_PIN_D3_phyData = 0.0;
  51. float green_LED_PIN_D4_phyData = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. EasyButton button(tilt_PushButton_PIN_D2);
  55.  
  56. int greenLedFlashCount = 0; // Counter for green LED flashing
  57.  
  58. unsigned long redLedStartTime = 0; // Start time for red LED on duration
  59. const unsigned long RED_LED_ON_DURATION = 15000; // Duration for red LED on (in milliseconds)
  60.  
  61. void setup(void)
  62. {
  63.   // put your setup code here, to run once:
  64.  
  65.   pinMode(tilt_PushButton_PIN_D2, INPUT_PULLUP);
  66.  
  67.   pinMode(red_LED_PIN_D3, OUTPUT);
  68.   pinMode(green_LED_PIN_D4, OUTPUT);
  69.  
  70.   button.begin();
  71.   button.onPressed(onPressed);
  72. }
  73.  
  74. void loop(void)
  75. {
  76.   // put your main code here, to run repeatedly:
  77.  
  78.   button.read();
  79.   updateOutputs(); // Refresh output data
  80.  
  81.   // Check if green LED flashing is required
  82.   if (greenLedFlashCount > 0)
  83.   {
  84.     digitalWrite(green_LED_PIN_D4, HIGH);
  85.     delay(100);
  86.     digitalWrite(green_LED_PIN_D4, LOW);
  87.     delay(100);
  88.     greenLedFlashCount--;
  89.   }
  90.  
  91.   // Check if red LED on duration has elapsed
  92.   if (redLedStartTime > 0 && millis() - redLedStartTime >= RED_LED_ON_DURATION)
  93.   {
  94.     red_LED_PIN_D3_rawData = LOW;
  95.     redLedStartTime = 0;
  96.   }
  97. }
  98.  
  99. void onPressed(void)
  100. {
  101.   Serial.println("Button pressed");
  102.  
  103.   // Flash the green LED 10 times
  104.   greenLedFlashCount = 10;
  105.  
  106.   // Turn on the red LED for 15 seconds
  107.   red_LED_PIN_D3_rawData = HIGH;
  108.   redLedStartTime = millis();
  109. }
  110.  
  111. void updateOutputs(void)
  112. {
  113.   digitalWrite(red_LED_PIN_D3, red_LED_PIN_D3_rawData);
  114.   digitalWrite(green_LED_PIN_D4, green_LED_PIN_D4_rawData);
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement