Posts: 1
Joined: Tue May 22, 2018 6:59 pm

Need some help with program

Thu May 24, 2018 11:56 am

Hello all, so first of all I'm quite newbie to Java.At college we are programming using raspberry pi 3 , java NetBeans.So the problem is that I don't own a raspberry and probably won't at least for now cause it's not my main studies thing to program using raspberry.Anyway I only get my hands on it once in a week for like an hour :D so it's quite little time..So I'm asking for help here because I can't test my programs at home and I don't have much time to write them in class and make it to work from the first time..
Let's start..It's quite a simple program.A have to make a traffic lights.So here is what I have to do: Traffic lights should work in normal mode for 2 minutes and 30 second.Then it should switch to blinking yellow light which should work for 1 minute and after that it should shut down.There are no specific time for every light in normal mode.So for example simple I have to put this code in those time frames which I wrote above:

Code: Select all

// Normal mode for 2 minutes 30 seconds

// Only yellow  blinking for 1 minute

//Shutdown after 1 minute of yellow blinking;;;
So can anyone help me ? How to put those modes in those time frames ? I though that I could use for loop for them but don't know how to set correct time, maybe any other options or anyone could make it with for loop.

Btw sorry if some sentences are wrong, cause english is not my main language.

User avatar
Posts: 30
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Re: Need some help with program

Wed May 30, 2018 10:38 am

Don't get me wrong. I feel that if it's a task given, you should do it yourself. However, I'll be happy to point you in (imho) the right direction.

If you have set time values you can always use currentTimeMillis(). This is a Long that displays time since January 1, 1970 UTC (read up on epoch) in Milliseconds.

At the start of the program I would assign a Long to currentTimeMillis and then set a while-loop.

Code: Select all

long startTime = System.currentTimeMillis();
while(System.currentTimeMillis() < startTime + (3*1000) { // 3000 milliseconds or 3 seconds
	// kill some time
Hopefully this gave you some inspiration to solve the problem.

Good luck!
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

Return to “Java”