Facebook
From alu, 3 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 308
  1. const int motionSensor = 2; // Motion sensor pin
  2. const int relayPin = 3;     // Digital pin connected to the relay module
  3. const int pumpOnTime = 1000; // Pump on time in milliseconds (1 second)
  4.  
  5. unsigned long pumpStartTime = 0; // Variable to store the pump start time
  6. bool pumpIsRunning = false;      // Flag to indicate if the pump is running
  7.  
  8. void setup() {
  9.   pinMode(motionSensor, INPUT);
  10.   pinMode(relayPin, OUTPUT);
  11. }
  12.  
  13. void loop() {
  14.   int motionDetected = digitalRead(motionSensor);
  15.  
  16.   if (motionDetected == HIGH && !pumpIsRunning) {
  17.     // Motion detected and pump is not already running
  18.     pumpStartTime = millis();
  19.     digitalWrite(relayPin, HIGH); // Turn on the relay
  20.     pumpIsRunning = true;
  21.   }
  22.  
  23.   if (pumpIsRunning && millis() - pumpStartTime >= pumpOnTime) {
  24.     digitalWrite(relayPin, LOW); // Turn off the relay
  25.     pumpIsRunning = false; // Reset the pump running flag
  26.   }
  27. }
  28.