Hello; I'm a rank beginner. I have the Arduino Uno kit and was attempting to make a theremin/proximity sensor using a Sainsmart HC-SR04 ultrasonic sensor (bought on Amazon: Amazon.com) .
I've got the theremin part working fine: as I move my hand in front of the sensor, I get a range of tones in the A minor pentatonic scale...but when I remove my hand so the sensor cannot "see" it, the audio emits a very high squeal (about D7, I think). I want it to be silent when my hand is out of the calibrated range, not to squeal! I see others have asked about this problem, but most of the time they didn't receive an answer...and the few times I found answers, I don't know how to execute them yet.
A possible way to fix it: limit the range (eg: when objects are more than 3 or so feet away, be silent), which might be accomplished by silencing the device when the echo return is 3 milliseconds or more away. But I don't know how to do that. I've tried fiddling with various sections of the code and so far, no success.
If it's not a simple bit of code you can easily show me, perhaps you could direct me to a tutorial or give me some keywords or something to help get me on the right track.
If this is entirely the wrong section to post this question, please let me know which would be the correct section and I'll repost it there (and delete this post if possible), hopefully without incurring the wrath of the Admins ![]()
Thank you very much for any help you can give me!
Here's the code (taken from Arduino Project Hub: Arduino Theremin with A Minor Pentatonic):
/*
Arduino Theremin with A minor pentatonic scale
pollux labs, 2020
All rights reserved.
*/
const int trigger = 5;
const int echo = 4;
const int piezo = 10;
int distance = 0;
int distanceHigh = 0;
int lengthOfScale = 0;
int note = 0;
//A Minor pentatonic scale
int scale[] = {
147, 165, 196, 220, 262, 294, 330, 392, 440,
523, 587, 659, 784, 880, 1047, 1175, 1319, 1568,
1760, 2093, 2349
};
//C Major scale
//int scale[] = {
// 131, 147, 165, 175, 196, 220, 247, 262, 294,
// 330, 349, 392, 440, 494, 523, 587, 659, 698,
// 784, 880, 988, 1047
//};
void setup() {
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
while (millis() < 5000) {
digitalWrite(trigger, HIGH);
digitalWrite(trigger, LOW);
distance = pulseIn(echo, HIGH);
if (distance > distanceHigh) {
distanceHigh = distance;
}
}
for (byte i = 0; i < (sizeof(scale) / sizeof(scale[0])); i++) {
lengthOfScale += 1;
}
}
void loop() {
digitalWrite(trigger, HIGH);
digitalWrite(trigger, LOW);
distance = pulseIn(echo, HIGH);
note = map(distance, 250, distanceHigh, scale[0], scale[lengthOfScale - 1]);
for (byte j = 0; j < (lengthOfScale); j++) {
if (note == scale[j]) {
tone(piezo, note);
break;
}
else if (note > scale[j] && note < scale[j + 1]) {
note = scale[j];
tone(piezo, note);
break;
}
}
delay(30);
}
Here's what the hardware looks like:
Connect the cables for VCC and GND to your Arduino Uno with 5V and GND.
The cable from the echo pin on the sensor is connected to digital pin 4, Trig is connected to digital pin 5.First, connect the four wires to your ultrasonic sensor.
Connect the cables for VCC and GND to your Arduino Uno with 5V and GND.
Next, plug the piezo buzzer directly into the Arduino at the opposite pin header. Plug one leg into GND and the other into digital pin 10.
AAnd
hh
Arduino Theremin with A Minor Pentatonic

