Arduino ToF Radar System To protect your home from zombies!!!




Arduino Sketch

#include <Servo.h>
#include <Wire.h>
#include <VL53L0X.h>
#include <MedianFilter.h>

#define start_degree 15
#define limit_degree   165

MedianFilter test(10, 0);

Servo myServo;
VL53L0X sensor;
int speaker_pin = 5;

int degree = start_degree;
int incDegree = 1;
int target_near_distance = 200;


bool bTarget = false;

void setup(){
 
Serial.begin(115200);
Serial.println("Start!");
 Wire.begin();

  sensor.init();
  sensor.setTimeout(500);
   
  myServo.attach(7);
 
  myServo.write(degree);
  delay(5000);
 
  sensor.startContinuous();
}

void loop(){
 
     long distance;
 
    if( !bTarget){
      myServo.write( degree );
      delay(50);
      degree+= incDegree;
      if( degree > limit_degree ){
        incDegree = -1;
      }
      else if( degree < start_degree ){
        incDegree = 1;
      }
    }
   
     distance = sensor.readRangeContinuousMillimeters();
     test.in( distance );
     distance = test.out();
   
  if (sensor.timeoutOccurred()) {Serial.print(" TIMEOUT");
      delay(5000);
  }

  Serial.println(distance);

   bTarget = false;
   if( (distance <= target_near_distance) && (distance != 0) ){
     
        bTarget = true;
        warningSound();
   }else{
       noTone(speaker_pin);
   }

}
void warningSound()
{
 
    int i = 200; // The starting pitch
  while(i < 800) {
    i+=3;
    tone(speaker_pin, i); // Emit the noise
    delay(1);
  }
  delay(100);
 
}

ความคิดเห็น