Go Down

Topic: Project 07 Mary had a little lamb (Read 189 times) previous topic - next topic

kaihin6

Oct 17, 2020, 11:49 pm Last Edit: Oct 17, 2020, 11:51 pm by kaihin6 Reason: Explicitly mention Project 07 Keyboard Instrument in body.
I am new to Arduino and enjoying the Starters Kit. I also have a kid, so I adjusted Project 07 Keyboard Instrument slightly so I could play "Mary had a little lamb".

I made two tweaks to the code, compared to the code in the book;
  • Replaced the note F (349Hz) with the note G (392Hz)
  • Print the note letter instead of value of the key


Here is the code if you want to play Mary had a little lamb:
Code: [Select]
int notes[] = {262,294,330,392}; //CDEG

void setup() {
  Serial.begin(9600);
}

void loop() {
  int keyValue = analogRead(A0);

  if(keyValue == 1023) {
    tone(8, notes[0]);
    Serial.println("C");
  }
  else if(keyValue >= 990 && keyValue <= 1010) {
    tone(8, notes[1]);
    Serial.println("D");
  }
  else if (keyValue >= 505 && keyValue <= 515) {
    tone(8, notes[2]);
    Serial.println("E");
  }
  else if(keyValue >= 5 && keyValue <= 10) {
    tone(8, notes[3]);
    Serial.println("G");
  }
  else{
    noTone(8);
  }
}

pert

Thanks for sharing your project @kaihin6!

Go Up