For my first Arduino-based assignment I wanted to incorporate the use of an ultrasonic sensor to generate an interaction for the user based on their presence alone. My initial idea was to have the output in the form of a message displayed on an LCD screen – I even wired up the circuit for this idea as shown below. However I was unable to figure out how to link information from the sensor to the LCD screen and decided to try a new approach.
In an attempt to simplify things for myself while maintaining the idea of detecting someone’s/something’s presence, I decided to create a ‘personal distance machine’ which tells users when something is too close for comfort. To do this I replaced the LCD screen with a buzzer and an RGB light which changes incrementally from green to yellow to red depending on the distance detected by the sensor. Below is an excerpt from the code as well as an example video.
void loop() { distance = getDistance(); //variable to store the distance measured by the sensor Serial.print(distance); //print the distance that was measured Serial.println(" in"); //print units after the distance if (distance <= 20) { //if the object is close //make the RGB LED red analogWrite(redPin, 255); analogWrite(greenPin, 0); analogWrite(bluePin, 0); tone(buzzerPin, 272); } else if (20 < distance && distance < 40) { //if the object is a medium distance //make the RGB LED yellow analogWrite(redPin, 255); analogWrite(greenPin, 50); analogWrite(bluePin, 0); } else { //if the object is far away //make the RGB LED green analogWrite(redPin, 0); analogWrite(greenPin, 255); analogWrite(bluePin, 0); noTone(buzzerPin); }