Request for good sample code for timer application

Supporting Java SE version 7 and up
t7653jm
Fresh meat
Posts: 3
Joined: Sun Mar 31, 2013 9:01 am
Contact:

Request for good sample code for timer application

Postby t7653jm » Sun Mar 31, 2013 9:21 am

I would like to have a time based loop on my project. For example I would like to check the inputs every second and then adjust the outputs based on the inputs. I would also like to use time of day as an input.

Can anyone recommend some sample JAVA code or functions that show how this can be achieved?

MarkS
Phidgetly
Posts: 39
Joined: Sat May 15, 2010 8:37 am
Location: Southeast Pennsylvania USA
Contact:

Re: Request for good sample code for timer application

Postby MarkS » Sun Mar 31, 2013 9:54 am

Have a look at Joda Time. http://joda-time.sourceforge.net/

Here's an example of using Joda Time's Instant class to open a Phidgets Dictionary:

Code: Select all

Dictionary dict;
String ipAddress = "127.0.0.1";
int port = 5001;
long timeout = 5000;

try {
   dict = new Dictionary();
   dict.open(ipAddress, port);

   Instant max = new Instant().plus(timeout);
   
   while (!dict.isAttached() && new Instant().isBefore(max)) {
      Thread.sleep(100);  /* In reality, you need a try/catch block here */
   }
   
   if (new Instant().isAfter(max)) {
      throw new PhidgetException(PhidgetException.EPHIDGET_NETWORK_NOTCONNECTED, "Dictionary Not Attached");
   }
   
   System.out.println("Dictionary opened");

}
catch (PhidgetException pe) {
   System.out.println(pe);
}


And another illustrating a one-second time delay. Here you could also use the Joda DateTime class instead of Instant, but that's kind of overkill for this purpose:

Code: Select all

Instant nextscan = new Instant().plus(1000);
while (new Instant().isBefore(nextscan) {
   thread.sleep(100);
}
Link: EcoSteam - Residential Steam Boiler Control using Phidgets
"Bringing 21st century automation to a 19th century home"

t7653jm
Fresh meat
Posts: 3
Joined: Sun Mar 31, 2013 9:01 am
Contact:

Re: Request for good sample code for timer application

Postby t7653jm » Sat Apr 13, 2013 6:21 pm

Thanks MarkS I appreciate your detailed response. I'll try that.


Return to “Java”

Who is online

Users browsing this forum: No registered users and 0 guests