Go to Post FIRST is inspiration. No matter how you do it, you learn something. Stop being silly. Stop making accusations. Go build robots. Now. - Aignam [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Spotlight this post!  
Unread 22-02-2011, 20:46
wdell wdell is offline
Registered User
AKA: William Dell
FRC #3999 (Shadetree Mechanics)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Killeen, Texas
Posts: 55
wdell has a spectacular aura aboutwdell has a spectacular aura about
Re: Java Threads

Quote:
Originally Posted by Aaron V View Post
Basically my main problem is that I need to pulse solenoids. It's simple to set it open or closed based on other input, but I don't know how to use time as an input. I figure I need threads because I don't want to stop everything else in the code.

Finally I've both seen the wait() function in the object class and the delay() function in the Timer class - I'm not sure which is better to use and which thread it effects.
How often are you trying to pulse the solenoids?

Code:
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.Solenoid;

public class Pulser implements Runnable {

    Solenoid firstSolenoid = new Solenoid(1);

    public void run() {
        while (true) {
            try {
                firstSolenoid.set(true);
                Thread.sleep(1000); // sleep thread for 1 second
                firstSolenoid.set(false);
                Thread.sleep(1000); // sleep thread for another second
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
That will pulse the solenoid on for a second, then off for a second. The sleep() function causes the thread to wait (time in milliseconds).

wait() causes the thread to sleep until another thread issues a notify() or notifyAll() command. It doesn't set a specific time to wait for.

You might try replacing the sleep() function with Timer.delay(), I think that would get rid of the try..catch statements.

Code:
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;

public class Pulser implements Runnable {

    Solenoid firstSolenoid = new Solenoid(1);

    public void run() {
        while (true) {
            firstSolenoid.set(true);
            Timer.delay(1);
            firstSolenoid.set(false);
            Timer.delay(1);

        }
    }
}
To start the thread, you need to tell it to start() somewhere in your main program:

Code:
Pulser pulser = new Pulser();
new Thread(pulser).start();

Last edited by wdell : 22-02-2011 at 20:51.
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 12:58.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi