Switch case makes Arduin Mega reset itself

Hi. I have a switch with multiple cases (7)

        switch (state_birdbox)
        {
        case BB_1:
        {
            if (pressed_button == BIRDBOX1)
            {
                state_birdbox = BB_2;
                setLightsBirdBox(BEC2);
                timer = main_time;
            }
            else
            {
                if (pressed_button != OUT_OF_BOUNDS)
                {
                    state_birdbox = BB_1;
                }
            }
            break;
        }
        
        case BB_2:
        {
            if (pressed_button == BIRDBOX2)
            {
                state_birdbox = BB_3;
                setLightsBirdBox(BEC3);
                timer = main_time;
            }
            else
            {
                if (pressed_button != OUT_OF_BOUNDS)
                {
                    state_birdbox = BB_1;
                }
            }
            break;
        }

        case BB_3:
        {
            if (pressed_button == BIRDBOX3)
            {
                state_birdbox = BB_4;
                setLightsBirdBox(BEC4);
                timer = main_time;
            }
            else
            {
                if (pressed_button != OUT_OF_BOUNDS)
                {
                    state_birdbox = BB_1;
                }
            }
            break;
        }
        
        case BB_4:
        {
            if (pressed_button == BIRDBOX4)
            {
                state_birdbox = BB_5;
                setLightsBirdBox(BEC5);
                timer = main_time;
            }
            else
            {
                if (pressed_button != OUT_OF_BOUNDS)
                {
                    state_birdbox = BB_1;
                }
            }
            break;
        }

        case BB_5:
        {
            if (pressed_button == BIRDBOX5)
            {
                state_birdbox = BB_6;
                setLightsBirdBox(BEC6);
                timer = main_time;
            }
            else
            {
                if (pressed_button != OUT_OF_BOUNDS)
                {
                    state_birdbox = BB_1;
                }
            }
            break;
        }
        
        case BB_6:
        {
            if (pressed_button == BIRDBOX6)
            {
                state_birdbox = BB_7;
                setLightsBirdBox(BEC7);
                timer = main_time;
            }
            else
            {
                if (pressed_button != OUT_OF_BOUNDS)
                {
                    state_birdbox = BB_1;
                }
            }
            break;
        }
        
        case BB_7:
        {
            if (pressed_button == BIRDBOX7)
            {
                ret = true;
            }
            else
            {
                if (pressed_button != OUT_OF_BOUNDS)
                {
                    state_birdbox = BB_1;
                }
            }
            break;
        }
        
        default:
            break;
        
        }// end switch

        if (main_time - timer > TIME_BETWEEN_BIRDBOX_MS)
        {
            state_birdbox = BB_1;
        }

        return ret;
    }

All the variables are correctly declared in cpp and .h files. The problem is that when I run this code the board reset its setup phase. Also, wierd is that if I remove any of the cases, the problem dissapears. What can be the cause?

P.S :

(5%) of program storage space, (20%) of dynamic memory,

Also, this is runned in main loop like this:

case Game::BIRD_BOX:
    {
        bool is_done_birdbox = BirdBox::runBirdBox(current_time);
        if (is_done_birdbox)
        {
               //some code
         }
        break;
    }

If I comment the runBirdBox function I don't have any problem. The current_time is the value of millis().

And I will also let the function used inside the case only in case there may be a problem with the variables (e.g too much memory used...)

    void setLightsBirdBox(int birdbox_number)
    {
        // Turn off all lights
        for (int i = 0; i < 7; i++)
        {
            digitalWrite(array_pini_220v[i], HIGH);  // HIGH = not activated
            delay(300);
        }

        // Bright up only what I need
        if (birdbox_number != -1)
        {
            digitalWrite(array_pini_220v[birdbox_number], LOW);
            delay(300);
        }
    }

The entire runBirdBox method looks like this:

    bool runBirdBox(unsigned long main_time)
    {
        pressed_button = getPressedButton();
        ret = false;

        switch (state_birdbox)
        {
        case BB_1:
        case BB_2:
        case BB_3:
        case BB_4:
        case BB_5:
        case BB_6:
        case BB_7:
        default:
        }

        if (main_time - timer > TIME_BETWEEN_BIRDBOX_MS)
        {
            state_birdbox = BB_1;
        }

        return ret;
    }

Please post your entire sketch. It is unlikely we can debug from code snippets.

If commenting out the "ret = true;" prevents the crash, I would look closely at the 'some code' in:

The problem was due to an array access out of bounds.

  • my array was 5 elements length
  • I was accessing array[PIN_NUMBER] (pin 20, 34, etc..) instead of array[element_number] (meaning 1,2,3,4).

Thanks for the piece of advice.

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