Commit 7e840802 authored by Mikkel's avatar Mikkel
Browse files

Added all programs for lesson 3

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.lejos.nxt.ldt.LEJOS_LIBRARY_CONTAINER/nxt"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lesson 3 - Exercise 1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.lejos.nxt.ldt.leJOSBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.lejos.nxt.ldt.leJOSNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
import lejos.nxt.*;
import java.io.*;
/**
* A simple data logger that can be used to sample a sequence of integer data
* values in a flash file. The file is a text consisting of a sequence of lines
* with a time value and a sample data value separated by a comma.
*
* When data has been sampled the flash file can be transfered to a PC by means
* of the tool nxjbrowse.
*
* @author Ole Caprani
* @version 4.02.13
*/
public class DataLogger {
private File f;
private FileOutputStream fos;
private int startTime;
public DataLogger(String fileName) {
try {
f = new File(fileName);
if (!f.exists()) {
f.createNewFile();
} else {
f.delete();
f.createNewFile();
}
fos = new FileOutputStream(f);
} catch (IOException e) {
LCD.drawString(e.getMessage(), 0, 0);
System.exit(0);
}
startTime = (int) System.currentTimeMillis();
}
public void start() {
startTime = (int) System.currentTimeMillis();
}
public void writeSample(int time, int sample) {
try {
Integer timeInt = new Integer(time);
String timeString = timeInt.toString();
Integer sampleInt = new Integer(sample);
String sampleString = sampleInt.toString();
for (int i = 0; i < timeString.length(); i++) {
fos.write((byte) timeString.charAt(i));
}
// Separate items with tab
fos.write((byte) ('\t'));
for (int i = 0; i < sampleString.length(); i++) {
fos.write((byte) sampleString.charAt(i));
}
// New line
fos.write((byte) ('\r'));
fos.write((byte) ('\n'));
} catch (IOException e) {
LCD.drawString(e.getMessage(), 0, 0);
System.exit(0);
}
}
public void writeSample(int sample) {
writeSample((int) System.currentTimeMillis() - startTime, sample);
}
public void close() {
try {
fos.close();
} catch (IOException e) {
LCD.drawString(e.getMessage(), 0, 0);
System.exit(0);
}
}
}
import lejos.nxt.*;
/**
* A simple sound sensor sampling program that samples the sensor with a fixed
* sample interval and store the values in a flash file "Sample.txt".
*
* The sound sensor should be connected to port 1.
*
* @author Ole Caprani
* @version 2.09.08
*/
public class SoundSampling {
public static void main(String[] args) throws Exception {
SoundSensor s = new SoundSensor(SensorPort.S1);
DataLogger dl = new DataLogger("Sample.txt");
int soundLevel;
LCD.drawString("Level: ", 0, 0);
while (!Button.ESCAPE.isDown()) {
soundLevel = s.readValue();
LCD.drawInt(soundLevel, 3, 7, 0);
dl.writeSample(soundLevel);
Thread.sleep(5);
}
dl.close();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
Thread.sleep(2000);
}
}
import lejos.nxt.*;
/**
* A simple sonar sensor test program.
*
* The sensor should be connected to port 1. In the known bugs and limitations
* of leJOS NXJ version alfa_03 it is mentioned that a gap of at least 300 msec
* is needed between calls of getDistance. This is the reason for the delay of
* 300 msec between sonar readings in the loop.
*
* @author Ole Caprani
* @version 30.08.07
*/
public class SoundSensorTest {
public static void main(String[] args) throws Exception {
DataLogger dl = new DataLogger("Sound_3,0m.txt");
SensorPort port = SensorPort.S1;
//port.setMode(SensorPort.MODE_RAW);
port.setTypeAndMode(SensorConstants.TYPE_SOUND_DB,
SensorConstants.MODE_PCTFULLSCALE);
//SoundSensor us = new SoundSensor(SensorPort.S1);
LCD.drawString("Sound(%) ", 0, 0);
LCD.drawString("Sound(raw) ", 0, 2);
while (!Button.ESCAPE.isDown()) {
//LCD.drawInt(us.readValue(), 3, 13, 0);
LCD.drawInt(port.readRawValue(), 3, 13, 2);
dl.writeSample(port.readRawValue());
Thread.sleep(5);
}
dl.close();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
Thread.sleep(2000);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.lejos.nxt.ldt.LEJOS_LIBRARY_CONTAINER/nxt"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lesson 3 - Exercise 3</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.lejos.nxt.ldt.leJOSBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.lejos.nxt.ldt.leJOSNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment