kculm:
...
Also when it plays, it looks like it just lighing the lights in order pin 2-9. I looks ok. but what I was going for was a not assigned to an LED
...
Yes, the "turn on" and "turn off" statements were in the wrong place (and looking at the wrong data). Notice in the below that I moved them to the playNote() function:
int speakerPin = 11;
char notes[] = "ccggaagffeeddcggffeedggffeedccggaagffeeddc "; // a space represents a rest
int length = sizeof(notes)/sizeof(notes[0]); // the number of notes
int beats[] = { 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2,4,};
int tempo = 1000; // make this smaller to speed the whole thing up, bigger to slow down
int ledPins[] = { 2,3,4,5, 6,7,8,9};
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[i] == note) {
Serial.println( String(" Note: ") + names[i] + " dur: " + duration + " ledPin: " + ledPins[i]);
digitalWrite(ledPins[i], HIGH); // turn on the right LED <<********************************
playTone(tones[i], duration); // play the note
digitalWrite(ledPins[i], LOW); // turn off the LED
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
for (int i=0;i<8; i++)
pinMode(ledPins[i],OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println("Start");
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
}
else {
playNote(notes[i], beats[i] * tempo);
delay (100); // make this smaller (or even delete the line) to have less time between the notes, bigger for more
}
}
}