Pages: [1]   Go Down
Author Topic: Another Piano  (Read 1068 times)
0 Members and 1 Guest are viewing this topic.
MD, USA
Offline Offline
God Member
*****
Karma: 2
Posts: 663
A jack of all trades and a master of none!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
/*
  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.
« Last Edit: January 20, 2010, 03:07:30 pm by digimike » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've got the Tone.h in the sketch on another tab but the "#include <tone.h" is causing an error.

Do you have the Tone library installed in the right place?

Does the name in the include statement (case, in particular) match that of the actual header file?
Logged

MD, USA
Offline Offline
God Member
*****
Karma: 2
Posts: 663
A jack of all trades and a master of none!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
/*
  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);
    }
  }
}
  
« Last Edit: January 20, 2010, 04:39:45 pm by digimike » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have no sound,
Code:
#include <Tone.h>
Tone speakerpin;    
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], INPUT);}
 
}

void loop()
{
  // checks the state of each button
  for(int x=0; x<8; x++) {
    buttonset = digitalRead(button[x]);

    if(buttonset == HIGH && button[x] == 2){ // if button on pin 2 is pressed
      speakerpin.play(NOTE_C3, 20);    //play not from library for 1 ms
    }
    if(buttonset == HIGH && button[x] == 3) {
      speakerpin.play(NOTE_D3, 20);
    }
    if(buttonset == HIGH && button[x] == 4) {
      speakerpin.play(NOTE_E3, 20);
    }
    if(buttonset == HIGH && button[x] == 5) {
      speakerpin.play(NOTE_F3, 20);
    }
    if(buttonset == HIGH && button[x] == 6) {
      speakerpin.play(NOTE_G3, 20);
    }
    if(buttonset == HIGH && button[x] == 7) {
      speakerpin.play(NOTE_A3, 20);
    }
    if(buttonset == HIGH && button[x] == 8) {
      speakerpin.play(NOTE_B3, 20);
    }
    if(buttonset == HIGH && button[x] == 9) {
      speakerpin.play(NOTE_C4, 20);
    }
  }
}
  

whenever I use tone.h, no sound comes= =
did I write anything wrong"? :-[
Logged

MD, USA
Offline Offline
God Member
*****
Karma: 2
Posts: 663
A jack of all trades and a master of none!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do you have it wired up?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I used tone.h to create a poly-piano. but How can I transform square wave to sine wave with using tone.h?

Pots? PWM? or other ways?
Logged

Pages: [1]   Go Up
Jump to: