View Full Version : Flash an LED while Polling
10-07-2008, 07:13 AM
I'm looking for some assistance in trying to accomplish what may be quite simple but has me perplexed.ˇ I'm pollingˇsome Input pins on a BS2P40 andˇturning a motor on or off and changing direction depending on their status.ˇ When 2 of the pins are active, I need to flash an LED (among other things) at 1 second intervals until the condition is no longer true.ˇ However, I need to know exactly when the condition is true or not trueˇsoˇI can not use the PAUSE command to hold the LED on or off due toˇprogram execution stoppingˇduring PAUSE.ˇ I think I'll figure it out eventually but I could use a few pointers.
BTW, we're a manufacturer of Semiconductor processing tools.
10-07-2008, 07:54 AM
You can still use a short PAUSE. Use a word counter to count the number of PAUSE 20 statements that are executed in a loop. Each time through the loop, check your two input pins to see if both are active. If not, exit the loop and GOTO the part of your code that decides what to do if only one input is on or if no inputs are on. If both are active, continue in the loop and increment the count in the word variable. When the count reaches 50, approximately one second has elapsed and you can toggle the state of the LED (and reset the count to zero).
This allows you to check the status of the inputs, yet still keep approximate time. The time required to execute the input test and the other stuff in the loop will be a fraction of the 20ms and the 20ms (which is fairly accurate) will swamp the other statement's execution time.
Check out the "app-note" link at www.emesystems.com (http://www.emesystems.com) for a discussion of the execution time of various Basic statements.
10-07-2008, 07:58 AM
Thanks for the quick reply.ˇ That's exactly where I was going next.ˇ IS there any reason why you chose 20ms rather than 1 or 2? Just curious if there's a minimum time value for Pause/
10-07-2008, 08:15 AM
1 or 2ms is almost the same order of magnitude of time as many of the simpler PBasic statements on a BS2. If 10ms or 20ms works for you, it would really swamp any effect of the execution time of the statements surrounding the PAUSE statement.
10-07-2008, 08:18 AM
Gotcha.ˇ Thanks.ˇ I think I'll go with 10ms.ˇ Can you give me a hint of what issues might come up if 10 is too small a value.
10-07-2008, 08:29 AM
I think Mike chose 20 ms because it is relatively long with respect to the execution time of BASIC Stamp instructions. A program polling loop on the Stamp is likely to take on the order of one millisecond. It is possible to write program code so that every path through the code takes the same amount of time, but that is harder to do when adding up pieces of code with lots of branches. It is easier to pace it with relatively long PAUSEs. But if your polling loop is quite simple, it would certainly be possible to shorten it and measure the execution time empirically.
Another approach to this problem is to attach an parallel RC circuit between a free pin and Vss, and make the RC value around 1 second. Add that pin to your polling loop. PULSOUT the pin briefly high and then make it an input. When it goes low (C having discharged through R), toggle the state of the LED, and pulse the RC pin high again to charge the capacitor for the next round. It is like RCTIME, but DIY.
Are you using the BS2p's POLLing commands, or simply a polling loop?
10-07-2008, 09:26 AM
And one more time, thank you for the quick reply.ˇ I know you guys are busy helping people wih much bigger problems than mine but I do have one more question;ˇ When I eliminate the Pause command all together and just test the loop to see if "X" has reached a value of 300 for on, and 300 for off, while doing my tests in between, the LED seems to blink just fine.ˇ Do either of youˇsee any problem with this?
10-07-2008, 09:59 AM
Good deal. No problem with that at all Bob. The only problem would come if you change the program loop substantially, and then you only have to readjust the timing.
10-07-2008, 10:32 AM
Great! Thanks for everything.
FYI - You may not be interested but I'm attching the schematic and PCB layout we're using just so you know we're using the Stamp in a professional manner.ˇˇThis board is an interface between a PLC and aˇCool Muscle Smart Motor (www.coolmuscle.com (http://www.coolmuscle.com)).