Advertisement
learnelectronics

COB Security Light

Jul 31st, 2018
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1.  
  2.  
  3. #include <NewPing.h>
  4.  
  5. #define TRIGGER_PIN  2  
  6. #define ECHO_PIN     3
  7. #define ALERT_PIN    4
  8. #define MAX_DISTANCE 200
  9. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  10.  
  11.  
  12. int time_on = 2000;
  13.  
  14.  
  15.  
  16. void setup() {
  17.   Serial.begin(115200);
  18.   pinMode(ALERT_PIN,OUTPUT);
  19.   pinMode(13,OUTPUT);
  20.   digitalWrite(ALERT_PIN,LOW);
  21. }
  22.  
  23. void loop() {
  24.    int test = (sonar.ping_cm());
  25.    if (test < 50 && test >0){
  26.    digitalWrite(ALERT_PIN,HIGH);
  27.    digitalWrite(13,HIGH);
  28.     delay(time_on);
  29.     digitalWrite(ALERT_PIN,LOW);
  30.     digitalWrite(13,LOW);
  31.    }
  32.  
  33.  
  34.   delay(100);      
  35.   Serial.print("Ping: ");
  36.   Serial.print(test);
  37.   Serial.println("cm");
  38.  
  39.  
  40.  
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement