LightSensor Listener

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc

Moderators: roger, 99jonathan, imaqine

LightSensor Listener

Postby McJul » Sun Jan 01, 2012 8:09 pm

Hi,
I'm using lejos version 9.0 beta, and nxt 2.0.
I want to use a Listener for the LightSensor but it doesn't work :(
The LightListener is never called and the light of the LightSensor isn't even turned on.
But if I use a Listener for the TouchSensor there are no Problems. What is wrong on my exampel program???

Code: Select all
import lejos.nxt.*;

public class TestProgramm {
   
  public static void main(String[] args){ 
     TestProgramm a = new TestProgramm ();
     a.go();
  }

  public void go (){
     MyLightListener lightListener = new MyLightListener ();
     MyTouchListener touchListener = new MyTouchListener ();
    
     LightSensor light = new LightSensor (SensorPort.S1);
     TouchSensor touch = new TouchSensor (SensorPort.S2);
    
     SensorPort.S1.addSensorPortListener(lightListener);
     SensorPort.S2.addSensorPortListener(touchListener);
    
     LCD.drawString("LightListener:", 0, 1);
     LCD.drawString("TouchListener:", 0, 4);
    
     Button.waitForPress();
  }
 
  class MyLightListener implements SensorPortListener {
   public void stateChanged(SensorPort source, int oldValue, int newValue) {
      LCD.drawString("alt: "+ oldValue + "   neu: " + newValue, 0, 2);
   } 
  }
 
  class MyTouchListener implements SensorPortListener {
     public void stateChanged (SensorPort source, int oldValue, int newValue){
        LCD.drawString("alt: " + oldValue + "   neu: " + newValue, 0, 5);
     }
  }
 
}
McJul
New User
 
Posts: 3
Joined: Sun Jan 01, 2012 7:53 pm

Re: LightSensor Listener

Postby gloomyandy » Sun Jan 01, 2012 8:47 pm

Hi,
Several issues here. First off if you have a NXT 2.0 set you have a color sensor not a light sensor. Secondly sensor listeners don't work with the color sensor and in general they are not a good idea (search this forum for many posts by me on the subject of why sensor listeners are a bad thing),they are basically left over from the RCX version of leJOS. Rather than use a listener, just create a thread that interfaces to the sensor and performs whatever actions you require...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3012
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: LightSensor Listener

Postby McJul » Sun Jan 01, 2012 9:31 pm

Thanks for this information.
I didn't know that the ColorSensor doesn't work with a listener. I thought it would be much easier with a listener. That's why I didn't use a thread.
I will try it :)
McJul
New User
 
Posts: 3
Joined: Sun Jan 01, 2012 7:53 pm


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff