Problem with Sharp GP2D12 distance sensor

This is where you talk about the RCX software itself, installation issues, and programming talk.

Moderator: roger

Problem with Sharp GP2D12 distance sensor

Postby mathijsie » Sun Jul 19, 2009 12:16 pm

Hello,

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.
mathijsie
New User
 
Posts: 1
Joined: Sun Jul 19, 2009 12:11 pm

Postby bnl » Mon Jul 27, 2009 11:55 am

your code seems to be based on the nqc code from
http://www.philohome.com/sensors/gp2d12.htm

did you also do the hardware stuff? like adding capacitor?

/Björn
bnl
New User
 
Posts: 7
Joined: Sun Mar 29, 2009 6:55 pm
Location: Sweden


Return to RCX Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff