Why is my button have all the LEDs light up when held down and the 2-digit 7 segment go haywire when a specified button is pressed?

int mode = 0;  // Stores the active mode
const int button1 = 3;  // Mode 1
const int button2 = 5;  // Mode 2
const int button3 = 7;  // Mode 3
const int button4 = 9;  // Mode 4
const int unitsPins[4] = {24, 25, 26, 27};    // Tens digit (left display)
const int tensPins[4] = {28, 29, 30, 31};   // Units digit (right display)
void setup() {
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
}
// Function to detect the last activated button and set the mode
void getMode() {
  if (debounceButton(button1)) { mode = 1; }
  else if (debounceButton(button2)) { mode = 2; }
  else if (debounceButton(button3)) { mode = 3; }
  else if (debounceButton(button4)) { mode = 4; }
}
// Function for Mode 1
void mode1Function() {
  while (mode == 1) {
    // Insert code for Mode 1 here (e.g., LED blink, counter, etc.)
    for (int num = 0; num <= 99; num++) {
      int tens = num / 10;
      int units = num % 10;
      for (int i = 0; i < 4; i++) {
        digitalWrite(tensPins[i], (tens >> i) & 1);
        digitalWrite(unitsPins[i], (units >> i) & 1);
      }
      delayCheck(500); 
     }
    }
  }
// Function for Mode 2
void mode2Function() {
  while (mode == 2) {
    for (int j = 9; j >= 0; j--) {  // Tens place
      digitalWrite(28, (j >> 0) & 1);
      digitalWrite(29, (j >> 1) & 1);
      digitalWrite(30, (j >> 2) & 1);
      digitalWrite(31, (j >> 3) & 1);
      for (int m = 9; m >= 0; m--) {  // Ones place
        digitalWrite(24, (m >> 0) & 1);
        digitalWrite(25, (m >> 1) & 1);
        digitalWrite(26, (m >> 2) & 1);
        digitalWrite(27, (m >> 3) & 1);
        delayCheck(500);
      }
    }
  }
}

// Function for Mode 3
void mode3Function() {
  while (mode == 3) {
    // Insert code for Mode 3 here
    for(int r = 24; r <=31; r++){
      //getMode();
      for (int i = 24; i <=31; i++){
      delayCheck(1);
    digitalWrite(i, LOW);
    }
      digitalWrite(r, HIGH);
      delayCheck(50);
      digitalWrite(r, LOW);
    }
  }
}
// Function for Mode 4
void mode4Function() {
  while (mode == 4) {
    // Insert code for Mode 4 here
    for (int i = 24; i <=31; i++){
     getMode();
    delayCheck(1);
    digitalWrite(i, LOW);
    }
  }
}
void delayCheck(int m){
  for(int d = 0; d < m; d++){        if (digitalRead(button1) == LOW) { mode = 1; return; }
        if (digitalRead(button2) == LOW) { mode = 2; return; }
        if (digitalRead(button3) == LOW) { mode = 3; return; }
        if (digitalRead(button4) == LOW) { mode = 4; return; }
        delay(1);
  }
}void clearDisplay() {
  for (int i = 24; i <= 31; i++) {
    digitalWrite(i, LOW);
  }
}int debounceButton(int pin) {
  if (digitalRead(pin) == LOW) { 
    for (int i = 0; i < 50; i++) {  // 50ms debounce delay
      delay(1);
      if (digitalRead(pin) == HIGH) return 0;  // If released early, ignore
    }
    while (digitalRead(pin) == LOW);  // Wait for full release
    return 1;  // Valid press detected
  }
  return 0;
}
void loop() {
  getMode();  // Check the initial button press
  if (mode == 1) mode1Function(); 
  if (mode == 2) mode2Function(); 
  if (mode == 3) mode3Function(); 
  if (mode == 4) mode4Function(); 
}

I don't know how to debug my program because looks the same ay my previous codes, just the conditions in each "modes" were different.

Can you provide a brief description of what you intend to do in each mode?

  • Did you write this sketch ?

Can you post an annotated schematic showing how this is wired? Your problem could be hardware.

Post your previous code that worked except the conditions in each "modes" were different.

a7

Hello yubi_nator

Welcome to the best Arduino forum ever :slight_smile:

I´ve made small code review.

This blocks the expect realtime behaiviour of the sketch.

	Line  31:   while (mode == 1) {
	Line  46:   while (mode == 2) {
	Line  65:   while (mode == 3) {
	Line  81:   while (mode == 4) {

You have to rethink the logical design.

It is bizarre for sure. I had to cut away part of it just to see that it works, I still can't see how...

I assume you did not miss delayCheck(), the means by which the while loops are exited, and indict the sketch based on its reliance on delay().

It interesting what people who know a little bit come up with when left with a blank piece of paper.

"Enough to be dangerous."

@yubi_nator - you might benefit from using the IDE Autoformat tool. At least until you are automatically typing formatted code. It will make your sketch easier to read, and certain problems easier to soot.

a7

60% of it is mine, the rest if with the help of AI since wasn't going forward.

mode 1 is a count up from 00 to 99
mode 2 is a countdown from 99 to 00
mode 3 is blinking 00
mode 4 kinda like reset since it will make it 00

Im not sure about its wirings since I am using the custom training board for Mega Pro mini, provided by our professor.

I am using 2 common anode 7-segment display and 4 buttons(no resistors).

I think my ideas don't translate well to code. lmao

The buttons should be looping while it is on a "mode"

Only if I can upload the video of the outcome.

The delayCheck certainly is an out of the box way to leave the whiles...
I guess it should work...
Would be better though to move tge while to where the functions are called (where the ifs are).
Maybe better to only have the ifs so the you have non blocking code...