SensorListeners again

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

Moderator: roger

SensorListeners again

Postby AES111 » Mon May 19, 2008 10:41 am

Hi,
i have a problem with a small Lejos app. i've wrote..

The programm should:
1) move forward until he hits a wall.
2) Then he should go backward and turn right.
3) back to 1)

I have 2 issues with this.. First he is doing 2) twice.. I've tryed around but can't figure out wheres the problem..

2nd problem is the loop. I have no idea how to realise it, so I've done some provisorily: (try {Thread.sleep(800000);} )...

Heres the code:

Code: Select all
import josx.platform.rcx.*;
       class berührung
{
        public static void main(String args[])
        {
           Sensor.S1.setTypeAndMode(1,0x20);
           Sensor.S1.activate();
           Sensor.S3.setTypeAndMode(1,0x20);
           Sensor.S3.activate();
           Motor.A.forward(); Motor.C.forward();

           Sensor.S1.addSensorListener(new SensorListener()
           {
             public void stateChanged (Sensor S1, int oldValue, int newValue)
             {
              Motor.A.backward(); Motor.C.backward();
              try {Thread.sleep(750);} catch (Exception e) {}
              Motor.A.forward(); Motor.C.stop();
              try {Thread.sleep(400);} catch (Exception e) {}
              Motor.A.forward(); Motor.C.forward();
             }

            });
           
            Sensor.S3.addSensorListener(new SensorListener()
           {
             public void stateChanged (Sensor S3, int oldValue, int newValue)
             {
              Motor.A.backward(); Motor.C.backward();
              try {Thread.sleep(750);} catch (Exception e) {}
              Motor.C.forward(); Motor.A.stop();
              try {Thread.sleep(400);} catch (Exception e) {}
              Motor.A.forward(); Motor.C.forward();
             }
            });
             try {Thread.sleep(80000);} catch (Exception e) {}
            }
            }



Thx for your help..
(copied a bit from an other post because my english is not so well :twisted: )
AES111
New User
 
Posts: 1
Joined: Mon May 19, 2008 10:33 am

Postby ltj » Mon May 19, 2008 12:52 pm

touch sensor generets two events form 0 to 1 (turns on) AND form 1 to 0 (turns off) sensor listener catch thoes 2 events and do thing twice, add something like this:
Code: Select all
             public void stateChanged (Sensor S1, int oldValue, int newValue)
             {
                  if (newValue == 1) {
                    Motor.A.backward(); Motor.C.backward();
                    try {Thread.sleep(750);} catch (Exception e) {}
                    Motor.A.forward(); Motor.C.stop();
                    try {Thread.sleep(400);} catch (Exception e) {}
                    Motor.A.forward(); Motor.C.forward();
                  }
             }


to both Listeners and it should be fine.

Instead of
Code: Select all
 try {Thread.sleep(80000);} catch (Exception e) {}
add
Code: Select all
 Button.RUN.waitForPressAndRelease();
so RCX will wait for RUN button to be pressed
ltj
New User
 
Posts: 4
Joined: Thu Apr 24, 2008 7:01 pm


Return to RCX Software

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff