Skip to content

Instantly share code, notes, and snippets.

@steverob
Last active January 1, 2016 10:49
Show Gist options
  • Save steverob/8133692 to your computer and use it in GitHub Desktop.
Save steverob/8133692 to your computer and use it in GitHub Desktop.
Play music using a Piezo sounder by pressing keys on the keyboard
int speakerPin = 12;
int input = 0;
int numNotes = 8;
int notes[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = {261, 294, 329, 349, 392, 440, 493, 523};
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
input = (char)Serial.read();
// say what you got:
Serial.print("Playing Note: ");
Serial.println(input);
for(int i = 0; i < numNotes; i++)
{
if(notes[i] == input){
tone(speakerPin, tones[i]);
delay(500);
noTone(speakerPin);
break;
}
}
}
}
@steverob
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment