One sound sensor program
import lejos.nxt.*;
public class Bot1 {
private SoundSensor soundSensor;
public Bot1() {
soundSensor = new SoundSensor(SensorPort.S1, true);
}
public static void main(String[] args) {
Bot1 bot = new Bot1();
bot.botControl();
}
public void botControl() {
while (!Button.ESCAPE.isPressed()) {
int power = normalizeSound();
MotorPort.B.controlMotor(power, 1);
MotorPort.C.controlMotor(power, 1);
}
}
public int normalizeSound() {
int input = soundSensor.readValue();
int output = 55 + (input * 45) / 100;
return output;
}
}
Bot2a - Two light sensors with the bot moving away from the light
import lejos.nxt.*;
public class Bot2a {
private LightSensor ls1;
private LightSensor ls2;
//private int MIN;
//private int MAX;
public Bot2a() {
ls1 = new LightSensor(SensorPort.S1, true);
ls2 = new LightSensor(SensorPort.S2, true);
}
public static void main(String[] args) {
Bot2a bot = new Bot2a();
//bot.measureMIN_MAX();
bot.botControl();
}
public void botControl() {
while (!Button.ESCAPE.isPressed()) {
int power1 = normalize(ls1.readNormalizedValue());
int power2 = normalize(ls2.readNormalizedValue());
power1 = power1 + 15;
power2 = power2 + 25;
LCD.drawInt(power1, 0, 0);
LCD.drawInt(power2, 0, 2);
LCD.refresh();
MotorPort.C.controlMotor(power1, 1);
MotorPort.B.controlMotor(power2, 1);
}
}
public int normalize(int input) {
int MIN_VALUE = 400;
int MAX_VALUE = 800;
int output = ((input - MIN_VALUE) * 100) / (MAX_VALUE - MIN_VALUE);
return output;
}
}
No comments:
Post a Comment