Hi, I am a beginner programmer with abysmal knowledge on the correct terminologies of arduino so excuse me for my cheap terms.
I am currently working on a piezo music element which plays a melody when the ping sensor detects ‘nothing’ within its vicinity(If an object is within 60 inches, it turns off). I just pasted the piezo music code in to the ping sensor code and fortunately it works . But the piezo only makes a ‘beep, beep beep’ sound continously without making any noticeable melody notes which wasn’t a surprise. This is the code(I think you can ignore the servo and led pins, they are extravaganza stuff)
#include <Servo.h>
Servo LFTleg;
Servo RGTleg;
Servo Tail;
Servo Frontpaw;
int pingPin = 7;
const int ledPin = 13;
int speakerPin = 3;
int length = 15;
char notes[] = "ccggaagffeeddc";
int beats [] = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1519, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
// play the tone corresponding to the note name
void setup()
{
Serial.begin(115200);
pinMode (ledPin, OUTPUT);
LFTleg.attach(9);
RGTleg.attach(10);
Tail.attach(11);
Frontpaw.attach(6);
pinMode(speakerPin, OUTPUT);
}
void loop()
{
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (inches <= 60) {
digitalWrite (speakerPin, LOW);
digitalWrite (ledPin, HIGH);
LFTleg.write(50);
RGTleg.write(50);
Tail.write(50);
Frontpaw.write(90);
}
else {
digitalWrite (speakerPin, HIGH);
for (int i = 0; i < length; i++)
playNote(notes[i], beats[i] * tempo);
delay(tempo / 2);
digitalWrite (ledPin, LOW);
LFTleg.write(90);
RGTleg.write(90);
Tail.write(90);
Frontpaw.write(70);
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 73.746 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Any suggestions from an experienced programmer? And can you tell me how and why the code is malfunctioning?
I want to know for further projects, thanks!