Push buttons and switch cases

I can’t get my switch cases to work properly, the sketch is meant to wait for input then after receiving input head to the second switch case and await input again and then play a tune through a buzzer, when the push button is pressed it seems to skip the second switch case and immediately begins playing the tune. I’ve tried including a delay but that hasn’t worked either. Please help I have literally no idea what to do.

float note_A0 = 62500 / 27.5;
float note_Bb0 = 62500 / 29.14;
float note_B0 = 62500 / 30.87;
float note_C1 = 62500 / 32.7;
float note_Cs1 = 62500 / 34.65;
float note_D1 = 62500 / 36.71;
float note_Eb1 = 62500 / 38.89;
float note_E1 = 62500 / 41.20;
float note_F1 = 62500 / 43.65;
float note_Fs1 = 62500 / 46.25;
float note_G1 = 62500 / 49.0;
float note_Gs1 = 62500 / 51.91;
float note_A1 = 62500 / 55;
float note_Bb1 = 62500 / 58.27;
float note_B1 = 62500 / 61.74;
float note_C2 = 62500 / 65.41;
float note_Cs2 = 62500 / 69.30;
float note_D2 = 62500 / 73.42;
float note_Eb2 = 62500 / 77.78;
float note_E2 = 62500 / 82.41;
float note_F2 = 62500 / 87.31;
float note_Fs2 = 62500 / 92.50;
float note_G2 = 62500 / 98.0;
float note_Gs2 = 62500 / 103.83;
float note_A2 = 62500 / 110.0;
float note_Bb2 = 62500 / 116.54;
float note_B2 = 62500 / 123.47;
float note_C3 = 62500 / 130.81;
float note_Cs3 = 62500 / 138.59;
float note_D3 = 62500 / 146.83;
float note_Eb3 = 62500 / 155.56;
float note_E3 = 62500 / 164.81;
float note_F3 = 62500 / 174.61;
float note_Fs3 = 62500 / 185.0;
float note_G3 = 62500 / 196.0;
float note_Gs3 = 62500 / 207.65;
float note_A3 = 62500 / 220.0;
float note_Bb3 = 62500 / 233.08;
float note_B3 = 62500 / 246.94;
float note_C4 = 62500 / 261.63;
float note_Cs4 = 62500 / 277.18;
float note_D4 = 62500 / 293.66;
float note_Eb4 = 62500 / 311.13;
float note_E4 = 62500 / 329.63;
float note_F4 = 62500 / 349.23;
float note_Fs4 = 62500 / 369.99;
float note_G4 = 62500 / 392.0;
float note_Gs4 = 62500 / 415.30;
float note_A4 = 62500 / 440;
float note_Bb4 = 62500 / 466.16;
float note_B4 = 62500 / 493.88;
float note_C5 = 62500 / 523.25;
float note_Cs5 = 62500 / 554.37;
float note_D5 = 62500 / 587.33;
float note_Eb5 = 62500 / 622.25;
float note_E5 = 62500 / 659.25;
float note_F5 = 62500 / 698.46;
float note_Fs5 = 62500 / 739.99;
float note_G5 = 62500 / 783.99;
float note_Gs5 = 62500 / 830.61;
float note_A5 = 62500 / 880.0;
float note_Bb5 = 62500 / 932.33;
float note_B5 = 62500 / 987.77;
float note_C6 = 62500 / 1046.50;
float note_Cs6 = 62500 / 1108.73;
float note_D6 = 62500 / 1174.66;
float note_Eb6 = 62500 / 1244.51;
float note_E6 = 62500 / 1318.51;
float note_F6 = 62500 / 1396.91;
float note_Fs6 = 62500 / 1479.98;
float note_G6 = 62500 / 1567.98;
float note_Gs6 = 62500 / 1661.22;
float note_A6 = 62500 / 1760.00;
float note_Bb6 = 62500 / 1864.66;
float note_B6 = 62500 / 1975.53;
float note_C7 = 62500 / 2093.0;
float note_Cs7 = 62500 / 2217.46;
float note_D7 = 62500 / 2349.32;
float note_Eb7 = 62500 / 2489.02;
float note_E7 = 62500 / 2637.02;
float note_F7 = 62500 / 2793.83;
float note_Fs7 = 62500 / 2959.96;
float note_G7 = 62500 / 3135.96;
float note_Gs7 = 62500 / 3322.44;
float note_A7 = 62500 / 3520.00;
float note_Bb7 = 62500 / 3729.31;
float note_B7 = 62500 / 3951.07;
float note_C8 = 62500 / 4186.01;
float potc[] = {
  note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, note_D5, // 15 INTRO
  note_A4, note_C5, note_D5, note_D5, note_D5, note_E5, note_F5, note_F5, note_F5, note_G5, note_E5, note_E5, note_D5, note_C5, note_C5, note_D5,
  note_A4, note_C5, note_D5, note_D5, note_D5, note_E5, note_F5, note_F5, note_F5, note_G5, note_E5, note_E5, note_D5, note_C5, note_D5,//End of part 1
  note_A4, note_C5, note_D5, note_D5, note_D5, note_F5, note_G5, note_G5, note_G5, note_A5, note_Bb5, note_Bb5, note_A5, note_G5, note_A5, note_D5, note_D5,
  note_E5, note_F5, note_F5, note_G5, note_A5, note_D5, note_D5, note_F5, note_E5, note_E5, note_F5, note_D5, note_E5,//END OF PART 3
  note_A5, note_Bb5, note_A5, note_A5, note_A5, note_A5, note_G5, note_G5, note_F5, note_E5, note_F5, note_E5, note_D5, //END OF PART 4
  note_A5, note_Bb5, note_A5, note_A5, note_C6, note_A5, note_G5, note_G5, note_F5, note_E5, note_F5, note_E5, note_D5
};
int potcdur[] = {250, 125, 250, 125, 250, 125, 125, 125, 125, 250, 125, 250, 125, 250, 125, 125,
                 125, 125, 250, 250,
                 125, 125, 250, 250,
                 125, 125, 250, 250,
                 125, 125, 125, 375,

                 125, 125, 250, 250,
                 125, 125, 250, 250,
                 125, 125, 250, 250,
                 125, 125, 375,


                 125, 125, 250, 250,
                 125, 125, 250, 250,
                 125, 125, 250, 250,
                 125, 125, 125, 375,


                 125, 125, 250, 250,
                 250, 125, 375,
                 125, 125, 250, 250,
                 125, 125, 500,

                 625, 500,


                 250, 250,
                 250, 125, 375, 625, 500,
                 250, 250,
                 250, 500, 675, 500,

                 250, 250,
                 250, 125, 375, 625, 500,
                 250, 250,
                 250, 2000,
                };
int A = sizeof(potc) / sizeof(potc[0]);
int tmp, i , j, Default, t, y, tmp2 = 1;
//8 button keypad to control servo pin 37 and 36
#define BUTTON_PORT PORTC
#define BUTTON_PIN PINC

#define BUTTON_BIT0 PC0
#define BUTTON_BIT1 PC1
#define BUTTON_BIT2 PC2
#define BUTTON_BIT3 PC3
#define BUTTON_BIT4 PC4
#define BUTTON_BIT5 PC5
#define BUTTON_BIT6 PC6
#define BUTTON_BIT7 PC7
int button;

void setup() {
  DDRB = (1 << DDB7);
  TCCR1A = (1 << COM1C1) | (1 << WGM11);
  TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS12);
  DDRE = (1 << DDE3); //Make PE3 (pins 5 of arduino) output pin (OC3A)
  BUTTON_PORT |= (1 << BUTTON_BIT0) | (1 << BUTTON_BIT1);

  TCCR3A = (1 << COM3A1) | (1 << WGM31); //Fast PWM mode 14, output compare mode 2 for channel A
  TCCR3B = (1 << WGM33) | (1 << WGM32) | (1 << CS32); //Fast PWM mode 14, prescaler clkio/256

  OCR3A = 93; //Drive Servo1 to Neutral mid-point
  ICR3 = 1250;
  TCNT3 = 0;

}

int button_is_pressed() {
  if (bit_is_clear(BUTTON_PIN, BUTTON_BIT0)) {
    _delay_ms(25);
    if (bit_is_clear(BUTTON_PIN, BUTTON_BIT0))
      return 1;
  }
  if (bit_is_clear(BUTTON_PIN, BUTTON_BIT1)) {
    _delay_ms(25);
    if (bit_is_clear(BUTTON_PIN, BUTTON_BIT1))
      return 2;
  }
  if (bit_is_clear(BUTTON_PIN, BUTTON_BIT2)) {
    _delay_ms(25);
    if (bit_is_clear(BUTTON_PIN, BUTTON_BIT2))
      return 3;
  }
  return 0;
}

void Pirates()  {
  for (i = 0; i < A; i++) {
    ICR1 = potc[i] + 3;
    OCR1C = potc[i] / 2;
    TCNT1 = 0;
    for (j = 0; j < potcdur[i]; j++) {
      _delay_ms(1);
    }
  }
  ICR1 = 0; OCR1C = 0; TCNT1 = 0;
}
void move_to_0() {
  OCR3A = 82;
}
void Allmodule() {
 button = button_is_pressed();
 switch (button) {
    case 1:
      Pirates();//Play Pirates
      break;
  }
}

void loop() {
  button = button_is_pressed();
  switch (button) {
    case 1:
      _delay_ms(500);
      button_is_pressed();
      Allmodule();//go to all module
      break;
  }
}
      button_is_pressed();

What is the point of calling this function, if you don't care what it returns?

I don't see anything in that function that waits for input. Perhaps you could point out what part actually does the waiting.

That was me messing around trying to get it to work i’ve removed it here, Im thinking that the “button” variable doesn’t clear before the second switch case.

void Pirates()  {
  for (i = 0; i < A; i++) {
    ICR1 = potc[i] + 3;
    OCR1C = potc[i] / 2;
    TCNT1 = 0;
    for (j = 0; j < potcdur[i]; j++) {
      _delay_ms(1);
    }
  }
  ICR1 = 0; OCR1C = 0; TCNT1 = 0;
}
void move_to_0() {
  OCR3A = 82;
}
void Allmodule()  { //Here it doesn't wait for a button press and skips straight to the 'void Pirates()'
 button = button_is_pressed();
 switch (button) {
    case 1:
      Pirates();//Play Pirates
      break;
  }
}

void loop() {
  button = button_is_pressed();
  switch (button) {
    case 1:
      _delay_ms(500);
      Allmodule();//go to all module
      break;
  }
}
void Allmodule()  { //Here it doesn't wait for a button press and skips straight to the 'void Pirates()'

There is nothing in the code that waits for a switch press.

I can't tell how your switch(es) are wired, or what the pin mode is, or whether or not you have the internal pullup resistors enabled, so you might be suffering from the dreaded floating pin problem.

The first switch case in the loop seems to work for some reason though, its just it skips the second and the second is almost a copy,
isnt the
" button = button_is_pressed();
switch (button)"

make it wait because the switch case uses the int from the button variable?

isnt the
" button = button_is_pressed();
switch (button)"

make it wait because the switch case uses the int from the button variable?

No, there is nothing that makes the code wait. It just has nothing to do until the correct SWITCH (NOT button!) is pressed.

Why won't you answer ALL the questions that are posed? How are the damned switches wired?

The way it is wired is one of these MATRIX and pins 1-8 are wired to the digital pins with a 10k resistor between it and ground

link123100:
The way it is wired is one of these MATRIX and pins 1-8 are wired to the digital pins with a 10k resistor between it and ground

Have you tries another sketch, that does NOT use direct port manipulation, to assure that the switch hardware is working?