This project uses an ultrasonic sensor to detect if an object is in front of the user and beeps to let them know how close the object is, faster beeping = objects are close and slower beeping = objects are far away. The entire system is embedded in a 3d printed enclosure that is shaped to look like glasses for a discrete appearance. An IR sensor is also placed in the glasses and points directly between the users eyebrows to sense if the user has put them on, if the user doesn't have the glasses on it turns off. The glasses also contain rechargeable batteries that last around 5 hours.
#include <HCSR04.h> // arduino library for ultrasonic sensor
#include <TimerOne.h>// arduino library for PWM functions
byte triggerPin = 12; // initializing pin 12 as the trigger pin of the ultrasonic sensor
byte echoPin = 13;// initializing pin 13 as the echo pin of the ultrasonic sensor
float t; // initializing variable that will store the frequency
int volume = 0; // initializing variable to store analog inputs of potentiometer that contols volume
int IR = 0; // initializing variable for IR sensor
int d = 0; // initializing variable to store duty cycle value
void setup () {
Serial.begin(9600); // serial monitor to check output
pinMode(9,OUTPUT); // setting buzzer pin as output
HCSR04.begin(triggerPin, echoPin); // add variables to function argument
}
void loop () {
double* distances = HCSR04.measureDistanceCm(); // storing the measurments into variable distances
IR = analogRead(A1); // reading IR sensor and storing in val
volume = analogRead(A2); // reading volume potentiometer value
delay(10); // 10ms delay
if (100 > distances[0] && IR<900){ // buzzer is only active if distance is less than 100cm & if the IR sensor's analog input is below 900, which indicates something is near
t = 1000000/((10 - (distances[0]/10) ) + 1) ; // sets the frequency of the buzzer, it changes with the distance, and ranges from 1Hz to 11Hz
d = map(volume,89,933,50,512); // mapping potentiometer values to fit in the desired range of the duty cycle (5% to 50%)
Timer1.initialize(t);// setting frequency
Timer1.pwm(9,d); // setting duty cycle
}
else{
Timer1.pwm(9,0);; // turning buzzer off when the distance is greater than 100
}
}