I am new to java and lejos so please don't blame me if i'm asking rather dumb questions.
I was trying to let the lego ultrasonic sensor scan a 360 degree area around my nxt by rotating it on a motor. Every 5 degrees it saves the distance to a .txt file.
My problem is that later when I read the file from my PC after uploading it with nxjbrowse.bat, it only contains the ASCII characters linked to the numbers that supposed to be saved there (0 - 255).
My code for the NXT:
package Dataloggers;
import java.io.;
import lejos.nxt.;
public class USdistance {
int totalRotation = 360;
int scanDensity = 5;
UltrasonicSensor ultrasonicSensor = new UltrasonicSensor(SensorPort.S3);
File distanceFile = new File("Distances.txt");
FileOutputStream fileStream = null;
public static void main(String[] args) {
@SuppressWarnings("unused")
USdistance usD = new USdistance();
}
public USdistance() {
Motor.A.setAcceleration(2000);
Motor.A.setSpeed(50);
try {
fileStream = new FileOutputStream(distanceFile);
} catch (Exception e) {
LCD.drawString("Can't make a file", 0, 0);
Button.ESCAPE.waitForPress();
System.exit(1);
}
DataOutputStream dataStream = new DataOutputStream(fileStream);
Motor.A.rotate(90);
Motor.A.resetTachoCount();
Motor.A.backward();
do {
if (-(Motor.A.getTachoCount()) % scanDensity == 0 ) {
int distance = ultrasonicSensor.getDistance();
try {
dataStream.writeInt(distance);
fileStream.flush();
} catch (IOException e) {
LCD.drawString("Can't write to the file", 0, 1);
Button.ESCAPE.waitForPress();
System.exit(1);
}
}
} while (-(Motor.A.getTachoCount()) < totalRotation);
Motor.A.stop();
try {
fileStream.close();
} catch (IOException e) {
LCD.drawString("Can't save the file", 0, 1);
Button.ESCAPE.waitForPress();
System.exit(1);
}
Motor.A.rotate(270);
}
}
Thanks in advance Rob
I solved my problem by using:
int distance = ultrasonicSensor.getDistance();
BufferedOutputStream buffStream = new BufferedOutputStream(new FileOutputStream(new File(distances.txt)));
buffStream.write(distance.getBytes());
By using this my text file looks like this: 41 41 0 0 0 0 0 0 40 40 40 40 40 39 39 39 39 39 39 40 40 40 40 instead of 㔵㈀㔵㈀㔵㈀㔵㈀㔵㐀5㐴㐀3㌴㐀4㐴㌀6㌳㌀3㈵㔀2㈵㔀2㈵㔀2㈵㔀2㌵㔀3㌵㈀㔵㈀㔵㈀㔵㈀
Anyway thanks for your replies @SimpleCoder