Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:57:47+00:00 2026-06-17T21:57:47+00:00

I’m currently programming on a Lego NXT 2.0 with leJOS 0.9.1 beta firmware. I

  • 0

I’m currently programming on a Lego NXT 2.0 with leJOS 0.9.1 beta firmware.

I have several different sensors, like a color sensor and a ultrasound sensor.

I’m using a while-loop to make the robot drive forward, until it hits a wall. However, for some reason, I don’t like this approach and wanted to implement a listener instead. A leJOS developer wrote, however, that using the listener model is not recommended and I should use threads instead to poll the value of the ultrasound sensor.

Now I’m wondering how bad the implementation with a while-loop really is (operating system wise, as in wasting of resources) and how a threading model would be more beneficial (and implemented)?

MWE:

public class SensorTest {
    static UltrasonicSensor sonic;
    static DifferentialPilot pilot;
    public static final int DISTANCE = 20;

    public static void main(String[] args){
        sonic = new UltrasonicSensor(SensorPort.S1);
        pilot = new DifferentialPilot(8.5, 25, Motor.C, Motor.B);
        int i = 0;      

        while (i < DISTANCE) {
            pilot.forward();
            i = sonic.getDistance();
        }
        pilot.stop();
      }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:57:48+00:00Added an answer on June 17, 2026 at 9:57 pm

    Not having any experience wit the Lego blocks (I’m serious jealous) you have to ask yourself some serious questions.

    In a simple situation, using a loop in this manner may not be a bad solution, but as you add more to, the time it takes to process each request (check n number of sensors, perform n number of operations), it will become slower to react.

    A Threaded model may allow you to process more data, but you may need to consider how many threads you need and what they are doing.

    For example, you could have a Driver thread, whose sole responsibility is to move the Lego. It would do this be querying a number of “sensor” thread’s to determine what it is telling, but each sensor thread would be polling data at there own rate and not caring about anything else.

    You could also inverse the model, allow the Driver to continue moving until a Sensor thread raised some kind of alert and told the Driver it should change directions.

    I, personally, would be trying to see if I could get a thread model working (even for a simple device) as it will provide more flexibility into the future to expand the operations…

    At the end of the day, it’s going to be balancing act

    Seriously jealous!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.