Continuous push button detection in for Stepper control
I implemented your code here on a UNO using 2 pushbuttons (with the stepper motor functions commented out) and it seems to work fine.  When I press button1 I get a lot of &quot;ok&quot; printouts for as long as the button is held down. When I press button2, the same thing happens except with &quot;ok2&quot; instead of &quot;ok&quot;.
Here's the code I used:
/*
    Name:       TwoPushButtonTest.ino
    Created:    9/28/2020 2:36:07 PM
    Author:     FRANKNEWXPS15\Frank
*/



//#include &lt;AccelStepper.h&gt;

const int button1Pin = A2;
const int button2Pin = A1;

int button1State = 0;
int button2State = 0;

boolean flag1 = 0;
boolean flag2 = 0;

//AccelStepper stepper = AccelStepper(1, 8, 9);
void setup()
{
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);

  Serial.begin(115200);
}

void loop()
{

  //int sensorReading = analogRead(A0);
  //int motorSpeed = map(sensorReading, 0, 1023, 500, 1000);
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  //stepper.setMaxSpeed(2000);
  //stepper.setSpeed(motorSpeed);

  if (button1State == HIGH)
  {

    Serial.print(&quot;ok&quot;);
    //stepper.setSpeed(motorSpeed);
    //stepper.runSpeed();


  }

  if (button2State == HIGH)
  {

    Serial.print(&quot;ok2&quot;);
    //stepper.setSpeed(-motorSpeed);
    //stepper.runSpeed();

  }
}

and here's the wiring diagram:

Note the two 10K pull-down resistors.  If these are not present, then the A2/A3 inputs are indeterminate whenever the buttons are in the 'disconnected' state, and anything can happen.