macca-nz

PIR and US trigger relays V1

Jan 23rd, 2022 (edited)
952
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const byte  ledPin = 13, pinBuzzer = 11, rPin =10, bPin = 8, pirPin =12;
  2. uint16_t    distance;
  3. bool        isHuman = false, update = false, armed = true,
  4.             ultaRead = false;
  5. uint8_t     eventCount = 0, rPinState = LOW, bPinState = LOW,
  6.             ledState = HIGH, blinkCount = 0;
  7. uint32_t prevTime = 0;
  8. #define DUTY 100
  9. #define MINUTE 600      //600 x DUTY = 60,000 or 1 minute
  10. #define SIXSEC 60       //60 x DUTY = 6000 (six seconds)
  11. #define THREE 3         //For LED blink @ 3 x DUTY or 300mS rate
  12.  
  13. void setup(){
  14.   pinMode(ledPin, OUTPUT);
  15.   pinMode(7, OUTPUT);           //TRIG
  16.   pinMode (6, INPUT);           //ECHO
  17.   pinMode(pinBuzzer, OUTPUT);
  18.   pinMode(rPin, OUTPUT);        //Define start state
  19.   pinMode(bPin, OUTPUT);
  20.   pinMode(pirPin, INPUT);
  21.   digitalWrite(rPin, rPinState);
  22.   digitalWrite(bPin, bPinState);
  23.   digitalWrite(ledPin, ledState);
  24. }
  25.  
  26. uint32_t duration(uint8_t triggerPin, uint8_t echoPin){
  27.     digitalWrite(triggerPin, LOW);
  28.     delayMicroseconds(2);
  29.     digitalWrite(triggerPin, HIGH);
  30.     delayMicroseconds(20);
  31.     digitalWrite(triggerPin, LOW);
  32.     return pulseIn(echoPin, HIGH);
  33. }
  34.  
  35. void loop(){
  36.  
  37.     if(armed){
  38.         isHuman = digitalRead(pirPin);
  39.             if (isHuman){
  40.                 ultaRead = true;
  41.             }
  42.     }
  43.  
  44.     if(millis() - prevTime >= DUTY){
  45.         prevTime = millis();
  46.             if(ultaRead){
  47.                 //Distance is in "mm" and it's rounded up/down to nearest "mm"
  48.                 distance = ((float)((0.01723 * duration(7, 6))* 10)+ 0.5);
  49.                     //Take a single distance read to see if object is 2000mm or closer
  50.                     if(distance <= 2000){  
  51.                         armed = false;        //Disarm sensors and enable events                  
  52.                         eventCount = 0;       //Zero event timer counter
  53.                         blinkCount = 0;       //Zero blink timer counter
  54.                         tone(pinBuzzer,293);  //Buzzer on
  55.                         rPinState = HIGH;     //Relay 1 on
  56.                         update = true;        //Update Outputs
  57.                     }
  58.                     ultaRead = false;         //Cancel UltraSonic read after each sample                  
  59.             }
  60.  
  61.             if(!armed){                       //Events enabled, Sensors disabled
  62.               eventCount++;                   //Plus 1 each duty cycle              
  63.               blinkCount++;
  64.                   if(blinkCount >= THREE){    //Blink LED every 300mS
  65.                       digitalWrite(ledPin, (ledState = !ledState));
  66.                       blinkCount = 0;
  67.                   }  
  68.                   if(eventCount >= MINUTE){   //60,000mS Arm sensors and LED ON solid
  69.                       armed = true;
  70.                       digitalWrite(ledPin, HIGH);
  71.                   }else if(eventCount == SIXSEC){   //6000mS Buzzer and 2 x relays are turned off
  72.                       noTone(pinBuzzer);
  73.                       bPinState = LOW;
  74.                       rPinState = LOW;
  75.                       update = true;
  76.                   }else if(eventCount == THREE){    //300mS delay for On state of bPin
  77.                       bPinState = HIGH;
  78.                       update = true;
  79.                   }
  80.             }
  81.            
  82.             if(update){                       //So we only write to the ouputs when needed
  83.                 digitalWrite(rPin, rPinState);
  84.                 digitalWrite(bPin, bPinState);
  85.                 update = false;
  86.             }
  87.     }
  88. }
RAW Paste Data Copied