Use of currentTimeMillis [SOLVED]

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

Moderators: 99jonathan, roger, imaqine

Use of currentTimeMillis [SOLVED]

Postby biozik » Wed Jul 11, 2007 9:06 am

Hello everybody,

I am new here. I am French, so excuse me for me English :oops:
I am working since one week on Lego Mindstorms NXT with the leJOS API.

I have a problem. I would like to stop my robot after 10 seconds. I have try to use the currentTimeMillis fonction but I have an error when I try to link.

This is my code :
Code: Select all
import lejos.nxt.*;

public class Time
{
public static void main(String [] args) throws Exception
   {
   LCD.clear();
   LCD.drawString("Press ENTER", 3, 3);
   LCD.refresh();
   Button.ENTER.waitForPressAndRelease();
      
   long t1 = System.currentTimeMillis();
   long t2 = t1;
      
while( t2-t1 < 10000 );
   {
   t2 = System.currentTimeMillis();
   LCD.clear();
   LCD.drawString("Wait", 3, 3);
   LCD.refresh();
   }

   LCD.clear();
   LCD.drawString("10 seconds", 3, 3);
   LCD.refresh();
   }
}


I think that the problem is that "t2-t1" is long and the while loop condition works with an integer.
Maybe do you know how I could convert a long in an integer or how to solve this problem?

Thank you

BioZiK
biozik
New User
 
Posts: 7
Joined: Wed Jul 11, 2007 8:52 am

Postby 99jonathan » Wed Jul 11, 2007 2:25 pm

if that is indeed the problem, you can convert a long to an int by (int). So you would do something like the following:

Code: Select all
 
...
int t1 = (int) System.currentTimeMillis();
   int t2 = t1;
     
while( t2-t1 < 10000 );
   {
   t2 =(int) System.currentTimeMillis();
...
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby biozik » Thu Jul 12, 2007 11:44 am

Thank you very much ;)

It is well running ;)
biozik
New User
 
Posts: 7
Joined: Wed Jul 11, 2007 8:52 am

Postby 99jonathan » Thu Jul 12, 2007 3:41 pm

Nice!! :D
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am


Return to NXJ Software

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests

more stuff