Last active
January 1, 2016 10:49
-
-
Save steverob/8133692 to your computer and use it in GitHub Desktop.
Play music using a Piezo sounder by pressing keys on the keyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Circuit Diagram
http://i.imgur.com/vxJ3lDI.png