Arduino keyboard code problem

Hey, I have just made this code to use buttons to act as a key board. However, the program is bugging out and the speaker constantly plays random noise. Please help me out.

int button_C = 2;
int button_D = 3;
int button_E = 4;
int button_F = 5;
int button_G = 6;
int button_A = 7;
int button_B = 8;
int button_Cup = 9; //set up all the variable for the pins

int speaker = 13;  //set up speaker pin

int buttonstate_C = 0;
int buttonstate_D = 0;
int buttonstate_E = 0;
int buttonstate_F = 0;
int buttonstate_G = 0;
int buttonstate_A = 0;
int buttonstate_B = 0;
int buttonstate_Cup = 0;//initialize all state of button to 0 just incase

//NOTES         'c'    ,   'd',    'e',    'f'  'g', 'a',    'b',  'C'
int tones[] = { 261.62, 293.66, 329.63, 349.23, 392, 440, 492.88, 523.25 }; //set up frequency of the notes
int Cur_tone = 0;

void setup()
{
  pinMode(button_C, INPUT);
  pinMode(button_D, INPUT);
  pinMode(button_E, INPUT);
  pinMode(button_F, INPUT);
  pinMode(button_G, INPUT);
  pinMode(button_A, INPUT);
  pinMode(button_B, INPUT);
  pinMode(button_Cup, INPUT);//set button pins as input

  pinMode(speaker, OUTPUT);//set speaker pin as output
}

void loop()
{
  buttonstate_C = digitalRead(button_C);
  buttonstate_D = digitalRead(button_D);
  buttonstate_E = digitalRead(button_E);
  buttonstate_F = digitalRead(button_F);
  buttonstate_G = digitalRead(button_G);
  buttonstate_A = digitalRead(button_A);
  buttonstate_B = digitalRead(button_B);
  buttonstate_Cup = digitalRead(button_Cup);//link the state of button to what the arduino read

  if((buttonstate_C == HIGH) || (buttonstate_E == HIGH) || 
    (buttonstate_G == HIGH) || (buttonstate_D == HIGH) || 
    (buttonstate_F == HIGH) || (buttonstate_A == HIGH) || 
    (buttonstate_B == HIGH) || (buttonstate_Cup == HIGH) ) //if any of the buttons are pressed
  { 
    if (buttonstate_C == HIGH)
    {
      Cur_tone = tones[0];
    } //set note tone to C
    if (buttonstate_D == HIGH)
    {
      Cur_tone = tones[1];
    }//set note tone to D
    if (buttonstate_E == HIGH)
    {
      Cur_tone = tones[2];
    }//set note tone to E
    if (buttonstate_F == HIGH)
    {
      Cur_tone = tones[3];
    }//set note tone to F
    if (buttonstate_G == HIGH)
    {
      Cur_tone = tones[4];
    }//set note tone to G
    if (buttonstate_A == HIGH)
    {
      Cur_tone = tones[5];
    }//set note tone to A
    if (buttonstate_B == HIGH)
    {
      Cur_tone = tones[6];
    }//set note tone to B
    if (buttonstate_Cup == HIGH)
    {
      Cur_tone = tones[7];
    }//set note tone to higher C

      tone(speaker,Cur_tone);

  }//manualy change the pulse for the speakers
  else //in case no button is pressed , close the speaker
  {
    digitalWrite(speaker, LOW);
  }

}

If you have wired your buttons between each input pin and ground, you need to set:

pinMode( button_C, INPUT_PULLUP ); // see Arduino reference: pinMode

Then, when you press a button it will go LOW, not HIGH

Other than that, rest of your code looks sorta Ok.

Yours,
TonyWilk

In your code there is a comment:-

in case no button is pressed , close the speaker

Their is no such thing as closing down the speaker and certainly writing a zero to the audio output pin is not going to do anything but cause glitches. I suspect what you want to do here is stop any output, look at the tone reference page to see what call you need to do that.

https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/