Go Down

Topic: polyphonic piano with arduino?? (Read 10801 times) previous topic - next topic

falohao

you said byte switchState[3]; is store the 3 tones by using 8 bits.
When I add  

switch (buttonset);
byte switchState[3];

at the bottom of loop, bow can I get back the tones 8-) ?
Then output them separately to 3 ch. .

Sorry I want to ask you more. Does this code output square wave or others? I think it can't be polyphonic.

PaulS

Quote
you said byte switchState[3]; is store the 3 tones by using 8 bits.


No. I said that the switchState array is to store the state of the switches.

If you want to note whether more than one button is held down, you need an array of tones.

Code: [Select]
int tones[3];

In the loop, set tones
  • to 0. Then check the now/then status, and, if a tone is to be played, set tones
  • to that value.

    You don't need a while loop to play the tone. A simple if is sufficient. If you want to play more than one, you'd use a for loop to cycles through the tones array, and digitalWrite the appropriate pin HIGH. Then, delay, then digitalWrite all the pins LOW.
The art of getting good answers lies in asking good questions.

falohao

:-/
tone = 0;
for(int x=0; x<8; x++)
{
  buttonset = digitalRead(button
  • );

      if(buttonState
  • == buttonset)

    Should I int or byte buttonState first?

falohao

Code: [Select]


/*
 Simple Polyphonic Piano

 Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
 attached to pins 11 thou 13.   Use  LED attached to pin 10.
 
The Circuit.
   * push buttons attached to pins 2 thou 9 from ground
   * speakers attached to pin 11,12 and 13 from ground
   * LED attached to pin 10 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 3 Mar 2010
 by Silver

*/
#include <Tone.h>

//define notes
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330

int button[] = {2, 3, 4}; //button pins array
//set button states array
byte speakerpin1 = 11; //speaker pin
byte speakerpin2 = 12;
byte speakerpin3 = 13;

byte buttonset = HIGH;  //set button state high
byte switchState[3]; //an array for storing 3 button states


//variable for setting musical note
int led = 10; //LED pin


void setup() {
 // set all pins to output
 pinMode(speakerpin1, OUTPUT);  
 pinMode(speakerpin2, OUTPUT);
 pinMode(speakerpin3, OUTPUT);
 pinMode(led, OUTPUT);
 for(int x=0; x<3; x++) {
   pinMode(button[x], OUTPUT);
 }
 // buttons are in the high position
 for(int x=0; x<3; x++) {
   digitalWrite(button[x], HIGH);
 }
}

void loop()
{  
int tones[3]; //an array of tones
byte buttonState[] = {HIGH, HIGH, HIGH};   //set button states array
 
for(int x=0; x<3; x++)  // checks the state of each button

{
  buttonset = digitalRead(button[x]);  //put now state into buttonset

  if(buttonState[x] == buttonset)  //compare
          {
          if(x == 0)  //which key held down
          {
           tones[0] = NOTE_C4;  //store the tone in array
               switchState[0] = LOW; //store the state in array
        }
        if(x == 1)
        {
           tones[1] = NOTE_D4;
               switchState[1] = LOW;
        }
        if(x == 2)
        {
           tones[2] = NOTE_E4;
               switchState[2] = LOW;
        }
     }
  }
  for(int tones[3] = 0; tones[3] < 3; tones[3]++){
  if (tones[0] > 0){
    digitalWrite(speakerpin1, HIGH);
    delayMicroseconds(tones[0]);
    digitalWrite(speakerpin1, LOW);
    delayMicroseconds(tones[0]);}
  if (tones[1] > 0){
    digitalWrite(speakerpin2, HIGH);
    delayMicroseconds(tones[1]);
    digitalWrite(speakerpin2, LOW);
    delayMicroseconds(tones[1]);}
  if (tones[1] > 0){
    digitalWrite(speakerpin3, HIGH);
    delayMicroseconds(tones[2]);
    digitalWrite(speakerpin3, LOW);
    delayMicroseconds(tones[2]);}  
buttonState[x] = buttonset;
tones[] = [0, 0, 0];
}
 
}

}


Dr. Pauls,

i revised the code. the code can be compiled but doesn't work.
I tried to store tones and switch states, is that right?
I added for loop to cycles tones array.
Can you point out some false to me?

Actually I don't know how to use array function and store something into it. Also I don't know how to loop an array....
but I tried .

PaulS

Quote
the code can be compiled but doesn't work.


Well, I doubted that, so I copied and pasted it into a new sketch, and it doesn't compile. There are several errors - some with syntax, some with logic.

Logic issues, first.

Code: [Select]
           buttonset = digitalRead(button[x]);  //put now state into buttonset
           if(buttonState[x] == buttonset)  //compare
         {


Here you are reading the state of a switch on the pin whose number is stored in the xth position of the button array. Then, you compare that with the state that is stored in the xth position of the buttonState array. OK. But, doesn't it matter if the button is pressed, or not. This was, you are setting up to play a note is the button is pressed, and was pressed last time, or if the button is not pressed and was not pressed last time.

You need an outer if to test if the button is pressed.

Code: [Select]
     byte buttonState[] = {HIGH, HIGH, HIGH};   //set button states array


Declaring an array, and initializing it is good. Doing it in loop isn't. Every time loop is called, the array will be re-initialized. Oops, there goes the persistence you were trying to achieve. Move this to the top of the sketch (before setup).

Code: [Select]
                       switchState[0] = LOW; //store the state in array


Why? You never reference switchState again.

I used switchState in all me code, because the thing being pressed isn't a button. It's a switch. Buttons hold shirts closed. Switches pass, or not, trains and electric current. Now, I know that this is simply a matter of semantics, but if I suggest that you create an array called switchState, and you prefer to call it buttonState, that's fine. As long as you change ALL references to switchState to buttonState.

The xth position of switchState (or buttonState, if you prefer) needs to be set to buttonset at the end of the for loop, regardless of what value buttonset has or what value switchState
  • has. Otherwise, on the next pass through loop, we can't compare buttonset against the previous value, because we might not have stored the previous value.

    Now, for the syntax errors.

    Code: [Select]
         for(int tones[3] = 0; tones[3] < 3; tones[3]++)


    The first part of the for loop statement is an initialization statement. A variable, tones[3] is declared, and initialized to 0. Well, tones[3] is not a valid variable name. It is a valid name for an array that can hold three values, but that is not what you want here. You want:

    Code: [Select]
      for(int t = 0; t < 3; t++)

    Next, you have three blocks of code that loop like this:
    Code: [Select]
               if (tones[0] > 0)
               {
                     digitalWrite(speakerpin1, HIGH);
                     delayMicroseconds(tones[0]);
                     digitalWrite(speakerpin1, LOW);
                     delayMicroseconds(tones[0]);
               }


    Suppose that the earlier for loop was correct, and that I had been holding two switches down - the 1st and third ones. At the end of the first for loop, tones[0] = NOTE_C4, tones[1] = 0, and tones[2] = NOTE_E4.

    Now, you are going to make three passes through this loop. On the first pass, you are going to play notes NOTE_C4 and NOTE_E4, because tones=[0] and tones[2] are not 0. On the second pass, you are going to play the same notes again, because the values in tones haven't changed. On the third pass, you are going to play the same two notes again.

    You want one block of code, with t as the array index:
    Code: [Select]
               if (tones[t] > 0)
               {
                     digitalWrite(speakerpin1, HIGH);
                     delayMicroseconds(tones[t]);
                     digitalWrite(speakerpin1, LOW);
                     delayMicroseconds(tones[t]);
               }


    Now this presents a problem, because all three notes are going to be played through the same speaker. To solve this, the speaker pins could be put in an array, and references in this for loop. The problem with that approach is that it would play the first note, then the second note, then the third note, if all three switches are held down.

    If you want all three notes to play at the same time, you need to keep the code that you have in the for loop, but get rid of the loop. This is still going to play one note, then the second note, then the third note, because you haven't coded them to play simultaneously.

    If they are to play simultaneously, you will need to get rid of the delay function calls. The blink without delay example shows how to do it. But, before you go there, consider that you are including the header file for the tone library, but you are not using it. Look at the examples on the Tone library page for how you should be playing the tones.

    Finally, you have this code:
    Code: [Select]
               buttonState[x] = buttonset;
               tones[] = [0, 0, 0];


    The first statement belongs in the first loop, and corrects one of the problems I mentioned above.

    The second is an attempt, I think, to reset the tones array to all 0's. There are two problems with this. First, you can not set all the values of an array in a single statement. You need to use:

    Code: [Select]
    tones[0] = 0;
    tones[1] = 0;
    tones[2] = 0;


    Second, you do not want to be resetting tones inside the loop that is trying to play the tones. The elements of the tones array should be set at the top of the first for loop:

    Code: [Select]
    tones[x] = 0;

    This assures that at the end of the first for loop, the tones array contains a 0 or a frequency.

    Considering how the tone library works, though, I think you are going about this all wrong. In loop, you need simply look at whether button
  • is pressed, or not. If it is, and the note is playing, leave it playing. If the note is not playing start it playing. If button
  • is not pressed, stop playing the note.
The art of getting good answers lies in asking good questions.

falohao

#20
Mar 03, 2010, 04:32 pm Last Edit: Mar 03, 2010, 04:41 pm by falohao Reason: 1
CHANGED code...by following problems you pointed out..!!
sounds come!!!
But unfortunately...
Note_C4 keeps on plying= = :o
whenever I press 2nd switch, it output higher note,
whenever I press 3rd switch, it output higher note.
Code: [Select]
/*
 Simple Polyphonic Piano

 Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
 attached to pins 11 thou 13.   Use  LED attached to pin 10.
 
The Circuit.
   * push buttons attached to pins 2 thou 9 from ground
   * speakers attached to pin 11,12 and 13 from ground
   * LED attached to pin 10 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 3 Mar 2010
 by Silver

*/
#include <Tone.h>

//define notes
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330

int button[] = {2, 3, 4}; //button pins array
int led = 10; //LED pin

byte speakerpin[3] = {11,12,13}; //array of speaker pin
byte buttonset = HIGH;  //set button state high
byte buttonState[3] = {HIGH, HIGH, HIGH};   //set button states array//an array for storing 3 button states

void setup() {
 // set all pins to output
 pinMode(speakerpin[0], OUTPUT);  
   pinMode(speakerpin[1], OUTPUT);
     pinMode(speakerpin[2], OUTPUT);
 pinMode(led, OUTPUT);
 for(int x=0; x<3; x++) {
   pinMode(button[x], OUTPUT);
 }
 // buttons are in the high position
 for(int x=0; x<3; x++) {
   digitalWrite(button[x], HIGH);
 }
}

void loop()
{  
int tones[3];
tones[0] = 0;
tones[1] = 0;
tones[2] = 0;
int buttonState[3];
buttonState[1] = HIGH;
buttonState[2] = HIGH;
buttonState[3] = HIGH;
 
for(int x=0; x<3; x++)  // checks the state of each button

{
  buttonset = digitalRead(button[x]);  //put now state into buttonset

  if(buttonState[x] == buttonset)  //compare
          {
          if(x == 0)  //which key held down
          {
           tones[0] = NOTE_C4;  //store the tone in array
               buttonState[0] = LOW; //store the state in array
        }
        if(x == 1)
        {
           tones[1] = NOTE_D4;
               buttonState[1] = LOW;
        }
        if(x == 2)
        {
           tones[2] = NOTE_E4;
               buttonState[2] = LOW;
        }
     }
  }
  for(int t = 0; t < 3; t++) {
  if (tones[t] > 0)
           {
       digitalWrite(speakerpin[0], HIGH);
     delayMicroseconds(tones[t]);
     digitalWrite(speakerpin[0], LOW);
     delayMicroseconds(tones[t]);
       
       digitalWrite(led, HIGH);
       delayMicroseconds(tones[t]);
       digitalWrite(led, LOW);
     delayMicroseconds(tones[t]);
           }

}
 
}


I declare speaker array first, but it's useless now..
Change all switchstates to buttonstates...
byte buttonstate[3] =XXXXXXXXXX is moved to header...
tones[1],[2],[3] set to 0 before first for loop...
buttonstate[1][2][3] set to high before 1st for loop...

I now want to output likes you said
Quote
play the first note, then the second note, then the third note, if all three switches are held down.


falohao

Do this first,
Quote
play the first note, then the second note, then the third note, if all three switches are held down.


After that, fix polyphony problems= = :'(

i read the code again & again...
Code: [Select]
buttonset = digitalRead(button[x]);  //put now state into buttonset

  if(buttonState[x] == buttonset)  //compare
          {
          if(x == 0)  //which key held down
          {
           tones[0] = NOTE_C4;  //store the tone in array
               buttonState[0] = LOW; //store the state in array
        }
        if(x == 1)
        {
           tones[1] = NOTE_D4;
               buttonState[1] = LOW;
        }
        if(x == 2)
        {
           tones[2] = NOTE_E4;
               buttonState[2] = LOW;


if no button held down, buttonset is equals to high,
this will have no sound. I can't clearly describe what I think.
But I think this part may have some problems I don't know.
I just copied this part in earlier reply you posted. :-[

PaulS

You need to post all of your code, again. But the problem is that you are not checking to see if the switch is pressed. You are only looking to see if the switch is in the same state it was last time, but you are not setting that correctly, either.

You are also not resetting tones
  • .

    Think about the variables involved, for the first button.

    buttonState[0] is set to HIGH.
    loop starts.
    buttonset is determined to be HIGH (the button is not pushed).
    buttonset is compared to buttonState[0]. [glow]They match, so tones[0] is set to NOTE_C4[/glow] and buttonState[0] is set to LOW.
    the first for loop ends, and the second begins.
    the note is played, because tones[0] is not 0.
    loop ends.

    loop starts.
    buttonset is read. Suppose it is still HIGH (not yet pressed).
    buttonset is compared to buttonState[0]. [glow]They do not match, so tones[0] and buttonState[0] remain unchanged.[/glow]
    the first for loop ends, and the second begins.
    the note is played, because tones[0] is not 0.
    loop ends.

    loop starts.
    buttonset is read. Suppose it is now LOW (pressed).
    buttonset is compared to buttonState[0]. They match, so tones[0] and buttonState[0] are set to NOTE_C4 and LOW.
    the first for loop ends, and the second begins.
    the note is played, because tones[0] is not 0.
    loop ends.

    The first highlighted problem would not be happening if you checked whether the switch was pressed, before seeing if it's state matches the last state.

    The second highlighted problem would not occur if you properly initialized tones in the first for loop (add tones
  • = 0; before the bottonset = analogRead line).
The art of getting good answers lies in asking good questions.

falohao

I'm fixing the highlighted problems.
First, can write this in reset tones
  • = 0????
    Code: [Select]
    tones[0] = 0;
    tones[1] = 0;
    tone[2] = 0;


    because x is not declared in code.

falohao

#24
Mar 04, 2010, 08:23 am Last Edit: Mar 04, 2010, 11:52 am by falohao Reason: 1
Code: [Select]
/*
 Simple Polyphonic Piano

 Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
 attached to pins 11 thou 13.   Use  LED attached to pin 10.
 
The Circuit.
   * push buttons attached to pins 2 thou 9 from ground
   * speakers attached to pin 11,12 and 13 from ground
   * LED attached to pin 10 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 3 Mar 2010
 by Silver

*/
#include <Tone.h>

//define notes
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330

int button[] = {2, 3, 4}; //button pins array
int led = 10; //LED pin
int tones[3];

byte speakerpin[3] = {11,12,13}; //array of speaker pin
byte buttonset = HIGH;  //set button state high
byte buttonState[3] = {HIGH, HIGH, HIGH};   //set button states array//an array for storing 3 button states

void setup() {
 // set all pins to output
 pinMode(speakerpin[0], OUTPUT);  
   pinMode(speakerpin[1], OUTPUT);
     pinMode(speakerpin[2], OUTPUT);
 pinMode(led, OUTPUT);
 for(int x=0; x<3; x++) {
   pinMode(button[x], OUTPUT);
 }
 // buttons are in the high position
 for(int x=0; x<3; x++) {
   digitalWrite(button[x], HIGH);
 }
}

void loop()
{  

tones[0] = 0;
tones[1] = 0;
tones[2] = 0;

buttonState[0] = HIGH;
buttonState[1] = HIGH;
buttonState[2] = HIGH;
 
for(int A=0; A<3; A++)  // checks the state of each button

{
  buttonset = digitalRead(button[A]);  //put now state into buttonset

  if(buttonState[A] == buttonset == HIGH)  //compare
          {
          if(A == 0)  //which key held down
          {
           tones[0] = 0;  //store the tone in array
               buttonState[0] = HIGH; //store the state in array
        }
        if(A == 1)
        {
           tones[1] = 0;
               buttonState[1] = HIGH;
        }
        if(A == 2)
        {
           tones[2] = 0;
               buttonState[2] = HIGH;
        }
     }
if(buttonState[A] != buttonset)  //compare
          {
          if(A == 0)  //which key held down
          {
           tones[0] = NOTE_E4;  //store the tone in array
               buttonState[0] = LOW; //store the state in array
        }
        if(A == 1)
        {
           tones[1] = NOTE_D4;
               buttonState[1] = LOW;
        }
        if(A == 2)
        {
           tones[2] = NOTE_C4;
               buttonState[2] = LOW;
        }
     }
  }
  for(int t = 0; t < 3; t++) {
  if (tones[t] > 0)
           {
       digitalWrite(speakerpin[0], HIGH);
     delayMicroseconds(tones[t]);
     digitalWrite(speakerpin[0], LOW);
     delayMicroseconds(tones[t]);
       
       digitalWrite(led, HIGH);
       delayMicroseconds(tones[t]);
       digitalWrite(led, LOW);
     delayMicroseconds(tones[t]);
           }

}
 
}


now the result is: press one key, output one note. It looks like monophonic piano. Then, when I press two keys, it outputs another lower note I didnt define. Is that normal?

I can't do play 1st, then play 2nd, then play 3rd if 3 keys are held down...

the above is my result,because of it,
I seriously read your posts and my code again and again><
but I don't think the code has errors, may be I can't see them.
I think you are called Dr. Paul Stoffregen@@

I read the code example you gave me...@@only LED keeps on and off. I understand the concept but how to do it?

SOS!!!!!!!! :-/ :'( :-/ :'(

PaulS

I really think you need to stop changing 47 things between iterations, and then saying you can't do what I suggest you do.

The tone library has functions to start playing a tone and to stop playing the tone. That's all you need to do.

When a button is pressed, start playing the tone. When the button is released, stop playing the tone.

The only thing you need to keep track of, in arrays, are the button pins and speaker pins.

Code: [Select]
int buttonPins[] = {8, 9, 10}; // Set these to match YOUR buttons
int speakerPins[] = {4, 5, 6}; // Ditto

Tone note1;
Tone note2;
Tone note3;

void setup()
{
  note1.begin(speakerPins[0]);
  note2.begin(speakerPins[1]);
  note3.begin(speakerPins[2]);
}

void loop()
{
  if(digitalRead(buttonPins[0]) == HIGH) // If the button is pressed
     note1.play();
  else
     note1.stop();

  if(digitalRead(buttonPins[1]) == HIGH) // If the button is pressed
     note2.play();
  else
     note2.stop();

  if(digitalRead(buttonPins[2]) == HIGH) // If the button is pressed
     note3.play();
  else
     note3.stop();
}


If your buttons are wired so that LOW is returned when the button is pressed, change HIGH to LOW.
The art of getting good answers lies in asking good questions.

falohao

but I followed your suggestions.
reset the tone to 0

reset the buttonstate to HIGH

do not include tone library frist because i didnt comprehend it

3 Speakers for 3 different notes

try to write code to check buttonstate and compare between previous and current

i posted wrong version of my code
Code: [Select]
/*
 Simple Polyphonic Piano

 Using 3 push buttons attached to pins 2 thou 4 and 3 speakers
 attached to pins 11 thou 13.   Use  LED attached to pin 10.
 
The Circuit.
   * push buttons attached to pins 2 thou 9 from ground
   * speakers attached to pin 11,12 and 13 from ground
   * LED attached to pin 10 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 3 Mar 2010
 by Silver

*/
int C = 262;
int D = 294;
int E = 330;

int button[] = {2, 3, 4}; //button pins array
int led = 10; //LED pin
int tones[3];

byte speakerpin[3] = {11,12,13}; //array of speaker pin
byte buttonset = HIGH;  //set button state high
byte buttonState[3] = {HIGH, HIGH, HIGH};   //set button states array//an array for storing 3 button states

void setup() {
 // set all pins to output
 pinMode(speakerpin[0], OUTPUT);  
   pinMode(speakerpin[1], OUTPUT);
     pinMode(speakerpin[2], OUTPUT);
 pinMode(led, OUTPUT);
 for(int x=0; x<3; x++) {
   pinMode(button[x], OUTPUT);
 }
 // buttons are in the high position
 for(int x=0; x<3; x++) {
   digitalWrite(button[x], HIGH);
 }
}

void loop()
{  

tones[0] = 0;
tones[1] = 0;
tones[2] = 0;

buttonState[0] = HIGH;
buttonState[1] = HIGH;
buttonState[2] = HIGH;
 
for(int A=0; A<3; A++)  // checks the state of each button

{
  buttonset = digitalRead(button[A]);  //put now state into buttonset

  if(buttonState[A] == buttonset == HIGH)  //compare
          {
          if(A == 0)  //which key held down
          {
           tones[0] = 0;  //store the tone in array
               buttonState[0] = HIGH; //store the state in array
        }
        if(A == 1)
        {
           tones[1] = 0;
               buttonState[1] = HIGH;
        }
        if(A == 2)
        {
           tones[2] = 0;
               buttonState[2] = HIGH;
        }
     }
if(buttonState[A] != buttonset)  //compare
          {
          if(A == 0)  //which key held down
          {
           tones[0] = 262;  //store the tone in array
               buttonState[0] = LOW; //store the state in array
        }
        if(A == 1)
        {
           tones[1] = 294;
               buttonState[1] = LOW;
        }
        if(A == 2)
        {
           tones[2] = 330;
               buttonState[2] = LOW;
        }
     }
  }
  for(int t = 0; t < 3; t++) {
  if (tones[t] > 0){
    if(tones[t] == 262){
       digitalWrite(speakerpin[0], HIGH);
     delayMicroseconds(tones[t]);
     digitalWrite(speakerpin[0], LOW);
     delayMicroseconds(tones[t]);}
    if(tones[t] == 294){
       digitalWrite(speakerpin[1], HIGH);
     delayMicroseconds(tones[t]);
     digitalWrite(speakerpin[1], LOW);
     delayMicroseconds(tones[t]);}
    if(tones[t] == 330){
       digitalWrite(speakerpin[2], HIGH);
     delayMicroseconds(tones[t]);
     digitalWrite(speakerpin[2], LOW);
     delayMicroseconds(tones[t]);}



       digitalWrite(led, HIGH);
       delayMicroseconds(tones[t]);
       digitalWrite(led, LOW);
     delayMicroseconds(tones[t]);}

}
 
}


output: 1 note to 1 speaker
(when I press 2 keys, lower note comes from that 2 speakers)
(when I press 3 keys, lower note comes from that 3 speakers)


PaulS

OK. We can keeping flogging this dead horse. I don't mind.

Start by explaining this:
Code: [Select]
          if(A == 0)  //which key held down
          {
           tones[0] = 0;  //store the tone in array
               buttonState[0] = HIGH; //store the state in array
        }
        if(A == 1)
        {
           tones[1] = 0;
               buttonState[1] = HIGH;
        }
        if(A == 2)
        {
           tones[2] = 0;
               buttonState[2] = HIGH;
        }


Wouldn't this work just as well:
Code: [Select]
 tones[A] = 0;
 buttonState[A] = HIGH;
The art of getting good answers lies in asking good questions.

falohao

followings are setting tone[0]to[2] to 0 and also setting ButtonState[0]to[2] to HIGH if NO key pressed.
I guess you know that
Code: [Select]
          if(A == 0)  //which key held down
          {
           tones[0] = 0;  //store the tone in array
               buttonState[0] = HIGH; //store the state in array
        }
        if(A == 1)
        {
           tones[1] = 0;
               buttonState[1] = HIGH;
        }
        if(A == 2)
        {
           tones[2] = 0;
               buttonState[2] = HIGH;
        }


I try to change that to yours.

falohao

OK!! Changed@@ result is same as before.
Next, this part is setting tones and buttonState if key held down.
Code: [Select]
if(buttonState[A] != buttonset)  //compare
          {
          if(A == 0)  //which key held down
          {
           tones[0] = 262;  //store the tone in array
               buttonState[0] = LOW; //store the state in array
        }
        if(A == 1)
        {
           tones[1] = 294;
               buttonState[1] = LOW;
        }
        if(A == 2)
        {
           tones[2] = 330;
               buttonState[2] = LOW;
        }


Do you find any problem in it? Dr.

Go Up