Ultrasonic Sensor Not Working?

Discussion specific to the intelligent brick, sensors, motors, and more.
Post Reply
tomvb96
Posts: 4
Joined: 01 Aug 2011, 19:05

Ultrasonic Sensor Not Working?

Post by tomvb96 »

I'm trying to make a simple robot that drives around a room without running in to obstacles by using its ultrasonic sensor but the sensor is giving me completely random readings. When I have it only print out its readings on the LCD screen it seems to work well but when I use it in my program the robot will do anything from run straight into a wall to stop 4ft in front of it. Is there anything wrong with my sensor and if so, what can I do?
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Ultrasonic Sensor Not Working?

Post by mightor »

Hi there,

It might help if you gave us a look at your program. Maybe it's something unrelated to the sensor that is causing this erratic behaviour :)

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
tomvb96
Posts: 4
Joined: 01 Aug 2011, 19:05

Re: Ultrasonic Sensor Not Working?

Post by tomvb96 »

I thought of that but my code seemed pretty straight forward. Here it is though, maybe I missed something...

Code: Select all

package rover;

import lejos.nxt.*;
import lejos.robotics.navigation.*;

public class Rover {
	static UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
	static DifferentialPilot sc = new DifferentialPilot(5.6F, 13F, Motor.C, Motor.A, true);
	/**
	 * @param args
	 */		
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		sc.setTravelSpeed(700);
		sc.backward();
		
		while(!Button.ESCAPE.isPressed()){
			if (us.getDistance() < 45){
				sc.stop();
				
			}
		}		
	}	
}
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Ultrasonic Sensor Not Working?

Post by mightor »

I wrapped your code in some code tags like this:

Code: Select all

[code]
your code
[/code]
You should do that next time you paste it into a post, makes a lot more readable :)

As for your code, I am not all that familiar with Lejos, but you're right, there doesn't seem to be anything really weird there. What are you bouncing the US off? Is it a hard or soft surface?

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
tomvb96
Posts: 4
Joined: 01 Aug 2011, 19:05

Re: Ultrasonic Sensor Not Working?

Post by tomvb96 »

Ok I'll try that with the code from now on, thanks.
At first I was just testing using my hand in front of the sensor but I also tried running it up to some hard wood furniture and they both would give the same random results.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest