//Julian Torres //define pins and variables for math involved int trigger = 2, echo = 3, redLED = 13, blueLED = 12, greenLED = 11, buzzer = 7, duration, //calc the time taken distance; //how far the object has to be to trigger a pin //define the input and output and initalizes the serial monitor for debugging. void setup() { pinMode(trigger,OUTPUT); pinMode(echo, INPUT); pinMode(redLED, OUTPUT); pinMode(blueLED, OUTPUT); pinMode(greenLED, OUTPUT); Serial.begin(9600); } void loop() { //set up trigger and make sure it is working properly. digitalWrite(trigger, LOW); delayMicroseconds(2); digitalWrite(trigger, HIGH); delayMicroseconds(5); digitalWrite(trigger, LOW); //math involved to calculate distance and time taken. duration = pulseIn(echo, HIGH); distance = (duration/2) / 29.1; //if the distance is greater than 50 the green led is on and serial monitor prints normal message if(distance > 50) { digitalWrite(greenLED, HIGH); digitalWrite(blueLED, LOW); digitalWrite(redLED, LOW); Serial.println("This is a safe distance. Stay there!"); noTone(buzzer); } //if distance is less than 50 and greater than 20 the blue led is on and serial monitor prints a warning else if(distance > 20) { digitalWrite(greenLED, LOW); digitalWrite(blueLED, HIGH); digitalWrite(redLED, LOW); Serial.println("DO NOT COME ANY CLOSER. I'M WARNING YOU!"); noTone(buzzer); } //if distance is less than 20 the red led is and serial monitor prints security breach else { digitalWrite(greenLED, LOW); digitalWrite(blueLED, LOW); digitalWrite(redLED, HIGH); Serial.println("SECURITY BREACH! ALARM SYSTEM ACTIVATED"); tone(buzzer, 100); } //delay between each print message on monitor delay(500); }