Parallel Parking Concept

I have an RC car with the following components:

• Arduino Uno
• 2 DC motors (1 for drive motor and one steering motor)
• L293D H Bridge
• 3 Ultrasonic sensors (HC SR04; front, side, rear)
• 9V Battery

and I want to perform a parallel parking sequence with the following concept:

1. Car is moving forward at a constant velocity parallel to any potential parking spaces
2. While the car is moving, the ultrasonic sensor on the side is constantly measuring for depth
3. Once the ultrasonic sensor detects a depth > lets say 10 cm, the car will recognize that there is a potential parking space, however, it needs to be long enough for the car to fit
4. At this point, a timer starts while the car moves forward with a constant velocity
5. While the depth > 10 cm for 4 seconds (arbitrary time but the idea is velocity x time = distance so if it’s the car is travelling at 0.1 m/s for 4 seconds, the distance is 0.4 m or 40 cm.
6. This 40 cm is longer than the car, and there is space to park, therefore, start the parallel parking sequence
7. a. Turn all the way to the right
b. Drive motor in reverse for half a second (or whatever value)
c. Wheels all way to left
etc…
• My problem is, while the millis() function is running, I want to make a copy of the time as soon as the the ultrasonic sensor detects a depth. The timer will continues as long as the depth > 10 cm.

• The timer will stop as soon as the depth < 10 cm and reset that timer to 0

• if the time is > whatever time I decide when I do the calculations, then start parallel parking.

• I’m not sure exactly how to manipulate the timer function so it saves multiple instances of time

• If someone can help me with the concept, I can definitely code it myself and will ask for feedback as necessary

Thank you so much and ask me anything if it is unclear

Each instant of interest is saved in an 'unsigned long' variable. You can calculate the time interval between any two instants by subtracting the earlier instant from the later instant. You can compare time intervals to each other and to constants. DO NOT COMPARE INSTANTS TO EACH OTHER.

To 'start a timer' you save millis() (or micros() for very short intervals) into your unsigned long variable. You do that same to 're-start a timer'.

Locks:

• My problem is, while the millis() function is running, I want to make a copy of the time as soon as the the ultrasonic sensor detects a depth. The timer will continues as long as the depth > 10 cm.
• The timer will stop as soon as the depth < 10 cm and reset that timer to 0
• if the time is > whatever time I decide when I do the calculations, then start parallel parking.

I’m not sure I understand what is troubling you. You can save the value of millis() to a variable at any time you want to.

I wonder if what you want to do is save the value of millis() when the depth <= 10 cm is detected and then keep checking the time since the depth was last found to be too small. if the time if long enough you know the space is big enough. Maybe something like this. This may seem like upside-down logic, but I think it will work

``````curDepth = xxx // whatever is needed to get a new value
if (curDepth <= 10) {
depthTooSmallMillis = millis(); // reset the clock if there is not enough depth
}
if (millis() - depthTooSmallMillis > requiredMillis) {
// space is big enough
}
``````

…R