Displaying/updating sensor readings on the LCD in real time

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Displaying/updating sensor readings on the LCD in real time

Postby DigitalOne » Fri Nov 02, 2007 11:09 pm

I figured my first task when I decided to use LeJOS for my NXT should be how to gather sensor data. It's not a robot if it can't respond to its environment in some way, right?

Anyway, the following is the first piece of Java code I've ever written and had work. It returns a current sensor reading and displays it on the LCD. This code also updates the value on screen to the current sensor reading for every iteration through the 'for' statement.

I should mention I've only tested this with the LightSensor but I don't see why it wouldn't work with the others.

Check it out,

Code: Select all

import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;

public class HelloWorld {
      
      public static void main(String[] args) {
      LightSensor ls;
      int t;
      ls = new LightSensor(SensorPort.S2, true);
      
      for(int i = 0; i<10000; i++) {
         t = ls.readNormalizedValue();
         LCD.drawInt(t,0,0);
         LCD.refresh();
      }
   }
}


Let me know what you think. Any advice is appreciated advice.

//Matt
"I would kill everyone in this room for a drop of sweet beer." -Homer Simpson
User avatar
DigitalOne
New User
 
Posts: 7
Joined: Tue Oct 23, 2007 6:20 pm
Location: America's Wang

Postby bbagnall » Sun Nov 04, 2007 8:01 pm

Looks good to me.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby cubed » Sat Nov 10, 2007 12:09 am

@DigitalOne - I have a suggestion in regard to your code.

Code: Select all
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;

public class HelloWorld {
     
      public static void main(String[] args) {
         LightSensor ls;
         int t;
         ls = new LightSensor(SensorPort.S2, true);
     
         while(true) {
            t = ls.readNormalizedValue();
            LCD.drawInt(t,0,0);
            LCD.refresh();

            if (Button.ESCAPE.isPressed()) {
               break;
            }
         }
     }
}


The change that I am suggesting is rather than using a for loop "for(int i = 0; i<10000; i++)" to do what you're doing, use an infinite loop, "while(true)". The only real change here is that your for loop will eventually end (after 10000 times, the way you have it written), whereas the while loop I am suggesting won't end until you press the escape button, at which point it will break out of the infinite loop. This is what I assume you were going for.
cubed
New User
 
Posts: 2
Joined: Sat Nov 10, 2007 12:04 am


Return to NXJ Projects

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

more stuff