12 V Brushed DC motor delay with push button switch

Hello,

I posted a topic in the project guidance forum found here https://forum.arduino.cc/index.php?topic=706271.msg4745954#msg4745954 which was for a dispenser system that uses pololu's
150:1 Metal Gearmotor 37Dx57L mm 12V (Helical Pinion) with the VNH5019 Motor Driver Carrier. I am using a Teensy 3.2 to run my system and it seems that I have some sort of delay or latency and I imagine that my issue could be in my code here:

 unsigned long startTime = millis();
         while (millis() - startTime < dispensingTimout) {   
         dp.showSaleScreen(fin_weight);
          // Toggle motor depending on whether
          // button is pressed
          if (dp.isButtonPressed()) {
            startTime = millis();
          //  Serial.println("Dispensing.");
            dp.motorOn(255);
           } else {
            dp.motorOff();
          }
       }

Where the dispensingTimout is a const int = 5000ms. When the button is pressed, I need to hold it down for the driver to register a signal and spin my motor. If I hold it, even momentarily, it will spin my motor with a delay before it turns my motor off and thus over-dispensing. Is my issue with the while loop here??

Note: Since my code is very long and is composed of three classes, I have attached a zip file of my entire code below.

Thank you!

firmware_slave.zip (7.01 KB)

Just an update, I have configured a separate code to see if the latency is still present and the delay was very little if any in the following code:

const int dispensingTimout = 5000; //in milliseconds

const int buttonPin = 5;
const int motorPin1 = 2;
const int motorPin2 = 3;
const int motorPwmPin = 23;

const bool isDebugMode = false;

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPwmPin, OUTPUT);
  Serial.begin(115200);
}

void loop() {
   unsigned long startTime = millis();
         while (millis() - startTime < dispensingTimout) {   
          // Toggle motor depending on whether
          // button is pressed
          bool returnVal = digitalRead(buttonPin);
          Serial.println(returnVal);
          if (returnVal == HIGH) {
            startTime = millis();
            Serial.println("Dispensing.");
            motorOn(255);
           } else {
            motorOff();
          }
       }
}

void motorOn(byte power)
{
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  analogWrite(motorPwmPin, power); 
}

void motorOff()
{
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  analogWrite(motorPwmPin, 0); 
}