11-20-2009 08:31 AM
Hi this is sligthly more difficult than the title reads i think.
I have a while loop reading the com port to detect a switch being on or off by doing an rs232 loopback
if i get an error its off, no error on.
what i want to do is increase a number by 1 when the switch is on the off position, but at the moment
the number just constantly increase as the "+1" function is in a loop. How can i make it only increase
the number once.
The loop needs to constantly run if the switch is on or off
Hope this makes sense, i have attached a picture of what it does now
I look forward to your suggestions.
Solved! Go to Solution.
11-20-2009 08:38 AM
You can add a shift register that will remember the last state of the switch. If the last state of the switch was True, but now is False, you can increment your counter.
-Cristian
11-20-2009 08:40 AM - edited 11-20-2009 08:41 AM
One way is to use a flag. I change the flag to true as soon as the thing turns on, then false again until the next time it's turned on.
There may be better solutions, but this is the first thing that came to mind.
EDIT: My example is what Cristian said. Beat me to it.
11-20-2009 08:52 AM - edited 11-20-2009 08:58 AM
edit: After re-reading your post I think you need Tim's idea above, not the First Call? function, but just in case you didn't know about it, there it is!
11-20-2009 08:54 AM
11-20-2009 08:58 AM
elset191 wrote:
I think he wants it to increment every time the switch is flipped. Not just the first time. At least, that's how I understood it.
Yep, I was editing while you responded
11-20-2009 08:59 AM
Cheers tim that is exactly what i wanted,
so simple but im struggling this friday afternoon.
Thank you very much
11-20-2009 09:17 AM
oops too late.
You guys are fast.
An example follows.