[SOLVED] Sustain pedal for piano using a buzzer and push buttons

Hello,
i’m currently having issues getting a sustain pedal to work with my arduino.

I don’t know how to get the buzzer to play the tone when i press the push button and pedal, then continue to play the tone when releasing the push button and holding the pedal down.

the buzzer should stop playing the tone when releasing the pedal as well.

sustain_pedal.ino (982 Bytes)

How can i get my sustain pedal to work?

What i want to happen: If i press my C tone key while holding my sustain button it plays the C tone, then release my C tone key and still hold my sustain button it will continue to play my C note until i release the sustain button.

What happens: The “if statement” does not continue to play the C tone when releasing the C tone key while holding the sustain key. I tried exchanging the “if statement” for a “while statement,” which does continue to play the note, but does not stop when i release the sustain button and I can’t get it out of the while loop without it doing the same thing as the “if statement”.

I’ve tried multiple ways around this without any success.
does any one have an idea so i can get this to work?

int button_C = 2;
int sustainPedal = 11;
int speaker = 13;
int buttonstate_C = 0;


//NOTES         'c'  , 'd',  'e',  'f',  'g', 'a',  'b',  'C'
int tones1[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; //freq
int tones2[] = { 3830, 3400, 3038, 2864, 2550, 2272, 2028, 1912};
int Cur_tone = 0;


void setup()
{
  pinMode(10,INPUT);
  pinMode(sustainPedal,INPUT);
  pinMode(button_C, INPUT);

  pinMode(speaker, OUTPUT);
}

void loop()
{

    piano2();

}


void piano2()
{
  int  buttonstate_C= digitalRead(button_C);
  int  pedalState = digitalRead(sustainPedal);

  if((buttonstate_C == LOW) && (pedalState == LOW))
   { 
      while(pedalState == LOW)
      {
    Cur_tone = tones1[1];
    digitalWrite(speaker, HIGH);
    delayMicroseconds(Cur_tone);
    digitalWrite(speaker, LOW);
    delayMicroseconds(Cur_tone);
      }
      
}
 else if((buttonstate_C == LOW))
  {
    if (buttonstate_C == LOW)
    {
      Cur_tone = tones1[0];
    }
    digitalWrite(speaker, HIGH);
    delayMicroseconds(Cur_tone);
    digitalWrite(speaker, LOW);
    delayMicroseconds(Cur_tone);
    } 
    
  else //in case no button is pressed , close the piezo

    digitalWrite(speaker, LOW);


  }

Project1_Piano.ino (1.18 KB)

while(pedalState == LOW)
      {
    Cur_tone = tones1[1];
    digitalWrite(speaker, HIGH);
    delayMicroseconds(Cur_tone);
    digitalWrite(speaker, LOW);
    delayMicroseconds(Cur_tone);
      }

The problem with this while loop is that you never read any new value into pedalState inside it. So once you're in you're locked in forever. You should read that pin again each pass through the while loop and get a new value for pedalState.

 else if((buttonstate_C == LOW)){  // if buttonstate_C is LOW here
    if (buttonstate_C == LOW)   // it is safe to assume it will still be LOW here.  There's no reason to test it again.

Double posted here: http://forum.arduino.cc/index.php?topic=491401.msg3353031#msg3353031

OP, please one thread per question. I put an answer on your other thread.

EDIT: This post was on the other thread before the merge.

Threads merged.

My apologies, first time posting and i thought the first one did not post or got deleted.

Thanks for the help, i'll try this out!

Hello, i tried reading my pin again and it still is stuck in the while loop.

I'm fairly new to arduino, so maybe i'm misinterpreting you

 if((buttonstate_C == LOW) && (pedalState == LOW))
   { 
      while(pedalState == LOW)
      {
    Cur_tone = tones1[1];
    digitalWrite(speaker, HIGH);
    delayMicroseconds(Cur_tone);
    digitalWrite(speaker, LOW);
    delayMicroseconds(Cur_tone);
    digitalRead(sustainPedal);
      }
      
}

hm
pedalState = digitalRead(sustainPedal);

digitalRead(sustainPedal);
      }

You sure enough call digitalRead but you just threw away the result. You need that result put into the pedalState variable.

Not sure if i’m understanding you or not.

Can you elaborate or show me how I would do this?

miskaalex14:
Not sure if i’m understanding you or not.

Can you elaborate or show me how I would do this?

Are you being serious? Did you skip over reply #8?

You need to save the result of the digitalRead into your pedalState variable. That would be:

pedalState = digitalRead(sustainPedal);

If that’s something that is confusing to you, then you need to put this project aside. It is too complicated for you. Go work on some of the basic tutorials and lessons and examples before you come back to this.

Read post #8. See the difference between that and what you have? Make your program look like that.

Steve

Can't believe i overlooked that, sorry for the frustration.

I got it working now, thanks!