import lejos.nxt.*;
import lejos.util.*;
import lejos.robotics.subsumption.*;
- Code: Select all
//Behavior "Scan"
//Search for objects in vicinity. Stop when something is in sight.
public class Scan implements Behavior {
private UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S4);
private boolean suppressed = false;
private int i = 0;
private int finding;
public void suppress() {
suppressed = true;
}
public boolean takeControl() {
return(sonar.getDistance() > 25 );
}
public void action() {
suppressed = false;
if(Button.RIGHT.isDown()) {
System.exit(1);
}
Motor.A.setSpeed(180);
Motor.C.setSpeed(180);
Motor.A.forward();
Motor.C.forward();
while(!suppressed) {
if(Button.RIGHT.isDown()) {
System.exit(1);
}
Thread.yield();
LCD.drawInt(sonar.getDistance(), 5, 3);
}
Motor.A.stop();
Motor.C.stop();
System.exit(1);
}
}
*The if statements were place so that I could exit and not have to pull the battery out if the program became stuck in an infinite loop.
import lejos.nxt.*;
- Code: Select all
import lejos.util.*;
import lejos.robotics.subsumption.*;
//SwarmFind
//Meant to cycle through behaviors to help swarm gather around desirable object.
public class SwarmFind {
public static void main (String[] args) {
Behavior scanner = new Scan();
Behavior[] behaviors = {scanner};
Arbitrator arby = new Arbitrator(behaviors);
arby.start();
}
}
Called SwarmFind because this is going to be part of a bigger swarm project.
Now, I've tested it with all the ports, different wires. This program I quickly whipped up:
- Code: Select all
import lejos.nxt.*;
import lejos.util.*;
import lejos.robotics.subsumption.*;
public class testUS {
public static void main(String[] args) {
UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S4);
while(sonar.getDistance() > 25) {
LCD.drawInt(sonar.getDistance(), 5, 0);
Motor.A.forward();
Motor.C.forward();
}
Motor.A.stop();
Motor.C.stop();
System.exit(1);
}
}
works like a charm, with the correct distance and stopping and all.
So, can anyone figure out what I'm doing wrong and explain it please? Thanks.
Oh, I have lejos 0.9.1 on a Windows 7, if that's any help.
