i am trying to do a code to play a melody when the ultrasonic sensor detects something. this code is adapted.
i got my arduino a few days ago, and i dont know much about coding.
Code: [Select]
#include "pitches.h"
// constants won't change
const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int BUZZER_PIN = 3; // Arduino pin connected to Piezo Buzzer's pin
const int DISTANCE_THRESHOLD = 50; // centimeters
// variables will change:
float duration_us, distance_cm;
// notes in the melody:
int melody[] = {
REST, 2, NOTE_D4, 4,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
NOTE_G4, 2, NOTE_D5, 4,
NOTE_C5, -2,
NOTE_A4, -2,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
NOTE_F4, 2, NOTE_GS4, 4,
NOTE_D4, -1,
NOTE_D4, 4,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4, //10
NOTE_G4, 2, NOTE_D5, 4,
NOTE_F5, 2, NOTE_E5, 4,
NOTE_DS5, 2, NOTE_B4, 4,
NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_B4, 4,
NOTE_G4, -1,
NOTE_AS4, 4,
NOTE_D5, 2, NOTE_AS4, 4,//18
NOTE_D5, 2, NOTE_AS4, 4,
NOTE_DS5, 2, NOTE_D5, 4,
NOTE_CS5, 2, NOTE_A4, 4,
NOTE_AS4, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_D4, 4,
NOTE_D5, -1,
REST, 4, NOTE_AS4, 4,
NOTE_D5, 2, NOTE_AS4, 4,//26
NOTE_D5, 2, NOTE_AS4, 4,
NOTE_F5, 2, NOTE_E5, 4,
NOTE_DS5, 2, NOTE_B4, 4,
NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_AS4, 4,
NOTE_G4, -1,
};
// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations[] = {
};
2; 4; 4;
4; -4; 4; 8; 4; 4;
4; 2; 5; 4;
5; -2;
4; -2;
4; -4; 4; 8; 4; 4;
4; 2; 4; 4;
4; -1;
4; 4;
4; -4; 4; 8; 4; 4; 10;
4; 2; 5; 4;
5; 2; 5; 4;
5; 2; 4; 4;
5; -4; 5; 8; 5; 4;
4; 2; 4; 4;
4; -1;
4; 4;
5; 2; 4; 4; 18;
5; 2; 4; 4;
5; 2; 5; 4;
5; 2; 4; 4;
4; -4; 5; 8; 5; 4;
4; 2; 4; 4;
5; -1;
4; 4; 4;
5; 2; 4; 4; 26;
5; 2; 4; 4;
5; 2; 5; 4;
5; 2; 4; 4;
5; -4; 5; 8; 5; 4;
4; 2; 4; 4;
4; -1
};
void setup() {
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
if (distance_cm < DISTANCE_THRESHOLD)
buzzer(); // play a song
delay(500);
}
void buzzer() {
// iterate over the notes of the melody:
int size = sizeof(noteDurations) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(BUZZER_PIN, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(BUZZER_PIN);
}
}
it keeps showing the expected unqualified-id before numeric constant message on this line:
Code: [Select]
4; -1
what should i do?
does it have others mistakes?