Page 1 of 1

Ultrasonic Sensor Not Working?

Posted: 21 Aug 2011, 16:00
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?

Re: Ultrasonic Sensor Not Working?

Posted: 21 Aug 2011, 16:09
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

Re: Ultrasonic Sensor Not Working?

Posted: 21 Aug 2011, 18:11
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();
				
			}
		}		
	}	
}

Re: Ultrasonic Sensor Not Working?

Posted: 21 Aug 2011, 20:07
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

Re: Ultrasonic Sensor Not Working?

Posted: 21 Aug 2011, 20:46
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.