I'm hacking the melody example from here: http://arduino.cc/en/Tutorial/Tone
I am trying to play several different melodies so I want to pass them to a function to play. The arrays are showing up with len = 2 inside the playTune function. I'm a javascript programmer so I'm sure this is a C++ syntax issue, especially since I don't understand how you can have an int array full of char data.
Thanks in advance for sharing your wisdom.
#include "pitches.h"
int speakerPin = 12;
int baseSpeed = 600;int horserace_notes[] = {
C4,F4,A4,C5,C5,C5,C5,0,A4,A4,A4,A4,0,F4,A4,F4,C4};
int horserace_beats[] = {
4, 4, 4, 4, 8, 8, 8, 8, 4, 8,8, 8, 8, 4, 4, 4, 2 };void setup() {
Serial.begin(9600);playTune(horserace_notes,horserace_beats);
}void loop() {
// no need to repeat the melody.
}void playTune(int notes[], int beats[]) {
int len = sizeof(beats)/ sizeof(int);
Serial.print(sizeof(beats));// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < len; 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 = baseSpeed/beats[thisNote];
tone(12, notes[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);
}
}