This Machine uses Sonar to sense if an object is near the sensor, at which time it alerts the viewer by rapidly blinking LEDs. If the object is far away, the LEDs blink at a steady pace.


// ref for sensor reading: www.HowToMechatronics.com


// defines pins numbers

const int trigPin = 9;
const int echoPin = 10;
const int led1 = 4;
const int led2 = 5; 
const int led3 = 6;


// defines variables
long duration;
int distance;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
  
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

// Checks if the object is close
if (distance > 15)
{
  digitalWrite(led1,HIGH);
  delay(500);
  digitalWrite(led1,LOW);
  delay(500);
  digitalWrite(led2,HIGH);
  delay(500);
  digitalWrite(led2,LOW);
  delay(500);
  digitalWrite(led3,HIGH);
  delay(500);
  digitalWrite(led3,LOW);
  delay(500);  
}

// Checks if the object is far
if (distance < 15)
{
  digitalWrite(led1,HIGH);
  delay(50);
  digitalWrite(led1,LOW);
  delay(50);
  digitalWrite(led2,HIGH);
  delay(50);
  digitalWrite(led2,LOW);
  delay(50);
  digitalWrite(led3,HIGH);
  delay(50);
  digitalWrite(led3,LOW);
  delay(50);  
}

}