Bool True/False versus 1/0

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
ncc1502
Posts: 43
Joined: Mon Apr 07, 2008 9:18 pm
Contact:

Bool True/False versus 1/0

Post by ncc1502 »

Does it make any difference of I assign a Bool variable wit True or 1 / False of 0?

I have a program in which I assign Boole versiables with 1 and 0

In decisions I also check if Bool variable = 1

For some Odd reason the program sometimes seems to compile wrong and makes wrong decisions at these bools.

If I assign the bool again with the same value and compile again it works as expected.


Is it a problem if a variable has the same name as a macro?
This happens if (bool) Encoder_Selection = 1, then execute Macro Encoder_Selection

mnf
Valued Contributor
Valued Contributor
Posts: 856
Joined: Wed May 31, 2017 11:57 am
Contact:

Re: Bool True/False versus 1/0

Post by mnf »

Can you post an example that demonstrates this? The names shouldn't cause a problem as Flowcode changes the names of variables to FCV_NAME or FCL_NAME depending on whether they are global or local respectively.

Are you combining results

Code: Select all

If a=1 and b
or other? Where some parenthesis might be needed.

Flowcode treats 0 as false and non-zero as true, are you sure you're variable has the expected value as well?

Martin

ncc1502
Posts: 43
Joined: Mon Apr 07, 2008 9:18 pm
Contact:

Re: Bool True/False versus 1/0

Post by ncc1502 »

I did not combine results.

The fault does not always occur. I have had this problem 2 or 3 times.

Because I knew it worked before I assumed I made an error somewhere.

I started to disable pictograms in my program until I only kept the part of the program that did not work, everything looked fine but it stil did not work. In simulation everything was fine.
Then I took out the macro call from the decision and put is in the normal program line and it worked as expected. I then opened the decisions, put in the same line and saved it, removed the macro call from the normal program line and it worked as expected.

It you look in main program:
First calculation:
Encoder_Selectie = 1
First decision
If Encoder_Selectie = 1 then call macro Initialisatie_Encoder_Selectie

and a bit further down in the main loop of the program
If Encoder_Selectie = 1 then call macro Encoder_Selectie

In simulation it worked fine, but in real life on te pic mocrocontroller it did not.

I took quite a lot out of the program to make it easyer to follow (and protect my work), the part that had the problem is unchanged.
Attachments
Example.fcfx
(45.57 KiB) Downloaded 79 times

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 9014
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Bool True/False versus 1/0

Post by medelec35 »

Hi
I suspect the issue your facing is caused by switch contact bounce.
When a switch is first closed, the contacts bounce open and close for a few milliseconds before finally resting in the closed position
There is no delay after the switch input, and since the while loop will run very fast then the microcontroller is detecting the opening and closing of the contacts when first closed the switch.
There are several ways around this.
Use a loop with an input and a dealy, use the switch component and set a debounce time or use a resistor and capacitor.
I have opted for the second one and set debounce within switch properties for 50ms:
Debounce Setting.png
(52.97 KiB) Downloaded 2254 times
The debounce set will only work with the switch component.
E.g ReadAll, ReadState, WaitUntilHigh & WaitUntilLow.
Debounce within switch properties will not work with input.
Also, don't forget as you have used

Code: Select all

XOR
for example

Code: Select all

Setup = Setup XOR 1
, each time the code is accessed, then

Code: Select all

Setup 
will be toggled!
XOR is commonly used for inverting bits.

How have you got your switches wired up?
Take a look here.

Another thing to look at is after compiling, look at Build, Compiler messages.
Check immediately after

Code: Select all

Building CASM file
If there is any mention of corruption then the cause must be investigated.
For example, I placed a ms delay within an interrupt and main which you can't do and I got:

Code: Select all

Building CASM file
Serious Warning: Possible sw stack corruption, function 'delay_ms' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)
Memory Usage Report
On a separate issue, I have noticed that within the configuration settings you have got Extended CPU Enable Enabled. That is known to cause issues with LCD's.
It will need to be disabled.
Attachments
Example2.fcfx
(45.84 KiB) Downloaded 80 times
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 856
Joined: Wed May 31, 2017 11:57 am
Contact:

Re: Bool True/False versus 1/0

Post by mnf »

A few thoughts:

Change encoder_selectie to a byte (rather than a bool) or use true/false (or just

Code: Select all

if encoder_selectie
) (It shouldn't make a difference - a bool is just a byte - but it makes your intentions clearer)

I can't find in the code any macro where encoder_selectie is changed to a value other than 1?

Is your program crashing? - I had a situation where my code was actually restarting - I'd written off the end of an array.

Martin

ncc1502
Posts: 43
Joined: Mon Apr 07, 2008 9:18 pm
Contact:

Re: Bool True/False versus 1/0

Post by ncc1502 »

@Martin (Medelex35)
I do not use mechanical switches, I use a rotary encoder that is also used at VVVF drives. F.I. a Wachendorff WDG58
The encoder has 2 outputs A and B and a resolution of 1024 pulses per revelation. There is no bounce on the outputs.

With 1024 pulses per revelation, a delay would not work because then I would miss almost alle pulses during te delay.
I don't think bounce is a problem, If I rotate the encoder fast for 1 revelation, my counter is (about) 1024.
I will receive about 1000 pulses per second (1 revelation).


My problem was not that I got a wrong pulse count, my problem was that I got a 0 count.
The macro Initialisatie_Encoder_Selectie or the macro Encoder_Selectie (or both) was/were not called in the if-then-else decisions. Why???? I have no clue. That was why I asked if I had to use true and false with bool variables.

As I wrote before, I put in the same text in the decision, saved it, compiled it and it worked.

Thanks for the tips on the settings of the Microcontroller chip and the check of the CASM file.
I found the settings for the microcontroller by trial and error, some I understand (f.i. watchdog) and some are (even with the datasheet) mambodjambo to me.

What settings should I use if i want to prevent that the program can be downloaded from the chip. I have tried some settings but depending on which are selected, the program does not run anymore. code protect/table write protect/table read protect.




@Martin (mnf)
you are right the setting encoder_selectie, does not change during runtime.
The program can work with two kinds of "selectie", before compiling the setting 1/0 is made. The setting will and can not be changed during runtime.
That is why I made the If-then-else sections, so I put in once which "selectie" is used and at all places in the program the right macro is called.
After I did this the problem started.
Before it worked fine, but I had to disable/enable parts of the flowchart manually.

mnf
Valued Contributor
Valued Contributor
Posts: 856
Joined: Wed May 31, 2017 11:57 am
Contact:

Re: Bool True/False versus 1/0

Post by mnf »

Just one more idea - if you tick the box in 'Project Options' - Use #if for constant decisions - then define encoder_selectie as a constant (value 1 etc)rather than using it as a variable and then assigning a value - then the unused code should be optimised out. What happens when you run the program?

Martin

Post Reply