digitalRead() breaks my program

#define red 4
#define green 3
#define blue 2
#define leftButton 8
#define rightButton 7
#define buzzer 11
#define trigger 10
#define echo 9

LiquidCrystal_I2C lcd(0x26,  16, 2);

int lightMode = 0;

enum Mode{
  MENU,
  PLAY,
  TRANSITION
};

Mode currentMode = MENU;

int currentSong = 0;
const int maxSongCount = 6;

bool toMenu = false;

void setup(){
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(leftButton, INPUT);
  pinMode(rightButton, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);

  lcd.init();
  lcd.backlight();

  lcd.print("Opening Omnitrix");
  playSong(nokia, nokia_durations, sizeof(nokia_durations));
  delay(500);
  lcd.clear();
  lcd.print("Opened");
  delay(1000);
  lcd.clear();

  Serial.begin(9600);
}

void loop()
{
  // Serial.print("Mode: ");
  // Serial.println(currentMode);
  switch(currentMode){
    case MENU:
      lightMode = 0;
      updateLight();

      int rState = digitalRead(rightButton);
      int lState = digitalRead(leftButton);
      if(rState == HIGH){
        increment();
        delay(50);
      }else if(lState == HIGH){
        decrement();
        delay(50);
      }

      songChoices();
      if(getDistance() <= 2){
        lcd.clear();
        lcd.print("Playing...");
        currentMode = TRANSITION;
      }
      break;
    case PLAY:
      lightMode = 1;
      updateLight();
      songToPlay();
      toMenu = true;
      currentMode = TRANSITION;
      break;
    case TRANSITION:
        lightMode = 2;
        updateLight();
        delay(1000);
      if(toMenu == false){
        toMenu = true;
        currentMode = PLAY;
      }else{
        toMenu = false;
        currentMode = MENU;
      }
      break;
  }
}

void updateLight(){
  switch(lightMode){
    case 0:
      analogWrite(red, 255);
      analogWrite(green, 0);
      analogWrite(blue, 0);
      break;
    case 1:
      analogWrite(red, 0);
      analogWrite(green, 255);
      analogWrite(blue, 0);
      break;
    case 2:
      analogWrite(red, 0);
      analogWrite(green, 0);
      analogWrite(blue, 255);
      break;
  }
}

void songToPlay(){
      Serial.print("Playing SOng");
  switch(currentSong){
    case 0:
      playSong(nokia, nokia_durations, sizeof(nokia_durations));
      break;
    case 1:
      playSong(rickroll, rickroll_durations, sizeof(rickroll_durations));
      break;
    case 2:
      playSong(end, end_durations, sizeof(end_durations));
      break;
    case 3:
      playSong(pirates, pirates_durations, sizeof(pirates_durations));
      break;
    case 4:
      playSong(hbd, hbd_durations, sizeof(hbd_durations));
      break;
    case 5:
      playSong(drift, drift_durations, sizeof(drift_durations));
      break;
    case 6:
      playSong(xmas, xmas_durations, sizeof(xmas_durations));
      break;
  }
}

void songChoices(){
  lcd.clear();
  switch(currentSong){
    case 0:
      lcd.print("NOKIA");
      break;
    case 1:
      lcd.print("RICKROLL");
      break;
    case 2:
      lcd.print("END OF BEGINNING");
      lcd.setCursor(0,1);
      lcd.print("pero parang hindi");
      lcd.setCursor(0,0);
      break;
    case 3:
      lcd.print("PIRATES");
      break;
    case 4:
      lcd.print("HBDMBTC");
      break;
    case 5:
      lcd.print("DRIFTING");
      break;
    case 6:
      lcd.print("XMAS");
      break;
  }
}

float getDistance(){
  digitalWrite(trigger, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  
  float duration = pulseIn(echo, HIGH);
  return (duration * 0.0343) / 2; 
}

void increment(){
  currentSong = (currentSong + 1) % (maxSongCount + 1);
}

void decrement(){
  currentSong = (currentSong - 1 + (maxSongCount + 1)) % (maxSongCount + 1);
}

void playSong(int melody[], int durations[], int durationSize){
  int size = durationSize / sizeof(int);

  for (int note = 0; note < size; note++) {
    int duration = 1000 / durations[note];
    tone(buzzer, melody[note], duration);

    int pauseBetweenNotes = duration * 1.30;
    delay(pauseBetweenNotes);

    noTone(buzzer);
  }
}

This part is my problem:

int rState = digitalRead(rightButton);
      int lState = digitalRead(leftButton);
      if(rState == HIGH){
        increment();
        delay(50);
      }else if(lState == HIGH){
        decrement();
        delay(50);
      }

Icant post the whole code because those are gigantic arrays. Context later.

So i was trying to make a music player with a piezo buzzer as the one making the sound, i found a github pag with notes and everything and base my code on that.

I've debugged for hours and everything led to the button checks. For some reason when I have it the currentMode would be stuck at TRANSITION and wont even play the song. I wanted to try making the digitalReads global but it would exceed the memory.

I'm so lost with this one :sob:. Thanks for helping

Edit: I didn't know before that the codes can be scrolled here is the complete code with the melodies:

#include <LiquidCrystal_I2C.h>
#include <pitches.h>

int rickroll[] = {
  NOTE_A4, REST, NOTE_B4, REST, NOTE_C5, REST, NOTE_A4, REST,
  NOTE_D5, REST, NOTE_E5, REST, NOTE_D5, REST,

  NOTE_G4, NOTE_A4, NOTE_C5, NOTE_A4, NOTE_E5, NOTE_E5, REST,
  NOTE_D5, REST,

  NOTE_G4, NOTE_A4, NOTE_C5, NOTE_A4, NOTE_D5, NOTE_D5, REST,
  NOTE_C5, REST, NOTE_B4, NOTE_A4, REST,

  NOTE_G4, NOTE_A4, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_D5, REST,
  NOTE_B4, NOTE_A4, NOTE_G4, REST, NOTE_G4, REST, NOTE_D5, REST, NOTE_C5, REST,

  NOTE_G4, NOTE_A4, NOTE_C5, NOTE_A4, NOTE_E5, NOTE_E5, REST,
  NOTE_D5, REST,

  NOTE_G4, NOTE_A4, NOTE_C5, NOTE_A4, NOTE_G5, NOTE_B4, REST,
  NOTE_C5, REST, NOTE_B4, NOTE_A4, REST,

  NOTE_G4, NOTE_A4, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_D5, REST,
  NOTE_B4, NOTE_A4, NOTE_G4, REST, NOTE_G4, REST, NOTE_D5, REST, NOTE_C5, REST,

  NOTE_C5, REST, NOTE_D5, REST, NOTE_G4, REST, NOTE_D5, REST, NOTE_E5, REST,
  NOTE_G5, NOTE_F5, NOTE_E5, REST,

  NOTE_C5, REST, NOTE_D5, REST, NOTE_G4, REST
};

int rickroll_durations[] = {
  8, 8, 8, 8, 8, 8, 8, 4,
  8, 8, 8, 8, 2, 2,

  8, 8, 8, 8, 2, 8, 8,
  2, 8,

  8, 8, 8, 8, 2, 8, 8,
  4, 8, 8, 8, 8,

  8, 8, 8, 8, 2, 8, 8,
  2, 8, 4, 8, 8, 8, 8, 8, 1, 4,

  8, 8, 8, 8, 2, 8, 8,
  2, 8,

  8, 8, 8, 8, 2, 8, 8,
  2, 8, 8, 8, 8,

  8, 8, 8, 8, 2, 8, 8,
  4, 8, 3, 8, 8, 8, 8, 8, 1, 4,

  2, 6, 2, 6, 4, 4, 2, 6, 2, 3,
  8, 8, 8, 8,

  2, 6, 2, 6, 2, 1
};

int end[] = {
  REST,
  
  NOTE_D5, NOTE_D5, NOTE_E5, NOTE_FS5, NOTE_G5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5, NOTE_E5, NOTE_D5, REST,
  NOTE_B5, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_CS5, NOTE_B5, NOTE_A5, NOTE_B5, NOTE_CS5, NOTE_D5, NOTE_B5, NOTE_A5,
  NOTE_D5, NOTE_D5, NOTE_E5, NOTE_FS5, NOTE_G5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5, NOTE_E5, NOTE_D5,
  NOTE_A5, NOTE_D5, REST, NOTE_A5, NOTE_D5, REST, NOTE_A5, NOTE_D5, REST, NOTE_A5, NOTE_D5, NOTE_B5,
  NOTE_CS5, NOTE_CS5, NOTE_CS5, NOTE_CS5, NOTE_CS5,
  NOTE_A5, NOTE_CS5, NOTE_A5, NOTE_CS5, NOTE_A5, NOTE_CS5,
  NOTE_D5, NOTE_D5, REST,
  NOTE_CS5, NOTE_CS5, NOTE_CS5,
  NOTE_D5, NOTE_CS5, NOTE_D5, NOTE_CS5, NOTE_D5, NOTE_FS5,
  NOTE_D5, NOTE_CS5, NOTE_D5, NOTE_CS5, NOTE_D5, NOTE_CS5,
  NOTE_CS5, NOTE_D5, NOTE_CS5, NOTE_D5, NOTE_CS5, NOTE_CS5,
  NOTE_D5, NOTE_CS5, NOTE_CS5, NOTE_D5, NOTE_CS5, NOTE_CS5,
  NOTE_CS5, NOTE_D5, NOTE_CS5, NOTE_CS5, NOTE_G5, NOTE_FS5,
  NOTE_D5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_D5,

  REST
};

int end_durations[] = {
  4,

  4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 1, 4,
  4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 1, 2,
  4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4,
  4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 4, 2,
  4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4,
  4, 2, 2,
  4, 4, 4,
  4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4,

  1
};

int nokia[] = {
  NOTE_E5, NOTE_D5, NOTE_FS4, NOTE_GS4, 
  NOTE_CS5, NOTE_B4, NOTE_D4, NOTE_E4, 
  NOTE_B4, NOTE_A4, NOTE_CS4, NOTE_E4,
  NOTE_A4
};

int nokia_durations[] = {
  8, 8, 4, 4,
  8, 8, 4, 4,
  8, 8, 4, 4,
  2
};

int xmas[] = {
  NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
  NOTE_E5,
  NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
  NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
  NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
  NOTE_D5, NOTE_G5
};

int xmas_durations[] = {
  8, 8, 4,
  8, 8, 4,
  8, 8, 8, 8,
  2,
  8, 8, 8, 8,
  8, 8, 8, 16, 16,
  8, 8, 8, 8,
  4, 4
};

int hbd[] = {
  NOTE_C4, NOTE_C4, 
  NOTE_D4, NOTE_C4, NOTE_F4,
  NOTE_E4, NOTE_C4, NOTE_C4, 
  NOTE_D4, NOTE_C4, NOTE_G4,
  NOTE_F4, NOTE_C4, NOTE_C4,
  
  NOTE_C5, NOTE_A4, NOTE_F4, 
  NOTE_E4, NOTE_D4, NOTE_AS4, NOTE_AS4,
  NOTE_A4, NOTE_F4, NOTE_G4,
  NOTE_F4
};

int hbd_durations[] = {
  4, 8, 
  4, 4, 4,
  2, 4, 8, 
  4, 4, 4,
  2, 4, 8,
  
  4, 4, 4, 
  4, 4, 4, 8,
  4, 4, 4,
  2
};

int pirates[] = {
  NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
  NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
  NOTE_A4, NOTE_G4, NOTE_A4, REST,
  
  NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
  NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
  NOTE_A4, NOTE_G4, NOTE_A4, REST,
  
  NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
  NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, REST,
  NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, REST,
  NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, REST,
  
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
  NOTE_D5, NOTE_E5, NOTE_A4, REST,
  NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, REST,
  NOTE_C5, NOTE_A4, NOTE_B4, REST,
  
  NOTE_A4, NOTE_A4,
  //Repeat of first part
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
  NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
  NOTE_A4, NOTE_G4, NOTE_A4, REST,
  
  NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
  NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
  NOTE_A4, NOTE_G4, NOTE_A4, REST,
  
  NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
  NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, REST,
  NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, REST,
  NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, REST,
  
  NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
  NOTE_D5, NOTE_E5, NOTE_A4, REST,
  NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, REST,
  NOTE_C5, NOTE_A4, NOTE_B4, REST,
  //End of Repeat
  
  NOTE_E5, REST, REST, NOTE_F5, REST, REST,
  NOTE_E5, NOTE_E5, REST, NOTE_G5, REST, NOTE_E5, NOTE_D5, REST, REST,
  NOTE_D5, REST, REST, NOTE_C5, REST, REST,
  NOTE_B4, NOTE_C5, REST, NOTE_B4, REST, NOTE_A4,
  
  NOTE_E5, REST, REST, NOTE_F5, REST, REST,
  NOTE_E5, NOTE_E5, REST, NOTE_G5, REST, NOTE_E5, NOTE_D5, REST, REST,
  NOTE_D5, REST, REST, NOTE_C5, REST, REST,
  NOTE_B4, NOTE_C5, REST, NOTE_B4, REST, NOTE_A4
};

int pirates_durations[] = {
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8,
  
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8,
  
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 8, 4, 8,
  
  8, 8, 4, 8, 8,
  4, 8, 4, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 4,
  
  4, 8,
  //Repeat of First Part
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8,
  
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8,
  
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 8, 8,
  8, 8, 8, 4, 8,
  
  8, 8, 4, 8, 8,
  4, 8, 4, 8,
  8, 8, 4, 8, 8,
  8, 8, 4, 4,
  //End of Repeat
  
  4, 8, 4, 4, 8, 4,
  8, 8, 8, 8, 8, 8, 8, 8, 4,
  4, 8, 4, 4, 8, 4,
  8, 8, 8, 8, 8, 2,
  
  4, 8, 4, 4, 8, 4,
  8, 8, 8, 8, 8, 8, 8, 8, 4,
  4, 8, 4, 4, 8, 4,
  8, 8, 8, 8, 8, 2
};

int drift[] = {
  NOTE_AS4, REST, NOTE_AS4, REST, NOTE_AS4, REST, NOTE_AS4, REST,
  NOTE_AS4, NOTE_B4, NOTE_DS5,
  NOTE_AS4, REST, NOTE_AS4, REST,
  NOTE_AS4, NOTE_B4, NOTE_DS5,
  NOTE_AS4, REST, NOTE_AS4, REST,
  NOTE_AS4, NOTE_B4, NOTE_DS5,
  NOTE_AS4, REST, NOTE_AS4, REST,
  NOTE_AS4, NOTE_B4, NOTE_DS5,
  NOTE_F5, REST, NOTE_F5, REST,
  NOTE_GS5, NOTE_FS5, NOTE_F5,
  NOTE_AS4, REST, NOTE_AS4, REST,
  NOTE_GS5, NOTE_FS5, NOTE_F5,
  NOTE_AS4, REST, NOTE_AS4, REST,
  NOTE_AS4, NOTE_B4, NOTE_DS5,
  NOTE_AS4, REST, NOTE_AS4, REST,
  NOTE_AS4, NOTE_B4, NOTE_DS5,
  NOTE_AS4, REST, NOTE_AS4, REST,
  REST
};

int drift_durations[] = {
  4, 4, 4, 4, 4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  3, 3, 4,
  4, 4, 4, 4,
  1
};

#define red 4
#define green 3
#define blue 2
#define leftButton 8
#define rightButton 7
#define buzzer 11
#define trigger 10
#define echo 9

LiquidCrystal_I2C lcd(0x26,  16, 2);

int lightMode = 0;

enum Mode{
  MENU,
  PLAY,
  TRANSITION
};

Mode currentMode = MENU;

int currentSong = 0;
const int maxSongCount = 6;

bool toMenu = false;

void setup(){
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(leftButton, INPUT);
  pinMode(rightButton, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);

  lcd.init();
  lcd.backlight();

  lcd.print("Opening Omnitrix");
  playSong(nokia, nokia_durations, sizeof(nokia_durations));
  delay(500);
  lcd.clear();
  lcd.print("Opened");
  delay(1000);
  lcd.clear();

  Serial.begin(9600);
}

void loop()
{
  // Serial.print("Mode: ");
  // Serial.println(currentMode);
  switch(currentMode){
    case MENU:
      lightMode = 0;
      updateLight();

      int rState = digitalRead(rightButton);
      int lState = digitalRead(leftButton);
      if(rState == HIGH){
        increment();
        delay(50);
      }else if(lState == HIGH){
        decrement();
        delay(50);
      }

      songChoices();
      if(getDistance() <= 2){
        lcd.clear();
        lcd.print("Playing...");
        currentMode = TRANSITION;
      }
      break;
    case PLAY:
      lightMode = 1;
      updateLight();
      songToPlay();
      toMenu = true;
      currentMode = TRANSITION;
      break;
    case TRANSITION:
        lightMode = 2;
        updateLight();
        delay(1000);
      if(toMenu == false){
        toMenu = true;
        currentMode = PLAY;
      }else{
        toMenu = false;
        currentMode = MENU;
      }
      break;
  }
}

void updateLight(){
  switch(lightMode){
    case 0:
      analogWrite(red, 255);
      analogWrite(green, 0);
      analogWrite(blue, 0);
      break;
    case 1:
      analogWrite(red, 0);
      analogWrite(green, 255);
      analogWrite(blue, 0);
      break;
    case 2:
      analogWrite(red, 0);
      analogWrite(green, 0);
      analogWrite(blue, 255);
      break;
  }
}

void songToPlay(){
      Serial.print("Playing SOng");
  switch(currentSong){
    case 0:
      playSong(nokia, nokia_durations, sizeof(nokia_durations));
      break;
    case 1:
      playSong(rickroll, rickroll_durations, sizeof(rickroll_durations));
      break;
    case 2:
      playSong(end, end_durations, sizeof(end_durations));
      break;
    case 3:
      playSong(pirates, pirates_durations, sizeof(pirates_durations));
      break;
    case 4:
      playSong(hbd, hbd_durations, sizeof(hbd_durations));
      break;
    case 5:
      playSong(drift, drift_durations, sizeof(drift_durations));
      break;
    case 6:
      playSong(xmas, xmas_durations, sizeof(xmas_durations));
      break;
  }
}

void songChoices(){
  lcd.clear();
  switch(currentSong){
    case 0:
      lcd.print("NOKIA");
      break;
    case 1:
      lcd.print("RICKROLL");
      break;
    case 2:
      lcd.print("END OF BEGINNING");
      lcd.setCursor(0,1);
      lcd.print("pero parang hindi");
      lcd.setCursor(0,0);
      break;
    case 3:
      lcd.print("PIRATES");
      break;
    case 4:
      lcd.print("HBDMBTC");
      break;
    case 5:
      lcd.print("DRIFTING");
      break;
    case 6:
      lcd.print("XMAS");
      break;
  }
}

float getDistance(){
  digitalWrite(trigger, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  
  float duration = pulseIn(echo, HIGH);
  return (duration * 0.0343) / 2; 
}

void increment(){
  currentSong = (currentSong + 1) % (maxSongCount + 1);
}

void decrement(){
  currentSong = (currentSong - 1 + (maxSongCount + 1)) % (maxSongCount + 1);
}

void playSong(int melody[], int durations[], int durationSize){
  int size = durationSize / sizeof(int);

  for (int note = 0; note < size; note++) {
    int duration = 1000 / durations[note];
    tone(buzzer, melody[note], duration);

    int pauseBetweenNotes = duration * 1.30;
    delay(pauseBetweenNotes);

    noTone(buzzer);
  }
}

Describe 3 things that led to the button checks.

In that mode, the button checks are never done. They are done only in MENU mode.

digitalRead() is a standard Arduino function. It is already global. Please explain what you mean by making it global.

Do you mean you want the result of the digitalRead()s to be global ?

1 Like

I don't see any button library. Without control of the bouncing you will get what you are experiencing.

1 Like

Dunno if it is your problem but if you use local variabkes in the code for a case in a switch, you must { embrace } the case code. Or very odd and unexpected things can happen.

It seems like evyone learns this the hard way. Like

case MENU:
{
      lightMode = 0;
      updateLight();

      int rState = digitalRead(rightButton);
      int lState = digitalRead(leftButton);
      if (rState == HIGH) {
        increment();
        delay(50);
      } else if (lState == HIGH) {
        decrement();
        delay(50);
      }

      songChoices();
      if (getDistance() <= 2) {
        lcd.clear();
        lcd.print("Playing...");
        currentMode = TRANSITION;
      }
}
      break;

The braces at the left edge are the ones I added. Check you other cases for the same issue.

I sprinkled some spaces in there just to relax the density. You might like the IDE Autoformat tool if your fingers do not automatically write formatted code. Yet.

a7

1 Like

Do yo have pulldown resistors (10k) on the button pins?

1 Like

Describe 3 things that led to the button checks.
I meant that I tried commenting some part of the code and only when I commented out the button checks then would the program work.

In that mode, the button checks are never done. They are done only in MENU mode.
I know but for some reason this weird scenario would just happen.

digitalRead() is a standard Arduino function. It is already global. Please explain what you mean by making it global.
I meant like making the variables that hold the digitalRead() global and not localized to the MENU

my bad for all misunderstanding, I made this late at night.

yes, but i would get something like this if I made the variables global. Sketch uses 10418 bytes (32%) of program storage space. Maximum is 32256 bytes. Global variables use 2770 bytes (135%) of dynamic memory, leaving -722 bytes for local variables. Maximum is 2048 bytes.

So I discovered moving the arrays to PROGMEM, and then I just modified playSong(). And was able to move apply @alto777 solution. I also tried just moving the digitalRead() variables to global and it worked. I still don't know why it needs that but thanks for all the help.

I typivally get away with the builtin pullups.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.