Third Eye
Third Eye
BY TEAM :
> NISARG P PATEL
> NISARG B PATEL
SOURCE CODE :
const int pingTrigPin = A4; //Trigger connected to PIN
const int pingEchoPin = A5; //Echo connected to PIN
int led=13; //Buzzer to PIN 4int buz1=9;void setup()
{Serial.begin(9600);pinMode(led, OUTPUT);
pinMode(buz1, OUTPUT); pinMode(pingTrigPin,
OUTPUT);pinMode(pingEchoPin, INPUT);}
void loop(){ long duration, cm;
digitalWrite(pingTrigPin, LOW);delayMicroseconds(2);
digitalWrite(pingTrigPin, HIGH);delayMicroseconds(5);
digitalWrite(pingTrigPin, LOW);duration =
pulseIn(pingEchoPin, HIGH);cm =
microsecondsToCentimeters(duration);
if(cm<=100 && cm>0){int d= map(cm, 1, 300, 10, 1000);
digitalWrite(led, HIGH);
digitalWrite(buz1, HIGH);delay(50);
digitalWrite(led, LOW);
digitalWrite(buz1,LOW);
delay(d); } Serial.print(cm);Serial.print("cm");
Serial.println();delay(40);}
long microsecondsToCentimeters(long microseconds)
CODE EXPLANATION :
This Arduino Nano code uses an ultrasonic sensor to measure distance and triggers an
LED and buzzer based on that distance.
•Setup:
•It defines pins for the ultrasonic sensor (trigger and echo), an LED, and a buzzer.
•It initializes serial communication for debugging.
•It sets the pin modes: trigger as output, echo as input, LED and buzzer as output.
•Loop:
•It sends a short pulse from the trigger pin to initiate the ultrasonic measurement.
•It measures the time it takes for the echo to return using pulseIn().
•It converts the echo time (duration) to centimeters using microsecondsToCentimeters().
•Distance Check:
•If the measured distance is between 1cm and 100cm:
•It calculates a delay value based on measured distance, closer object will cause faster blinking and beeping
•It turns on the LED and buzzer for 50 milliseconds.
•It turns off the LED and buzzer.
•It delays the time, based on the mapped distance.
•Serial Output:
•It prints the measured distance to the serial monitor.
•Delay:
•It adds a small delay to prevent rapid readings.
•microsecondsToCentimeters:
•This function converts the time taken for the sound wave to return into a distance in centimeters.
In essence, the code measures distance and gives a visual and audible warning (blinking LED and beeping buzzer) that
increases in frequency as an object gets closer.
THANK YOU