Advertisement
pleasedontcode

"Interval Activations" rev_01

Jun 7th, 2024
531
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: "Interval Activations"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-07 13:36:55
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* initiate the system to control led, buzzer and */
  21.     /* solenoid each going on in every 5, 10, and 20 */
  22.     /* minutes respectively for 3seconds */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <TimerOne.h> // Include TimerOne library for handling timing intervals
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void updateOutputs(void);
  32. void timerISR(void);
  33.  
  34. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  35. const uint8_t LED_PIN = 2;
  36. const uint8_t BUZZER_PIN = 3;
  37. const uint8_t SOLENOID_PIN = 4;
  38.  
  39. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  40. /***** used to store raw data *****/
  41. bool LED_PIN_rawData = 0;
  42. bool BUZZER_PIN_rawData = 0;
  43. bool SOLENOID_PIN_rawData = 0;
  44.  
  45. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  46. /***** used to store data after characteristic curve transformation *****/
  47. float LED_PIN_phyData = 0.0;
  48. float BUZZER_PIN_phyData = 0.0;
  49. float SOLENOID_PIN_phyData = 0.0;
  50.  
  51. /***** DEFINITION OF TIMING VARIABLES *****/
  52. unsigned long lastLEDTime = 0;
  53. unsigned long lastBuzzerTime = 0;
  54. unsigned long lastSolenoidTime = 0;
  55. const unsigned long LED_INTERVAL = 5 * 60 * 1000; // 5 minutes in milliseconds
  56. const unsigned long BUZZER_INTERVAL = 10 * 60 * 1000; // 10 minutes in milliseconds
  57. const unsigned long SOLENOID_INTERVAL = 20 * 60 * 1000; // 20 minutes in milliseconds
  58. const unsigned long ON_DURATION = 3000; // 3 seconds in milliseconds
  59.  
  60. void setup(void)
  61. {
  62.     // put your setup code here, to run once:
  63.     pinMode(LED_PIN, OUTPUT);
  64.     pinMode(BUZZER_PIN, OUTPUT);
  65.     pinMode(SOLENOID_PIN, OUTPUT);
  66.  
  67.     // Initialize Timer1 to call timerISR every 1 second
  68.     Timer1.initialize(1000000); // 1 second in microseconds
  69.     Timer1.attachInterrupt(timerISR);
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // put your main code here, to run repeatedly:
  75.     updateOutputs(); // Refresh output data
  76. }
  77.  
  78. void updateOutputs()
  79. {
  80.     unsigned long currentTime = millis();
  81.  
  82.     // Check if it's time to turn on the LED
  83.     if (currentTime - lastLEDTime >= LED_INTERVAL)
  84.     {
  85.         LED_PIN_rawData = 1; // Turn on LED
  86.         lastLEDTime = currentTime; // Reset the timer
  87.     }
  88.  
  89.     // Check if it's time to turn off the LED
  90.     if (LED_PIN_rawData && (currentTime - lastLEDTime >= ON_DURATION))
  91.     {
  92.         LED_PIN_rawData = 0; // Turn off LED
  93.     }
  94.  
  95.     // Check if it's time to turn on the Buzzer
  96.     if (currentTime - lastBuzzerTime >= BUZZER_INTERVAL)
  97.     {
  98.         BUZZER_PIN_rawData = 1; // Turn on Buzzer
  99.         lastBuzzerTime = currentTime; // Reset the timer
  100.     }
  101.  
  102.     // Check if it's time to turn off the Buzzer
  103.     if (BUZZER_PIN_rawData && (currentTime - lastBuzzerTime >= ON_DURATION))
  104.     {
  105.         BUZZER_PIN_rawData = 0; // Turn off Buzzer
  106.     }
  107.  
  108.     // Check if it's time to turn on the Solenoid
  109.     if (currentTime - lastSolenoidTime >= SOLENOID_INTERVAL)
  110.     {
  111.         SOLENOID_PIN_rawData = 1; // Turn on Solenoid
  112.         lastSolenoidTime = currentTime; // Reset the timer
  113.     }
  114.  
  115.     // Check if it's time to turn off the Solenoid
  116.     if (SOLENOID_PIN_rawData && (currentTime - lastSolenoidTime >= ON_DURATION))
  117.     {
  118.         SOLENOID_PIN_rawData = 0; // Turn off Solenoid
  119.     }
  120.  
  121.     // Update the physical outputs
  122.     digitalWrite(LED_PIN, LED_PIN_rawData);
  123.     digitalWrite(BUZZER_PIN, BUZZER_PIN_rawData);
  124.     digitalWrite(SOLENOID_PIN, SOLENOID_PIN_rawData);
  125. }
  126.  
  127. void timerISR()
  128. {
  129.     // This function is called every 1 second by Timer1
  130.     // We can use it to handle any periodic tasks if needed
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement