LED counter
LittleOne
Posts: 6
I'm using the following ·simple timer program and the javelin stamp to make an LED counter
// Program Listing 7.5 - Simple Timer Demo
import stamp.core.*;
// This program blinks an LED circuit connected to P0 every 200 mS and
// blinks an LED circuit connected to P2 every 300 mS using the Timer object.
public class SimpleTimer {
· public static void main() {
··· Timer t1 = new Timer();························· // timer for first LED
··· Timer t2 = new Timer();························· // timer for second LED
··· boolean led0=false;
··· boolean led2=false;
··· t1.mark();······································ // Start timer t1
··· t2.mark();······································ // Start timer t2
··· while (true) {·································· // do forever…
····· String msg = "test\n";
····· CPU.message(msg.toCharArray(),msg.length());·· // Print Message
····· if (t1.timeout(1000)) {························ // If 200 mS LED interval
······· led0=!led0;································· // set LED
······· CPU.writePin(CPU.pin0,led0);
······· t1.mark();·································· // start new time period
····· }············································· // end if
····· if (t2.timeout(300)) {························ // If 300 mS LED interval
······· led2 = !led2;······························· // Negate LED
······· CPU.writePin(CPU.pin2,led2);
······· t2.mark();·································· // start new time period
····· }············································· // end if
··· }··············································· // end while
· }················································· // end main
}··················································· // end class declaration
for some reason though the timout function isn't working at the correct speed.· If you test multiple "timeouts" it should be a linear function, unfortunatly it looks more like e^(-x) function.· Why is this? And how do i fix it?
Post Edited By Moderator (Chris Savage (Parallax)) : 9/1/2005 3:19:43 PM GMT
// Program Listing 7.5 - Simple Timer Demo
import stamp.core.*;
// This program blinks an LED circuit connected to P0 every 200 mS and
// blinks an LED circuit connected to P2 every 300 mS using the Timer object.
public class SimpleTimer {
· public static void main() {
··· Timer t1 = new Timer();························· // timer for first LED
··· Timer t2 = new Timer();························· // timer for second LED
··· boolean led0=false;
··· boolean led2=false;
··· t1.mark();······································ // Start timer t1
··· t2.mark();······································ // Start timer t2
··· while (true) {·································· // do forever…
····· String msg = "test\n";
····· CPU.message(msg.toCharArray(),msg.length());·· // Print Message
····· if (t1.timeout(1000)) {························ // If 200 mS LED interval
······· led0=!led0;································· // set LED
······· CPU.writePin(CPU.pin0,led0);
······· t1.mark();·································· // start new time period
····· }············································· // end if
····· if (t2.timeout(300)) {························ // If 300 mS LED interval
······· led2 = !led2;······························· // Negate LED
······· CPU.writePin(CPU.pin2,led2);
······· t2.mark();·································· // start new time period
····· }············································· // end if
··· }··············································· // end while
· }················································· // end main
}··················································· // end class declaration
for some reason though the timout function isn't working at the correct speed.· If you test multiple "timeouts" it should be a linear function, unfortunatly it looks more like e^(-x) function.· Why is this? And how do i fix it?
Post Edited By Moderator (Chris Savage (Parallax)) : 9/1/2005 3:19:43 PM GMT
Comments
· /**
·· * Checks whether <code>timeMS</code> milliseconds have elapsed since
·· * the last call to <code>mark()</code>.
·· *
·· * @return true if the timeout period has been exceeded.
·· */
· public boolean timeout(int timeMS) {
··· return timeout(timeMS/569, (timeMS%569)*115);
· }
If you find the constant 119 in your method, change that to 115.
Then run your program again and see if it solved your error.
Also noted you test t1.timeout(1000) which is 1000ms, not 200ms.
regards peter
Post Edited (Peter Verkaik) : 9/1/2005 6:47:43 AM GMT