Fail to work when using ultrasound and line sensor together


#1

Hi there,
I am trying to extend the line sensor program to include the ultrasound distance detector. I force the car to stop when the ultrasound read something too close ahead. The code is as follow

If I use the “When FLAG clicked” event trigger, it works: I mean the car wheel stop when I put my hand close to the ultrasound sensor, I also try to show the ultrasound reading, it changes when I place obstacle at different distance. However, the same code when I replace the “When FLAG clicked” with mBot program and burn it to the mBot in Arduno mode. The ultrasound sensor never works. The car keeps going and never stop even I have my hand in front of the car in 5 cm. If it even odd, the car keep moving forward, seems that the line follower doesn’t work either.

p.s. if I place my hand to block the ultrasound sensor before my turning the power on, after the power is connected, the car wheel stop until I put my hand away. Very strange.


#2

If you read from the ultrasonic sensor very quickly, it might return 0, especially if the bot is moving.
So you might try to wait a bit (maybe 0.1 seconds) if you read 0 and repeat.


#3

Thanks for the response. Yes, adding a condition to see if it is greater than zero or not. But it still doesn’t work … I finally figure out that the wait 1 secs cause the problem. After removing that, both ultrasonic and line follower works together.


#4

You might want to be aware that a reading of zero on the ultrasonic sensor equates to nothing detected in the maximum range of the sensor (per hardware spec). I would strongly suggest that you solve one problem at a time by using the smallest amount of code to debug the problem. In this case, I would suggest removing the line following bits and focusing on the ultrasonic-controlled movement.

For example, the following program will start and stop based on the Ultrasonic sensor reading:

Note that instead of testing to see if an object is detected within the specified distance (13cm), I’m testing to see if there is not an object within that distance. This avoids the need to add an extra test for a zero (which indicates that there is no object within range of the sensor) but provides the same outcome.

I might also suggest isolating the line follower sensor tests into individual if-then blocks. You really don’t save anything by using nested if-then-else blocks (in my opinion) in this case because functionally if the if test fails it will fall through to the next if.

On other thing to note is that setting the motors to the same values can be done by using the [run forward] at [speed] block with a speed of zero. You can, of course, set the motors individually but you don’t have to. :slight_smile:

Regards,

Chuck


#5