Distance sensor code not working

I am using a Gen2 distance sensor with a Gen1 iq brain. I created the following code (when My hand moves closer the motor should stop) but nothing happens.
image
But, when I change the less than to greater than and I hold my hand within 10 inches and move my had further away, the motor stops.
What am I doing wrong? I want the code to stop the motor when an object is less than 10 inches
Thanks much
Wayne

Add another when started that has forever prints the distance reading, so your code looks like:

when started:
  forever:
    print(Distance12.distance(inches))

when started:
  motor1.spin(forward)
  waitUntil(Distance12.distance(inches) < 10)
  motor1.stop()

Then you can see what the brain sees, and it might reveal useful information. It’s called debugging.

You will want to clear the screen, of course, in the forever loop before you print.

Why would they be meaningless?

1 Like

Remember to add a line of code that goes to a new line! Also, you can add a wait block to fit how often you want the readings. Otherwise, they become meaningless.

If you print a value every, say, 10 milliseconds, and the brain has about 50 rows, you can only fit half a second of data. So, given the nature of the readings, wouldn’t it be more appropriate to have a longer wait because that shows the meta trend. Having no wait, or the default which is quite small won’t show you where the problem is

1 Like

Well, I usually do

while (true) {
  Brain.Screen.clearscreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Distance inches: ");
  Brain.Screen.print(Distance12.distance(inches));
}

So then the more often the better, because you’re looking at current data.

2 Likes

How is this relevant to the persons question? Also they are using blocks so they might not understand the written code.

Just theorizing here… but since “drive forward” is not a blocking command, and the get distance command isn’t blocking either… the initial value of the distance sensor may be “0” until it gets a chance to get an initial reading from the sensor.

Basically, the code may be running too fast and finding a default value. Try adding a small wait (maybe 0.1 seconds) to the top of the program to see if that helps?

4 Likes

They are saying, basically, “The distance sensor works when I use > but not when I use <. Why?” And I don’t know why the distance sensor would not register their hand as within 10 inches and stop the motor, so I suggest he print the distance sensor’s reading to the brain constantly so maybe he can diagnose the problem better. That code can be used to print the distance sensor’s reading to the brain, so I supplied him with it.

Not in retaliation, and I do know this does not prove my post was helpful, how does this image
help them in any way? You think the problem is with the waitUntil block? Then why would the waitUntil block work when he uses >? But you didn’t explain why this would work or anything, you just gave him free code to copy, without learning anything.

If he doesn’t, I’ll be happy to rewrite it in block-er code, but I think at least reading and translating code to blocks would help him understand the code he’s using. I don’t want to give him ready-made code to mindlessly copy, since

  1. he wouldn’t learn anything,
  2. if he copies it wrong, it won’t work and he won’t be able to fix it, and
  3. he won’t know what the purpose of it is.
2 Likes

Thanks,
I tried your block code but it still doesn’t work. It does work with a greater than but not with less than.

Thanks,
I did try this block code but it only works with a greater than. I also tried the template in Vexcode called “Distance Sensor” and I have the same problem

Can you show us where your distance sensor is?

Ok, I just tried it with a Gen2 brain and it works. It does not work on the Gen 1 brain.
Has anyone figured out how to use the gen2 range sensor with the gen1 brain? VEX website says it’s compatible

There’s a few legacy things with the 2nd Gen sensor on a 1st Gen Brain. The dev team tested these this afternoon

  • The robot will require approximately 0.5 seconds in order to initialize the sensor and get a value to return. During this 0.5 second window, the value returned will be 0.
  • If the distance sensor doesn’t detect an object, the sensor will return the value 0. On other platforms (IQ Gen 2 / EXP / V5), a “no echo” returns a maximum distance rather than 0. However due to IQ Gen 1 being an older platform, it does not have this improvement.

You can add the 0.5 second delay to your project at the beginning to work around the first issue, but the second issue will be present at all times.

3 Likes

Isn’t there a (object detected) block or something they can use or is it still the same thing as you said.

The sensor has no data to return until it starts returning a non-zero value (approximately 0.5 seconds after project start)

1 Like

Thanks everyone for your help. I did add the .5 delay and that works.