Compass Sensor Problem - "509"

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

Moderators: 99jonathan, roger, imaqine

Compass Sensor Problem - "509"

Postby saprager » Tue Nov 13, 2007 4:53 pm

I created a simple program to output the value from the compass sensor to the screen, however, the LCD consistantly reads 509, and no other number. The code compiles and downloads fine. Help please

public class Tester {
public static void main(String[] args){

CompassSensor compass_CS = new CompassSensor(SensorPort.S4);

for(int i = 0; i < 20; i++){
LCD.drawInt((int)compass_CS.getDegrees(), 2, 2);
LCD.refresh();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
}
saprager
New User
 
Posts: 2
Joined: Thu Oct 25, 2007 11:16 pm

Postby MadGinga » Tue Nov 13, 2007 5:03 pm

Hi there,

The following code, is a modified version of a code I wrote to read and display a light sensor reading, hopefully it should work. Using the "While(true)" command means that it will continue to display untill you manually shut down the nxt.

Hope this helps

Martin

Code: Select all
public class display {

   public void second (String[] args)
     throws Exception
     {
   CompassSensor compass_cs = new CompassSensor(SensorPort.S4);
   while(true) {
      int k = compass_cs.getDegrees();
      LCD.drawInt(k, 0, 1);
      LCD.refresh();
}
}
}
MadGinga
New User
 
Posts: 7
Joined: Mon Nov 05, 2007 4:12 pm
Location: UK

Postby gloomyandy » Wed Nov 14, 2007 2:17 am

If you are using the current release version of leJOS, then you may want to try a port other than port 4. The current i2c implementation (this is what is used to talk to the compass sensor), does not work well with that port. This port has additional hardware for adding another communications mode (not currently used..), and this causes problems with the i2c interface...

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

Postby 99jonathan » Thu Nov 15, 2007 3:36 am

MadGinga wrote:
Code: Select all
public class display {

   public void main (String[] args)
     throws Exception
     {
   CompassSensor compass_cs = new CompassSensor(SensorPort.S1);
   while(true) {
      LCD.clear();
      LCD.drawInt((int)compass_cs.getDegrees(), 0, 0);
      LCD.refresh();
}
}
}


fixed up the code a bit. its really bad to declare a variable every time a loop runs. Especially an infinite loop. The memory will just fill up. I added a clear() command too because if the compass displays "360" and then tries to display 12 without clearing, it will come out as 120.

Also changed the port.
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby gloomyandy » Thu Nov 15, 2007 9:17 am

Actually I don't think that simple types require a memory allocation (they are allocated on the stack), so your loop shouldn't run out of memory... May be wrong though...

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


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 5 guests

more stuff