how to measure time it takes for a DC motor to reach top speed?

I have a project that involves regulating the speed of a DC motor and for some measurements I need to know the time it takes for the motor to reach a specific speed, (ex. time taken to get from 1000RPM to 5000RPM) I already have the code to calculate the RPM, now I need more code that prints out the time, preferably using micros(), any help is welcome.. :slight_smile:

this is the loop part that works for me for reading the rpm using a Slot-type Optocoupler Module:

void loop(){

lastReading = reading;
reading = digitalRead(sensor);
if (reading == LOW && lastReading == HIGH)
{
stateChangeTime = micros();
duration = stateChangeTime - previousStateChangeTime;
previousStateChangeTime = stateChangeTime;
count++;
}
if (micros() - stateChangeTime >= 1000000)
{
RPM = 0;
}
else
{
RPM= 60000000/duration;
}

Measure deltas of say, RPM.

When the deltas stop growing, you've reached maximum speed.

Please remember to use code tags when posting code

DinkoDude112:
I need to know the time it takes for the motor to reach a specific speed, (ex. time taken to get from 1000RPM to 5000RPM)

When the RPMs reach 1000, store the value of micros() in a global variable:

startTimestamp = micros();

When the RPMs reach 5000, the elapsed time is micros() - startTimestamp.

I'm new to this, so I have to ask.. How do I define the speed limits in the code( RPM= 1000 and RPM=5000) so that is starts to count and stop? Do you have an example of a code in which it is used for this puropose?

DinkoDude112:
How do I define the speed limits in the code( RPM= 1000 and RPM=5000)

Use an if statement:
https://www.arduino.cc/reference/en/language/structure/control-structure/if/

It's possible that RPM will go from a value below the target speed to a value above the target speed without hitting the actual target speed so you should use >= in your if statement's Boolean test instead of ==. This means you will need to use a global flag variable to determine when the timestamp has been set so that you don't keep setting the timestamp over and over again:

timestampSet = true;

DinkoDude112:
Do you have an example of a code in which it is used for this puropose?

I could write the code for you, but I think you'll learn more by writing it for yourself. From the code you've already written, I can see that you're quite capable of doing so. So give it a try and if you get stuck, come back with your complete code (please use code tags to post it this time, as explained in the forum rules) and a detailed description of the problem you're having.