Hi everyone, having a bit of trouble with a tutorial I'm trying to follow for a university hand in.
The link for the tutorial is here Ultrasonic sensor (HC – SR04) and Piezo speakers measuring distance and playing a tone with Piezo – Péter Takács
Now I've got it wired up fine as I'm producing a noise, but it's producing constantly when fully wired up, and not repeating at a quicker or slower rate depending on distance, and I'm not too sure why.
The code is;
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
on 10 Nov 2012
Further modified by Peter Takacs on 04/02/04 https://patakacs.wordpress.com/2014/02/04/ultrasonic-sensor-hc-sr04-and-piezo-speakers-measuring-distance-and-playing-a-tone-with-piezo/
*/
int echoPin = 7;
int trigPin = 8;
int LEDPinYellow = 2; // Yellow LED
int LEDPinGreen = 4; // Green LED
int soundPin = 12; // Piezo
int maximumRange = 100;
int minimumRange = 0;
long duration, distance;
void setup() {
Serial.begin (9600); // Init communications to serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPinYellow, OUTPUT);
pinMode(LEDPinGreen, OUTPUT);
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){
// Yellow led indicated out of maximumRange. Prints “Out Of Range” to serial if target is outside maximumRange.
Serial.println("Out Of Range");
digitalWrite(LEDPinYellow, HIGH);
digitalWrite(LEDPinGreen, LOW);
}
else {
// When ultasonic sensor picks up a signal _within_ maximumRange, print distance in cm to serial monitor, turn off YellowLed and turn on GreenLED.
Serial.print("Distance = " );
Serial.print(distance);
Serial.println(" cm" );
digitalWrite(LEDPinYellow, LOW);
digitalWrite(LEDPinGreen, HIGH);
tone(soundPin, 800, 300);
delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones.
noTone(soundPin);
}
// Mandatory delay
delay(50);
}
Now I could use some help, hand in is in a couple of days, and I wasn't expecting this tutorial to be this.... finnicky? Pretty sure I've got it wired up right as it's making a noise. What's odd is that when I have the 'out of range' LED wired in, the noise doesn't stop, but WITHOUT that LED wired in, it stops when out of range as it's supposed to.
If anyone could point me in the right direction here it'd be much appreciated!