Advertisement
pleasedontcode

"LED Control" rev_01

Feb 28th, 2025
233
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 NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-28 21:07:34
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* lo start accende led per 5s (timer1 in interrupt) */
  21.     /* dopo si spegne fino alla successiva pressione   il */
  22.     /* pulsante di stop (interrupt) spegne l'uscita in */
  23.     /* qualsiasi situazione  in interrupt come counter, */
  24.     /* quando arriva a 10 spegnere tutto fino a che non */
  25.     /* si */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t start_PushButton_PIN_D2       = 2;
  39. const uint8_t stop_PushButton_PIN_D3        = 3;
  40. const uint8_t count_PushButton_PIN_D4       = 4;
  41. const uint8_t up_PIN_D8                     = 8; // Added for up button
  42. const uint8_t down_PIN_A4                   = A4; // Added for down button
  43. const uint8_t reset_PIN_D4                  = 4; // Reset pin (already defined)
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t out_LED_PIN_D5        = 5;
  47.  
  48. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  49. /***** used to store raw data *****/
  50. bool    out_LED_PIN_D5_rawData      = 0;
  51.  
  52. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  53. /***** used to store data after characteristic curve transformation *****/
  54. float   out_LED_PIN_D5_phyData      = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57.  
  58. // Global variables for USER CODE
  59. int cont = 0;
  60. int flag = 0;
  61. bool ledActive = false; // To track if the LED is active
  62.  
  63. void setup(void)
  64. {
  65.     // put your setup code here, to run once:
  66.  
  67.     pinMode(start_PushButton_PIN_D2,    INPUT_PULLUP);
  68.     pinMode(stop_PushButton_PIN_D3, INPUT_PULLUP);
  69.     pinMode(count_PushButton_PIN_D4,    INPUT_PULLUP);
  70.     pinMode(out_LED_PIN_D5,  OUTPUT);
  71.  
  72.     // USER CODE setup
  73.     pinMode(LED_BUILTIN, OUTPUT);
  74.     pinMode(up_PIN_D8, INPUT); // up
  75.     pinMode(down_PIN_A4, INPUT); // down
  76.     pinMode(reset_PIN_D4, INPUT); // reset
  77.     Serial.begin(9600);
  78.     TCCR1A = 0B00000000; // Normal Mode
  79.     TIMSK1 = 0B00000001; // INT on overflow
  80.     TCNT1 = -15625;
  81.     sei();
  82. }
  83.  
  84. void loop(void)
  85. {
  86.     // put your main code here, to run repeatedly:
  87.     updateOutputs(); // Refresh output data
  88.  
  89.     // USER CODE loop
  90.     if (flag == 1) { // 5 seconds have passed
  91.         flag = 0;
  92.         ledActive = false; // Deactivate LED
  93.         out_LED_PIN_D5_rawData = LOW; // Turn off LED
  94.         Serial.println("LED turned off after 5 seconds");
  95.     }
  96. }
  97.  
  98. // Function to start the LED
  99. void startLED() {
  100.     ledActive = true; // Set LED active
  101.     out_LED_PIN_D5_rawData = HIGH; // Turn on LED
  102.     cont = 0; // Reset counter
  103.     Serial.println("LED started");
  104. }
  105.  
  106. // Function to stop the LED
  107. void stopLED() {
  108.     ledActive = false; // Set LED inactive
  109.     out_LED_PIN_D5_rawData = LOW; // Turn off LED
  110.     cont = 0; // Reset counter
  111.     Serial.println("LED stopped");
  112. }
  113.  
  114. /***** TIMER INTERRUPT FUNCTION *****/
  115. ISR(TIMER1_OVF_vect) { // Timer interrupt function
  116.     if (ledActive) {
  117.         cont++;
  118.         if (cont >= 5) { // 5 seconds
  119.             flag = 1; // Set flag to indicate timeout
  120.         }
  121.     }
  122.     TCNT1 = -15625; // Reset timer
  123. }
  124.  
  125. // Update outputs
  126. void updateOutputs()
  127. {
  128.     digitalWrite(out_LED_PIN_D5, out_LED_PIN_D5_rawData);
  129. }
  130.  
  131. // Button press handling
  132. void handleButtonPress() {
  133.     if (digitalRead(start_PushButton_PIN_D2) == LOW) {
  134.         startLED(); // Start LED on button press
  135.     }
  136.     if (digitalRead(stop_PushButton_PIN_D3) == LOW) {
  137.         stopLED(); // Stop LED on button press
  138.     }
  139. }
  140.  
  141. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement