Problems with millis()

Hi,
I am having problems with this code. I am trying to make "smart car" library to help me with my upcomming school project. What I am trying to do is, When I am moving in certain direction, I want to check distance aswell (for distance measuring I am using HC-SR04 sensor), but something does't seem to work. If I comment out m_distance = m_sensor.GetDistance() than everything work just fine. I tested function GetDistance() and works as it should.

bool SmartCar::MoveForward_ForSetTime(const int delay_t, const float setDistance) 
{
	unisgned long timeNow = millis();
	while (millis() <= timeNow + delay_t)
	{
		//m_distance = m_sensor.GetDistance();
		//if (m_distance <= setDistance)
		//	return false;
		m_motors.moveForward(m_speed);
	}
	return true;
}

https://www.arduino.cc/en/Hacking/libraryTutorial

https://forum.arduino.cc/t/using-library-in-another-library/337306/4

The problem is not creating library.

Thank you for answer anyway.

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

1 Like

And what would be that something?

And what is the problem?

You could post your sketch and then say "I was expecting this, but instead I get that", or "getDistance() seems to be interfering with the movement of the motor and I don't know why"

Take a brief moment to read: https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/681310

This math will go wrong when the timer overflows. Use this instead:
while (millis() - timeNow <= delay_t)
Always subtract past time from the current time to get a time interval.

Note: It might make more sense to rename "timeNow" to "startTime since a moment after you set it, it is not the time "now" anymore.

1 Like

@schoolboy2 thanks to that while loop you have a disguised blocking code that may be causing havoc elsewhere. Unless there is something you know and we don't...

The while loop should be "blocking". The way I want to use this, is when I call my method MoveForward_ForSetTime(const int delay_t, const float setDistance) the motors shoud be moving in that direction for specified time, unless distance measured is less than given setDistance and in that case I want to terminate while loop.

You could print this to see why it never breaks your loop. Print the number you are comparing it to once also to see if you get a surprise there.

a7

I already did it and it's not a problem. If I comment if statement, the code still doesn't work. It looks like GetDistance() function is causing the problem in a while loop, but if I use GetDistance() function alone it works and I do not know what could be wrong with it, since it just creates a pulse with triger pin and recieve a pulse with echo pin and than covert travel time to distance and return that distance.

Time to post all your code or a small example that compiles, runs and illustrates the problem.

There is no reason that can be seen now for a function that works in one place and doesn’t work in another.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.