Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Arduino Alarm
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-10-09 13:26:23
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Check for any code adjustment required */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // Minimal Arduino sketch for sensor monitoring with simple alarm management
- /****** GLOBAL DEFINITIONS *****/
- const int NUM_SENSORS = 3;
- const int sensorPins[NUM_SENSORS] = {2, 3, 4}; // Digital input pins for sensors (use INPUT_PULLUP)
- const int BUZZER_PIN = 9; // Buzzer control pin
- const int LED_PIN = LED_BUILTIN; // Built-in LED (usually pin 13)
- bool sensorActive[NUM_SENSORS] = {false, false, false};
- unsigned long alarmEndTime = 0;
- bool alarmActive = false;
- /****** FUNCTION PROTOTYPES *****/
- void setupSensorsAndIO();
- void processSensor(int sensorIndex, bool isActive, unsigned long currentTime);
- void requestAlarm(unsigned long duration, const char* reason);
- void triggerAlarm(unsigned long duration, const char* reason);
- void manageAlarm(unsigned long currentTime);
- void setup(void);
- void loop(void);
- /****** SETUP *****/
- void setupSensorsAndIO()
- {
- // Initialize Serial for debugging
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for some boards
- }
- // Initialize sensor pins as input with pull-up resistors
- for (int i = 0; i < NUM_SENSORS; i++) {
- pinMode(sensorPins[i], INPUT_PULLUP);
- }
- // Initialize actuators
- pinMode(BUZZER_PIN, OUTPUT);
- pinMode(LED_PIN, OUTPUT);
- // Ensure outputs start in OFF state
- digitalWrite(BUZZER_PIN, LOW);
- digitalWrite(LED_PIN, LOW);
- Serial.println("System initialized: sensors and IO configured");
- }
- void setup(void)
- {
- setupSensorsAndIO();
- }
- /****** MAIN LOOP *****/
- void loop(void)
- {
- unsigned long currentTime = millis();
- // Read all sensors and process state changes
- for (int i = 0; i < NUM_SENSORS; i++) {
- bool isActive = (digitalRead(sensorPins[i]) == LOW); // Active when LOW due to pull-up
- processSensor(i, isActive, currentTime);
- }
- // Manage ongoing alarm state
- manageAlarm(currentTime);
- }
- /****** FUNCTION IMPLEMENTATIONS *****/
- void processSensor(int sensorIndex, bool isActive, unsigned long currentTime)
- {
- if (isActive) {
- if (!sensorActive[sensorIndex]) {
- sensorActive[sensorIndex] = true;
- Serial.print("Sensor "); Serial.print(sensorIndex); Serial.println(" activated");
- // If any sensor activates and no alarm is active, start an alarm
- if (!alarmActive) {
- requestAlarm(10000, "Sensor activation"); // 10 seconds duration as example
- }
- }
- } else {
- sensorActive[sensorIndex] = false;
- }
- }
- void requestAlarm(unsigned long duration, const char* reason)
- {
- if (!alarmActive) {
- alarmActive = true;
- alarmEndTime = millis() + duration;
- digitalWrite(BUZZER_PIN, HIGH);
- digitalWrite(LED_PIN, HIGH);
- Serial.print("ALARM STARTED: "); Serial.println(reason);
- } else {
- // If an alarm is already active, extend duration to ensure visibility
- alarmEndTime = millis() + duration;
- Serial.println("ALARM EXTENDED");
- }
- }
- void triggerAlarm(unsigned long duration, const char* reason)
- {
- // For this simple example, triggerAlarm delegates to requestAlarm
- requestAlarm(duration, reason);
- }
- void manageAlarm(unsigned long currentTime)
- {
- if (alarmActive && currentTime >= alarmEndTime) {
- alarmActive = false;
- digitalWrite(BUZZER_PIN, LOW);
- digitalWrite(LED_PIN, LOW);
- Serial.println("ALARM ENDED");
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment