Distance sensor code not working


#1

Hi, I would like to teach my class the following Bloxter code, but in my test runs it’s not working. The distance sensor is working, I can see the correct readings in the sidebar. But When I run the code, it doesn’t stop when it sees an obstacle, it pushes right through it and only stops when it gets to all black. What am I doing wrong? Note: The image shows < 1cm, but I actually tried many different distances… nothing worked. Thank you!


#2

Hello @penny.gray

The code as is won’t work. Are you trying to follow the line until it gets to all black or alternatively the robot should stop if there’s an obstacle?
The following code would work:
In short you’re telling the robot to follow the line until one of two conditions happens.
Either the line sensor reports all black, or the distance sensor reports an obstacle.


#3

I’d like to do both. I got the project idea from a Dexter studio project called “self driving car” - the only difference is that I don’t use functions in my code. Here’s the project, it’s step
6.


#4

Thank you, I will try that. Did you see the dexter studio project link I posted? Can you explain why the code would work in that project, and not for me?


#5

The code in the project doesn’t quite do what you think it does.

The robot waits for the road to be clear, then it starts. Should an obstacle appear while it’s moving, it would not detect it and wouldn’t react to the obstacle.

You’ll notice the distance test in the project is something like 20 inches, a lot more than 1 cm. The robot checks if the whole road ahead is clear, then it commits itself.


#6

That makes sense, but that didn’t happen in my test runs. I actually had the distance set to much higher <= 30 cm with the robot only about 20 cm away from the obstacle. But it still started driving even when there was an obstacle. If it worked as it should, you’re saying it shouldn’t have even started?