Button not working correctly. Static bool, Potentiometer. uno

if the button is connected between the pin and 5V, what would cause it to be pulled low?

button switches are commonly connected between the pin and ground and the pin is configured as INPUT_PULLUP so that the pin will be HIGH when the button is not pressed.

the other aspect of you code is that it's not checking for a change in state and each iteration thru loop while the pin is HIGH will cause ledFlag to toggle

shouldn't Serial.begin() be called once in setup()?

isn't A1 your "button" pin?

consider


#undef MyHW
#ifdef MyHW
byte pinLeds [] = { 10, 11, 12, 13 };
const byte button = A1;

#else
byte pinLeds [] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, 17, 18, 19 };
const byte button = 15;
#endif

#define N_LED     sizeof(pinLeds)

enum { LedOff = HIGH, LedOn = LOW };
bool ledflag = false;

byte butLst;

// -----------------------------------------------------------------------------
void loop ()
{
    byte but     = digitalRead (button);

    Serial.print   (button);
    Serial.print   ("  ");
    Serial.print   (butLst);
    Serial.print   ("  ");
    Serial.println (but);

    if (butLst != but)  {
        butLst = but;
        delay (20);         // debounce

        if (LOW == but)
            ledflag = ! ledflag;
    }

    if (ledflag)  {
#if 0
        byte led = random (10, 13);
#else
        byte led = random (2, 13);
#endif
        digitalWrite (led, ! digitalRead (led));
        delay (100);
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < N_LED; n++)  {
        digitalWrite (pinLeds [n], LedOn);
        pinMode      (pinLeds [n], OUTPUT);
    }

    delay (2000);
    for (unsigned n = 0; n < N_LED; n++)
        digitalWrite (pinLeds [n], LedOff);

    pinMode (button, INPUT_PULLUP);
    butLst = digitalRead (button);
}