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.
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"
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.
@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.
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.