pleasedontcode

Arduino Alarm rev_02

Oct 9th, 2025
67
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: Arduino Alarm
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-09 13:26:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Check for any code adjustment required */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. // Minimal Arduino sketch for sensor monitoring with simple alarm management
  28.  
  29. /****** GLOBAL DEFINITIONS *****/
  30. const int NUM_SENSORS = 3;
  31. const int sensorPins[NUM_SENSORS] = {2, 3, 4}; // Digital input pins for sensors (use INPUT_PULLUP)
  32. const int BUZZER_PIN = 9;                     // Buzzer control pin
  33. const int LED_PIN = LED_BUILTIN;               // Built-in LED (usually pin 13)
  34.  
  35. bool sensorActive[NUM_SENSORS] = {false, false, false};
  36. unsigned long alarmEndTime = 0;
  37. bool alarmActive = false;
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setupSensorsAndIO();
  41. void processSensor(int sensorIndex, bool isActive, unsigned long currentTime);
  42. void requestAlarm(unsigned long duration, const char* reason);
  43. void triggerAlarm(unsigned long duration, const char* reason);
  44. void manageAlarm(unsigned long currentTime);
  45.  
  46. void setup(void);
  47. void loop(void);
  48.  
  49. /****** SETUP *****/
  50. void setupSensorsAndIO()
  51. {
  52.     // Initialize Serial for debugging
  53.     Serial.begin(9600);
  54.     while (!Serial) {
  55.         ; // wait for serial port to connect. Needed for some boards
  56.     }
  57.  
  58.     // Initialize sensor pins as input with pull-up resistors
  59.     for (int i = 0; i < NUM_SENSORS; i++) {
  60.         pinMode(sensorPins[i], INPUT_PULLUP);
  61.     }
  62.  
  63.     // Initialize actuators
  64.     pinMode(BUZZER_PIN, OUTPUT);
  65.     pinMode(LED_PIN, OUTPUT);
  66.    
  67.     // Ensure outputs start in OFF state
  68.     digitalWrite(BUZZER_PIN, LOW);
  69.     digitalWrite(LED_PIN, LOW);
  70.  
  71.     Serial.println("System initialized: sensors and IO configured");
  72. }
  73.  
  74. void setup(void)
  75. {
  76.     setupSensorsAndIO();
  77. }
  78.  
  79. /****** MAIN LOOP *****/
  80. void loop(void)
  81. {
  82.     unsigned long currentTime = millis();
  83.  
  84.     // Read all sensors and process state changes
  85.     for (int i = 0; i < NUM_SENSORS; i++) {
  86.         bool isActive = (digitalRead(sensorPins[i]) == LOW); // Active when LOW due to pull-up
  87.         processSensor(i, isActive, currentTime);
  88.     }
  89.  
  90.     // Manage ongoing alarm state
  91.     manageAlarm(currentTime);
  92. }
  93.  
  94. /****** FUNCTION IMPLEMENTATIONS *****/
  95. void processSensor(int sensorIndex, bool isActive, unsigned long currentTime)
  96. {
  97.     if (isActive) {
  98.         if (!sensorActive[sensorIndex]) {
  99.             sensorActive[sensorIndex] = true;
  100.             Serial.print("Sensor "); Serial.print(sensorIndex); Serial.println(" activated");
  101.             // If any sensor activates and no alarm is active, start an alarm
  102.             if (!alarmActive) {
  103.                 requestAlarm(10000, "Sensor activation"); // 10 seconds duration as example
  104.             }
  105.         }
  106.     } else {
  107.         sensorActive[sensorIndex] = false;
  108.     }
  109. }
  110.  
  111. void requestAlarm(unsigned long duration, const char* reason)
  112. {
  113.     if (!alarmActive) {
  114.         alarmActive = true;
  115.         alarmEndTime = millis() + duration;
  116.         digitalWrite(BUZZER_PIN, HIGH);
  117.         digitalWrite(LED_PIN, HIGH);
  118.         Serial.print("ALARM STARTED: "); Serial.println(reason);
  119.     } else {
  120.         // If an alarm is already active, extend duration to ensure visibility
  121.         alarmEndTime = millis() + duration;
  122.         Serial.println("ALARM EXTENDED");
  123.     }
  124. }
  125.  
  126. void triggerAlarm(unsigned long duration, const char* reason)
  127. {
  128.     // For this simple example, triggerAlarm delegates to requestAlarm
  129.     requestAlarm(duration, reason);
  130. }
  131.  
  132. void manageAlarm(unsigned long currentTime)
  133. {
  134.     if (alarmActive && currentTime >= alarmEndTime) {
  135.         alarmActive = false;
  136.         digitalWrite(BUZZER_PIN, LOW);
  137.         digitalWrite(LED_PIN, LOW);
  138.         Serial.println("ALARM ENDED");
  139.     }
  140. }
  141.  
  142. /* END CODE */
  143.  
Advertisement
Add Comment
Please, Sign In to add comment