12 V Brushed DC motor delay with push button switch

Hello,

I posted a topic in the arduino project guidance forum found here Arduino scale & display for food dispenser & weight calculation - Project Guidance - Arduino Forum 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.0 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); 
}

Hello, Iqasi096.

From briefly looking through the posts you linked to, it sounds like you have a lot going on in your system, including communication between multiple controllers, some display, and other inputs like RFID that could be causing latency issues.

Focusing specifically on the while loop you posted about, can you clarify if the code in your last post is functioning how you want it to? If not, can you describe what you want it to do compared to what it currently does? There are some things about it that seem confusing to me, but it might just be because it is only a snippet pulled from your full code. For example, you are resetting the startTime every time the while loop finishes, so that code would probably appear to function the same way if you removed the while loop completely and just ran it as your main loop() function.

Also, do you have your button set up to return a high signal when it is pressed? If so, your IF statement will make it so the motor continues dispensing as long as you hold the button and for 5 seconds after you release it (because it will continually update startTime as long as the button is pressed).

Brandon

Hey there Brandon,

I have deduced that my dilemma stems from the dp.showSaleScreen(fin_weight); expression in my initial post. Commenting it out seems to fix the latency issue between the button and motor. To answer your questions, yes the code in my last post functions how I want it to and the button does return a high signal when pressed.

The E paper display consistently updating in the while loop causes the momentarily lag, I do however require the screen to give a live feed of my fin_weight variable, amongst other variables, with the motor driver running the motor accordingly with the push of the button.

Here is a flowchart of how the entire system should work:

And here is a chart showing how everything is connected:

Do you have any suggestions on what I can do to fix my current code/system??

Thank you,
Abe

We do not have any specific advice for reducing the latency of your E paper display. You might contact the manufacturer to see if they have any suggestions. Otherwise, you could use a dedicated controller for the display, to remove the overhead from the controller reading your button input and driving the motor.

Brandon

1 Like