0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« on: August 15, 2009, 08:50:18 pm » |
I am trying to run this code:
int pingPin = 7; int speakerPin = 9;
int length = 100; // the number of notes char notes[] = "CgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCg"; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}; 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[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names == note) { playTone(tones, duration); } } } void setup(){ pinMode(speakerPin, OUTPUT); }
void loop() { long duration, inches, i;
// 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); if (inches <= 10) i == 1
;for (int i; i < length; i) { if (notes == ' ') { delay(beats * tempo); // rest } else { playNote(notes, beats * tempo); } // pause between notes delay(tempo / 10);
}
long microsecondsToInches (long microseconds) [glow]{[/glow]
// 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/2801 [glow][/glow]PINGv1.3.pdf return microseconds / 74 / 2; }
and this error:
In function 'void loop()': error: a function-definition is not allowed here before '{' token
comes up. Can someone please tell me why?
|