Multiple LEDs with piezo buzzer ISO scoping error

The goal of my code is to play a certain note with an led. For example, if D, as an example note plays, then led 6 would light up. My code is attached below, along with the error that comes up. I can not figure out how to fix the code. If anybody knows how to get the code to work, please let me know! Thank you.

Screen Shot 2016-05-09 at 2.44.47 PM.png

jesse_eva_piezoelements8leds.ino (1.85 KB)

(deleted)

Style Hint: You can use a true/false value in place of a HIGH/LOW value. Using that trick you can simplify all your note name comparisons down to:

    digitalWrite(led1, note == 'c');
    digitalWrite(led2, note == 'g');
    digitalWrite(led3, note == 'a');
    digitalWrite(led4, note == 'f');
    digitalWrite(led5, note == 'e');
    digitalWrite(led6, note == 'd');

The goal of my code is to play a certain note with an led.

In what universe? LEDs are really, really quiet. It's hard to hear any "notes" they play.

The piezo buzzer is supposed to play the notes.

This should do what you wanted:

const int speakerPin = 10;


const int length = 15;  // the number of notes
const char notes[] = "ccggaagffeeddc "; // a space represents a rest
const int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
const int tempo = 300;
const int led1 = 0;
const int led2 = 1;
const int led3 = 2;
const int led4 = 3;
const int led5 = 4;
const int led6 = 5;

void setup() {
  pinMode(speakerPin, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);

}

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);
  }
}

const char names[] = "cdefgabC";
const int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

void playNote(char note, int duration) {
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      digitalWrite(led1, note == 'c');
      digitalWrite(led2, note == 'g');
      digitalWrite(led3, note == 'a');
      digitalWrite(led4, note == 'f');
      digitalWrite(led5, note == 'e');
      digitalWrite(led6, note == 'd');
      playTone(tones[i], duration);
      break;  // No need to look at the rest of the names
    }
  }
}

void loop() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }
    delay(tempo / 2);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
  }
  delay(1000); // pause between repeats
}