[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)

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);

Not sure if i'm understanding you or not.

Can you elaborate or show me how I would do 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!