AccelStepper ed encoder

Ciao a tutti,
sto cercando di fare funzionare un codice ma sono bloccata, perché non ho molta pratica con i motori e la loro gestione.
Ho un stepper (step per rotazione completa = 1026) che alimento esternamente a 12 volt.
Ho anche un encoder a rotazione, con pressione pulsante.

Il mio obiettivo per ora è questo:
ruotare l'encoder per settare la velocità di movimento
Premere il bottone 1 volta
ruotare l'encoder per settare per quanti secondi il motore si muoverà.
Premere il bottone una 2 volta per far partire il movimento.
Alla velocità e per il tempo fissati.
Dopo i secondi trascorsi il motore si ferma.

Uso la libreria accelStepper perché ho visto che una accelerazione graduale mi permette di avere le velocità che mi servono (60 rpm).
Mentre con altre librerie se do 60 rpm, il motore non parte.

Il codice dell'encoder mi funziona, il codice dello stepper anche (utilizzo questo codice:).

#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::HALF4WIRE, 8, 10, 9, 11);


void setup(){  
}

void loop() {
    stepper1.setMaxSpeed(1026); //513 step per second = 30 rpm (1rivoluzione impiega 2sec) // 1026 60rpm 
    stepper1.setAcceleration(100.0);
    stepper1.moveTo(10260); // durata movimento (velocità in step per secondi x n° s). Con la accellerazione iniziale il tempo non è precissimo ma va bene.
    stepper1.run();
}

Messo tutto insieme non funziona. Nello specifico:
L'encoder va. Setto la velocità
Setto i secondi.
Quando premo per la seconda volta il pulsante... i led sullo stepper driver si accendono ma il motore a volte va, a volte non va.
Quando va, va alla velocità sbagliata per un tempo sbagliato.
Molto lento, per pochissimo tempo.

Potete aiutarmi in qualche modo? :\

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>
#include <AccelStepper.h>


// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(2, 3);
//   avoid using pins with LEDs attached

// Counts number of button presses output count to serial
byte switchPin = 4;                    // switch is connected to pin 2
byte buttonPresses = 0;                // how many times the button has been pressed
byte lastPressCount = 0;               // to keep track of last press count

//stepper
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::HALF4WIRE, 8, 10, 9, 11);
float vel = 0;
int timeExp = 0;
int totalRun = 0;


void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  pinMode(switchPin, INPUT);          // Set the switch pin as input
  digitalWrite(switchPin, HIGH);      // set pullup resistor
  stepper1.setAcceleration(100.0);


}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition / 4); // 4 step encoding, divided by 4 to have 1 point increment
  }

  if (digitalRead(switchPin) == LOW)  // check if button was pressed
  {
    buttonPresses++;                  // increment buttonPresses count
    delay(250);                       // debounce switch
  }

  if (buttonPresses == 0) {           // to set for the speed for the stepper
    vel = (newPosition / 4) * 102.6;  // stepper has 1026 step for 1 turn.  I use 102.6 as smaller incremental. If I set 102 as speed it takes 10 seconds for a complete turn.
    Serial.println(vel);
  }

  if (buttonPresses == 1) {         // to set for how long the motor has to run
    timeExp = (newPosition / 4);      // s the motor has to run
    Serial.println(timeExp);
    totalRun = timeExp * vel;          // if my speed is stepXseconds my totalRun time for the moveTo fx is speed * seconds
    Serial.println(totalRun);

  }

  if (buttonPresses == 2) {
    stepper1.setMaxSpeed(vel);   //513 step per second = 30 rpm (1revolution 2sec) // 1026 60rpm
    stepper1.moveTo(totalRun);   // if my speed is stepXseconds my totalRun time for the moveTo fx is speed * seconds
    buttonPresses++;             // increment buttonPresses count
    //stepper1.run();
   
  }

  if (buttonPresses == 3) //Serial.println("movimento in corso");
  
  stepper1.run();

  if (buttonPresses == 4) { // rollover every fourth press
    buttonPresses = 0; 
    vel = 0;
    totalRun = 0;
    timeExp = 0;
  }    

  if (lastPressCount != buttonPresses)               // only do output if the count has changed
  {
    Serial.print ("Button press count = ");          // out to serial
    Serial.println(buttonPresses, DEC);
  }
  lastPressCount = buttonPresses;    // track last press count

}