I say its another Piano cause i’m sure many have made one like this. But i’m sure most of them have used the arduino tone library. This one doesn’t. I used the basic note wavelengths as found at this site. http://www.phy.mtu.edu/~suits/notefreqs.html
This piano can play from low C to middle C.
/*
Piano
Using 8 push buttons attached to pins 2 thou 9 and a speaker
attached to pin 13. Create a C minor piano. The button on pin
2 will play a low C and the button on pin 9 will play middle c.
The Circuit.
* push buttons attached to pins 2 thou 9 from ground
* speaker attached to pin 13 from ground
With this basic setup you can easily add 3 more notes. You can
also go in and change the frequency to get the notes you desire.
created 20 Jan 2010
by digimike
*/
boolean button[] = {2, 3, 4, 5, 6, 7, 8, 9}; //create array for button pins
boolean speakerpin = 13; //sets speaker pin
boolean buttonset = 0; //variable for reading button push
int tone = 0; //variable for setting musical note
void setup() {
// set all pins to output
pinMode(speakerpin, OUTPUT);
for(int x=0; x<8; x++) {
pinMode(button[x], OUTPUT);
}
// buttons are in the high position
for(int x=0; x<8; x++) {
digitalWrite(button[x], HIGH);
}
}
void loop()
{
// checks the state of each button
for(int x=0; x<8; x++) {
buttonset = digitalRead(button[x]);
if(buttonset == LOW && button[x] == 2){ // if button on pin 2 is pressed
tone = 2100; //stores the note's wavelength to be played.
}
if(buttonset == LOW && button[x] == 3) {
tone = 1870;
}
if(buttonset == LOW && button[x] == 4) {
tone = 1670;
}
if(buttonset == LOW && button[x] == 5) {
tone = 1580;
}
if(buttonset == LOW && button[x] == 6) {
tone = 1400;
}
if(buttonset == LOW && button[x] == 7) {
tone = 1250;
}
if(buttonset == LOW && button[x] == 8) {
tone = 1110;
}
if(buttonset == LOW && button[x] == 9) {
tone = 1050;
}
}
while(tone > 0) { // as long as there is a not to be played
digitalWrite(speakerpin, HIGH); // turn on 5V to speaker
delayMicroseconds(tone); // delay the designated wavelength in ms
digitalWrite(speakerpin, LOW); // turn off speaker
delayMicroseconds(tone); // delay the designated wavelength in ms
tone = 0; // resets tone variable to 0 so the tone dosn't play constantly
}
}
Everything should be self explanatory here. But if you need to see a diagram of the circuit setup i can throw one together on Fritzing. Next i’ll be trying to figure out how to get this working using the library. Right now i can’t get the sketch to use it. I’ve got the Tone.h in the sketch on another tab but the “#include <tone.h” is causing an error.
Yes i saw your post in response to another guy with the same problem a little while ago. So its not in the right place. I just need to learn how to use it now.
EDIT:
That didn’t take long to figure out.
Code using the Tone Library.
/*
Piano W/ Tone Library
created 20 Jan 2010
by digimike
*/
#include <Tone.h>
Tone speakerpin; // variable for speaker
boolean button[] = {2, 3, 4, 5, 6, 7, 8, 9}; //create array for button pins
boolean buttonset = 0; //variable for reading button push
void setup() {
speakerpin.begin(13); // speaker is on pin 13
// set all pins to output
for(int x=0; x<8; x++) {
pinMode(button[x], OUTPUT);
}
// buttons are in the high position
for(int x=0; x<8; x++) {
digitalWrite(button[x], HIGH);
}
}
void loop()
{
// checks the state of each button
for(int x=0; x<8; x++) {
buttonset = digitalRead(button[x]);
if(buttonset == LOW && button[x] == 2){ // if button on pin 2 is pressed
speakerpin.play(NOTE_C3, 20); //play not from library for 1 ms
}
if(buttonset == LOW && button[x] == 3) {
speakerpin.play(NOTE_D3, 20);
}
if(buttonset == LOW && button[x] == 4) {
speakerpin.play(NOTE_E3, 20);
}
if(buttonset == LOW && button[x] == 5) {
speakerpin.play(NOTE_F3, 20);
}
if(buttonset == LOW && button[x] == 6) {
speakerpin.play(NOTE_G3, 20);
}
if(buttonset == LOW && button[x] == 7) {
speakerpin.play(NOTE_A3, 20);
}
if(buttonset == LOW && button[x] == 8) {
speakerpin.play(NOTE_B3, 20);
}
if(buttonset == LOW && button[x] == 9) {
speakerpin.play(NOTE_C4, 20);
}
}
}