I played around a bit with static tones found in the 'pitches.h' library, with a different tone being played for each range step (ie. when the led changed). Kinda cool, kinda boring...
So I decide to spice it up and made the pitch vary as a function of the distance. Much more rewarding. Much more annoying... The closer you are, the high it screams.
Taking it another step into sonic oblivion, I made the delay an inverse function of distance. Now the closer you get, the higher it screams, AND it screams more often. Wow.
Code:
int ledRed = 9; int ledGre = 6; int ledYel = 7; #define trigPin 10 #define echoPin 13 void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledRed, OUTPUT); pinMode(ledYel, OUTPUT); pinMode(ledGre, OUTPUT); } void loop() { float duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) * 0.0344; if (distance <=400) { tone(8, ((150 - distance) * 10)); } else { tone(8, 0); analogWrite(ledGre, LOW); analogWrite(ledYel, LOW); analogWrite(ledRed, LOW); } if (distance >= 90 && distance < 200){ analogWrite(ledGre, 350); } else { analogWrite(ledGre, LOW); } if (distance > 50 && distance < 90){ analogWrite(ledYel, 600); } else { analogWrite(ledYel, LOW); } if (distance <= 50){ analogWrite(ledRed, 350); } else { analogWrite(ledRed, LOW); } delay(distance * 3); }
Hardwares:
By default, pin D8 is audio out. I ended up throwing a pot in the positive line to limit the volume...
*Please note: These fritzing diagrams are intended to show only the proper connections, not necessarily the proper routing of components and wires... Bear this in mind if you are trying to replicate this build.*
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.