Hi,
I am using a code to play two notes. I would like to slide from one note to the next being able to control the overall time it takes to get from one note to the next. (Like the ascending part of a siren).
int speakerPin = 8;
int length = 2; // the number of notes
char notes[] = "cb "; // a space represents a rest
int beats[] = { 5, 5 };
int tempo = 500;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 8) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'b'};
int tones[] = { 1915, 1014 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}