How do run a loop until a condition exist
3snke
Posts: 7
in Propeller 1
I am working on making a counter for when a condition exists using the external encoders found in the Activity Bot kit. Currently a while(1) loop is used to monitor the condition of the encoder and adds a +1 while the encoder state is at 1, however this loop continues during the entire 1 state. Anyone run across a function or a concept that will only add 1 to a variable inside an endless loop during a transition from 1 to 0?
Comments
if (newState == 1) variableA += 1;
or:
if (newState == 0 and oldState == 1) variableB += 1;
print("%c p26 LED Cnt = %d\n p26state LED Cnt = %d%c", HOME, encoderCount, pinState26new, CLREOL);
set_output(26, input(14));
set_output(27, input(15));
pinState26new = get_state(14);
{
if ( pinState26new == 1 && pinState26 ==0 )
{
encoderCount += 1;
}
pinState26 = get_state(14);
- Thanks!
Your input might change between the first reading of it and the second.
I'm new to programming so I may not understand the difference between initializing pinState26 first, the way the code is written currently the loop starts at the transition (to my understanding) of 1 from the encoder and asses the loop, when i tried both pin states before the loop the events of the transition from 1 to 0 was not recognized in the if statement, but I will play around with the code again.
Thanks for the input, I will try pinState26 = pinState26new, although I didn't have any problems with the posted code I also didn't run tests using longer durations.
Each time through the loop you compare the 'new' (current) value with an old value. The first time through you don't have that old value, so you need to either compare with a default state, or by reading the pin once before you enter the loop.
As pinState26 and pinState26new seem to be derived from the state of pin 14 you might want to reconsider your variable names. Better than naming them for a pin (which might change for a number of reasons) I would suggest a functional name, perhaps 'encoder'.
Then before entering the loop you either read the current pin state or set it to a default.
In the loop you read the current state, you perform your comparison and conditional action, update the storage variable, and then repeat.
Something like:
This loop doesn't appear to do much more than provide human readable output while passing some state from one pair of pins to another. I assume that at some point you'll want this count to be reported to other code?
Of course, on the Prop each Cog has two counter modules that can perform this logic for you. The POSEDGE detector mode with FRQx set to 1 would perform all the logic in this loop automatically, but you'd lose the pin state transfer (14 to 26, and 15 to 27) and the printing of state, which while helpful is quite time consuming, meaning some edges could be missed if the pulses are short enough.
With the counter set up, you could simply read the PHSx register inside the code that currently calls the loop, rather than tying up a Cog with this while loop and having to pass the results to another Cog.
An issue would occur if the pin changed state between any of the three separate times that you read pin 14 in the loop: