0

My issue is maybe simple but I haven't been able to figure it out. I am using an arduino uno, seeedstudio motor shield v2, and a wheel encoder. The issue is, the example code for this shield looks like this:

void loop()
{
motor.speed(0, 100);            // set motor0 to speed 100
delay(1000);
motor.brake(0);                 // brake
delay(1000);
motor.speed(0, -100);           // set motor0 to speed -100
delay(1000);
motor.stop(0);                  // stop
delay(1000);
}

But delays are blocking and I can seem to count encoder pulses because of this (presumably). I have tried different implementations of millis() but the encoder reads wild numbers in the 10s of thousands/ negative.

1 Answers1

0

There is no wheel encoder in your code, but I assume your problem is that delay is a blocking function.

The easiest way to to check if a time has elapsed without blocking the program is to manually check if the time has elapsed since your last instruction. As an example:

void loop()
{
    unsigned long previous_time = millis();
    motor.speed(0, 100);  
    while (millis() - previous_time < 1000) 
    {
        // do something (check the encoder for instance
    }
}

This way, you'll be able to perform other instructions while you are waiting for the delay to complete. Of course you can put this check in a function.

Hope it helps!

Tom
  • 303
  • 3
  • 14