sketch only executes when button is held, not pushed.

hey everyone, I'm trying to execute this command for acceleration of a motor when a button is pushed, but it only seems to work if i hold down the button, rather than just an inturrupt. Any thoughts on this.l

#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>

int buttonApin = 2;

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers



Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);


// wrappers 
void forwardstep2() {  
  myStepper2->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, DOUBLE);
}

// Accel wrapper
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()
{  
  AFMStop.begin(); // Start the bottom shield
  pinMode(buttonApin, INPUT_PULLUP);  

    
  stepper2.setMaxSpeed(500.0);
  stepper2.setAcceleration(50);
  stepper2.moveTo(500);

}

void loop()
{
if (digitalRead(buttonApin) == LOW)
    {
  stepper2.run();
  
    }
 
}

It's only running when you hold the button because that's what you're asking it to do:

void loop()
{
if (digitalRead(buttonApin) == LOW)

...

If you want to do something on the falling edge of an input, use an interrupt. Something like:

void setup()
{  
  AFMStop.begin(); // Start the bottom shield
  pinMode(buttonApin, INPUT_PULLUP);  
  attachInterrupt(digitalPinToInterrupt(buttonApin), RunStepper, FALLING);
    
  stepper2.setMaxSpeed(500.0);
  stepper2.setAcceleration(50);
  stepper2.moveTo(500);

}


void loop() {
  //interrupt handler takes care of falling edges being detected
  
}

void RunStepper() {
  stepper2.run();
}

Do you want the stepper to run when the button becomes pressed rather than when it is pressed ? If so then look at the StateChangeDetction example in the IDE

When should the stepper stop ?

BlackFin, i tried running your code and I wind up getting nothing happening. no errors, just no actions. I THOUGHT it would behave the same as when i run this code. The difference being the " "stepper2.moveTo(500);"

If I remove the "if" statement, the stepper accelerates up and then down for 500 steps with the origional code posted.

UKHeliBob - ill check out the state change thanks. im hoping to just set a number of steps, say 500 then have it stop, which i thought would happen with the setup logic.

#include <Wire.h>
#include <Adafruit_MotorShield.h>


Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);



//Naming the buttons
int buttonApin = 2;
int buttonBpin = 3;
int ledPin = 6;


void setup() 
{
  myMotor->setSpeed(50); 
  AFMS.begin(1600);  

  
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  



  
 
}

void loop() 
{
   int sensorVal = digitalRead(2);
    myMotor->release();
  { 
    if (digitalRead(buttonApin) == LOW)
    {
    digitalWrite(ledPin, HIGH);
   myMotor->step(100, FORWARD, DOUBLE);
   digitalWrite(ledPin, LOW);
   delay(500);
    }
    
  }
}

Which platform are you using? Check this page:

to determine if your board supports interrupts on the digital input(s) you're using.

Im running an UNO with an adafruit motorshiled V2