Loops and going round in them!!

For general Flowcode discussion that does not belong in the other sections.
Post Reply
simon-sandtcontrols
Posts: 43
http://meble-kuchenne.info.pl
Joined: Sun Mar 21, 2021 3:56 pm
Has thanked: 5 times
Been thanked: 7 times

Flowcode v10 Loops and going round in them!!

Post by simon-sandtcontrols »

Hi All,

I wonder if someone could explain the do's and don'ts around loops, in particular counting, please, what works and what doesn't. I have been caught up in this a number of times and it always seems to be a trial and error job, it would be nice to know some definitive rules to save the experimentation.

I will start with what I think I know and what I have tried. All described below works fine in the simulate but the issues arise when it's running in a PIC. This project was to generate a tone of varying length and frequency. The idea being to change the time duration for the frequency and the loop count to change the time the tone is on. All variables as integers and no number created greater than 2000.
Screenshot 2025-01-03 092933.png
Screenshot 2025-01-03 092933.png (39.49 KiB) Viewed 1889 times
If I set the loop to "count" I can make it work as long as I only put a number in the count box, if I put a variable in there and change the value of variable, it doesn't work. The time the tone was on did not change, changing the value of the variable. It counted a number of loops, but I can't say how many or where it got the number from. Why? It would be so useful for this to work.

So I added a count function, to count manually and exit the loop when the count conditions are met.
Screenshot 2025-01-03 093240.png
Screenshot 2025-01-03 093240.png (51.68 KiB) Viewed 1889 times
This works with the expression While "Count < Count_Target", however if I swap the the expression around and make it While "Count_Target > Count" it doesn't work, it exits the loop after 1 pass regardless. Why?

So out of curiosity I change it to Until "Count_Target < Count", that didn't work either. Again Why?

It seems to me that you should be able to use 4 different connotations of the While / Until expression, and achieve exactly the same goal, and in simulate this is confirmed, but in reality in the PIC it's not the case. In this case I was using a PIC 16F1503, but in other cases I have been using other 8 bit PICs such as the 16F1937 etc. I may be missing the obvious, quite possible, but I can't seem to find anything on this from searches etc. I know my variables were changing correctly, and doubly proved this by only changing the loop setup between each flash of the PIC.

Many Thanks.

Simon.

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Loops and going round in them!!

Post by mnfisher »

If you can post your flowchart - someone will take a look....

while loops - do something while a condition is true.

So while (.count < 10) at the start of a loop will loop until .count is greater than or equal to 10. while (10 > .count) will do the same...
At the end of a loop is similar but the loop will be executed at least once. Remember to increment (or decrement / change) )the variable in the loop.

for loops use a count (and check your variable is large enough - a common error is using a byte and overflowing) - it is particularly useful if you need to index into something (an array for example) It can be duplicated using while (as can all loops)

until - loops until a condition is true (for example loop until(pin == 1) will busy wait on a pin going high)

Marftin

simon-sandtcontrols
Posts: 43
Joined: Sun Mar 21, 2021 3:56 pm
Has thanked: 5 times
Been thanked: 7 times

Re: Loops and going round in them!!

Post by simon-sandtcontrols »

Thanks for the reply Marftin,

I haven't posted the flowchart as It felt pointless, I have it working and to make my point I would have to post 5 different versions, all which work in simulate but not once flashed into the PIC.

I can confirm that all the variables were Integer and numbers never exceeded 2000, so no chance they over ran. "Count" was set to zero on entering the loop .

However, from your post you do kind of confirm that all variations of the expression that achieve the same outcome should work, but don't. So is this a bug rather than needing a set of rules?

Simon.

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Loops and going round in them!!

Post by mnfisher »

I doubt there is a bug in these...

If you can post a simple example that doesn't do as expected it would help.

Martin

simon-sandtcontrols
Posts: 43
Joined: Sun Mar 21, 2021 3:56 pm
Has thanked: 5 times
Been thanked: 7 times

Re: Loops and going round in them!!

Post by simon-sandtcontrols »

Hi Martin,

Well this is very perplexing, in absence of the flowchart from earlier in the week, as quite a lot of changes were made after I witnessed the issues, and without the hardware (this is a project I am helping my Dad with and he took all of the circuitry with him) I have tried to re-create the flowchart in it's simplest form and in both a PIC 1937 & PIC 1503. I can not re-create the problem, it works as expected in all 5 versions. Counting to a variable, and all 4 connotations of While / Until - </>.

I know I literally swapped the Count & Count_Target variables and used the opposite sign earlier in the week and it went from jumping through the loop to working as it should. I also know that it wouldn't count to the variable, I had to put the counting calculations in manually. I also know that these were the only changes made between compiling's.

So I am now left questioning what ever went on earlier in the week, also why have I experienced issues with this in the past, that I now can't re-create.

I guess until I experience the problem again, I am going to have to park this and remember to save a non-function version to post here. Unless of course anyone else on her has had similar un-explained problems like this.

Thanks again for your reply and interest.

Simon

Steve-Matrix
Matrix Staff
Posts: 1465
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 204 times
Been thanked: 347 times

Re: Loops and going round in them!!

Post by Steve-Matrix »

Hi Simon,

As Martin has said, seeing an example would help. Especially with the C code output too.

Also, make sure you are initialising any variables you are using. In simulation, Flowcode will automatically set uninitialised variables to zero. But when running in the target, uninitialised variables could be set to an unexpected value. It is good practice to initialise variables to an appropriate value.

Regarding the initial issue you faced where a loop (using the "count" option) where a literal value worked but a integer value did not, this could be to do with some odd C rules with implicit arithmetic integer conversions. You could try setting the variable type that you use for the loop as an unsigned integer instead of a signed integer and see if that works. Comparing signed and unsigned variables can give unexpected results.

(EDIT: you've just responded before I submitted the above)

simon-sandtcontrols
Posts: 43
Joined: Sun Mar 21, 2021 3:56 pm
Has thanked: 5 times
Been thanked: 7 times

Re: Loops and going round in them!!

Post by simon-sandtcontrols »

Hi Steve,

After having packed it all up, I wondered if it was because I had 2 loops one after the other, maybe some clash, but I have just put it all back together and proved that not to be the case either.

OK, this is very helpful, I am pretty sure I did initialise the variables with starting values, but that is entirely possible one has slipped through the net. Although they were all updated by an interrupt (as I have just done in the example that all worked, so it's not that either) so would have had values very quickly.

I think this could be more of the cause of issue, I had used signed integers, although the same in the example I have been experimenting with that worked. There was more calculations in the experiments earlier in the week, so maybe that had an effect. If I come across this again, I will make sure I look at the variable types being compared and save the flowchart to post here.

Many Thanks.

Simon

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Loops and going round in them!!

Post by mnfisher »

Doo da doo da..

Is that Rod Serling sat over there ? Are you about to enter the 'Twilight Zone' :-)

Glad it is up and running - sometimes the littlest thing can cause the most perplexing results!

Martin

simon-sandtcontrols
Posts: 43
Joined: Sun Mar 21, 2021 3:56 pm
Has thanked: 5 times
Been thanked: 7 times

Re: Loops and going round in them!!

Post by simon-sandtcontrols »

Your not kidding, it's enough to drive you round the twist sometimes. It's amazing how it's always out to prove you a liar as well, he says bashing his head slowly against the wall.

I am thinking this all to do with signed / unsigned variables. At least I now know what to try first next time.

Thanks again.

Simon

Post Reply