Math in LeJOS

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

Moderators: imaqine, 99jonathan, roger

New User
Posts: 2
Joined: Sun Nov 27, 2011 2:29 am

Math in LeJOS

Postby jdhigh2 » Sun Nov 27, 2011 2:54 am


I'm having trouble performing some simple math in LeJOS for implementation of PID control for line tracking for a school project. I have 3 light sensors. I want to average the values from the 3 sensors, and I want to subtract the 1st light sensor value from the 3rd. When I try to compile my program with nxjc it does not recognize my + and - symbols. Here is my code:

import java.util.Random;

import lejos.robotics.RegulatedMotor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.KalmanFilter;
import lejos.util.Matrix;
import lejos.util.PilotProps;
import lejos.nxt.*;
import lejos.nxt.comm.RConsole;
import java.lang.*;
import javax.bluetooth.*;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.UltrasonicSensor;

public class Light3math {
public static void main(String[] args) throws Exception {
LightSensor light1 = new LightSensor(SensorPort.S1);
LightSensor light2 = new LightSensor(SensorPort.S2);
LightSensor light3 = new LightSensor(SensorPort.S3);
int average = 0;
int difference = 0;

while (true) {
LCD.drawInt(SensorPort.S1.readValue(), 4, 0, 1);
LCD.drawInt(SensorPort.S2.readValue(), 4, 0, 2);
LCD.drawInt(SensorPort.S3.readValue(), 4, 0, 3);
average = (light1 + light2 + light3) / 3;
difference = light3 - light1;


User avatar
Advanced Member
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Re: Math in LeJOS

Postby Shawn » Sun Nov 27, 2011 5:52 am

How can you substract one LightSensor from another?

Wouldn't you have to get the values?

Return to “NXJ Software”

Who is online

Users browsing this forum: Google [Bot] and 7 guests