I'm trying to get my Sharp GP2D12 distance sensor working on my RCX 2.0 brick. However, I don't seem to get it working.
I tried two completely different codes, one I found on the internet, the other one I've wrote myself. Both give more or less the same result: the sensor's value is slowly increasing until it reaches 1023. Once it reaches 1023 the value remains the same.
This is the code I wrote myself:
- Code: Select all
/**
*
*/
import josx.platform.rcx.*;
/**
* @author Mathijs
*
*/
public class proximityafstand {
public static void main (String[] args)
throws Exception
{
while(true)
{
Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_RAW);
Sensor.S1.activate();
Thread.sleep(250);
Sensor.S1.passivate();
Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_RAW);
//Sensor.S1.activate();
Thread.sleep(50);
Sensor.S1.passivate();
Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_RAW, SensorConstants.SENSOR_MODE_RAW);
int value = Sensor.S1.readValue();
LCD.showNumber(value);
}
}
}
And I found this one on the internet:
- Code: Select all
import josx.platform.rcx.*;
public class DistanceTest implements SensorConstants {
private Sensor sharpSensor;
public DistanceTest(Sensor sharpPort) {
sharpSensor = sharpPort;
sharpSensor.setTypeAndMode(SENSOR_TYPE_LIGHT,
SENSOR_MODE_RAW);
sharpSensor.activate();
// Initial charge:
try{Thread.sleep(1000);}catch(Exception e){}
}
public synchronized int getRaw() {
// Charge capacitor:
sharpSensor.activate();
try{Thread.sleep(250);}catch(Exception e){}
// Read value:
sharpSensor.passivate();
try{Thread.sleep(50);}catch(Exception e){}
int raw = Sensor.S1.readValue();
return raw;
}
public static void main(String [] args) {
DistanceTest dt = new DistanceTest(Sensor.S1);
while(true) {
LCD.showNumber(dt.getRaw());
// Sound.beep();
}
}
}
Can someone please help me getting the sensor to work?
Thanks in advance.
