Macros and variable options.

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

Moderator: Benj

Post Reply
User avatar
E*2Engineer
Posts: 35
Joined: Wed Dec 25, 2013 6:54 pm
Has thanked: 12 times
Been thanked: 6 times
Contact:

Macros and variable options.

Post by E*2Engineer »

Hello all,
I am writing a program that has multiple inputs and outputs, I am writing it with macros, to help keep the program structured,
and not a hot mess of a main program.
Hardware is my forte'. I *will* be re-learning C, and mastering FC6 to be able to be able to be a more functional programmer, but for now I have FC6 to get me running.

I am Multiplexing my 16 inputs through a CD4067 16 channel signal mux. I plan on using one macro to get all signal values, and another macro to make the decisions, based
on the values of the inputs that have been assigned variables.
The program attached only shows co2 high and low set point pots, I ran into problems when trying to get a variable assigned to the co2 sensor signal.

My question is, is it possible to write a program with macros using only global variables, without using a bunch of work-arounds, that defeats the purpose of me
trying to keep this simple? I have a current count of 16 total variables.

Will this be too memory intensive, which I understand is the purpose of using locals? Or am I just making to big of a deal of learning how to use local variables in my program?
So far it has been a bit confusing for this Blonde to comprehend the use of locals. Just putting a ".CO2" instead of just "CO2" as a variable has not solved my troubles.
Attached is a start of a test program that shows something of what I am trying to accomplish. If CO2 signal input is above or below the window set by potentiometers,
turn the CO2 supply on or off.

I have been though several tutorials, which have not helped.
If somebody could just show an example of of using locals, if using only globals is not feasible, I would be very appreciative.
Attachments
CONTROL CO2 LEVEL 3 26 16 .fcfx
ROW OF LED'S AT TOP SHOWS PORT B SELECTING MUX CHANNEL.
(16.89 KiB) Downloaded 250 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Macros and variable options.

Post by medelec35 »

Hi Craig,
First I need to point out some issues with the flowchart you have posted.
1) You have got components added on both System panel(3D) and Dashboard panel(2D)
2) When you add a component they will go go in the centre so you will need to move them as soon as they are added as you can see in the screen shot:
panels1.png
(29.88 KiB) Downloaded 3570 times
As you can see, the led is on the same spot as pot.
3 ) with Pots, switches led components etc.
If you right click in a component and select properties, you must change what each component is connected to.
I have had a look at all three pots and there are all connected to An0:
panels2.png
(38.77 KiB) Downloaded 3570 times
You must change each pot so there are connected to the correct adc channel.
To do that just click on the An0 to the right of channel.
4) Unconnected components.
If you look at the dash board there is a LED component that's not connected:
Unconnected1.png
(48.26 KiB) Downloaded 3570 times
When you try and compile to hex or target you will get an error message:

Code: Select all

CONTROL CO2 LEVEL 3 26 16 .c(482): WARNING: Too few arguments for: FCP_SET
CONTROL CO2 LEVEL 3 26 16 .c(492): WARNING: Too few arguments for: FCP_SET
..................
CONTROL CO2 LEVEL 3 26 16 .c(482): error: missing right paren
CONTROL CO2 LEVEL 3 26 16 .c(482): error: missing semicolon
CONTROL CO2 LEVEL 3 26 16 .c(480): error:  failure

failure
Completed BoostC compilation, return = 1

C:\Program Files (x86)\Flowcode 6\compilers\pic\boostc\boostc_16F.exe reported error code 1
You must make sure you set a connection for every component you add!
5) Every time a call macro is executed then the limited stack of microcontroller is increased by 1(at least that what I believe any way)
You have got a call within a call, calling itself! A very big NO NO.
Can't be done, as will not only be stuck in a loop, but will cause a stack issue:
Call Macro1.png
(110.77 KiB) Downloaded 3570 times
For best practice, always allow the call macro to exit at the end.
Don't call another macro within a call macro unless you know what you're doing and no stack corruption can occur.

6) You have got an invalid variable preventing from compiling.
You need to choose the correct variable:
Wrong Variable.png
(56.74 KiB) Downloaded 3570 times

If I was you I would create a bit of coding at a time, save then compile it to make sure all is well.
If you don't save on a regular basis, PC or flowcode could crash or there could be power failure and all the work is gone!

Once you get the basics sorted you can go more deep like into local variables.
It's always wise to look at support on Matrix home page,especially at learning centre & wiki
Aslo take a look at the links on Benj signature.

Martin
Martin

User avatar
E*2Engineer
Posts: 35
Joined: Wed Dec 25, 2013 6:54 pm
Has thanked: 12 times
Been thanked: 6 times
Contact:

Re: Macros and variable options.

Post by E*2Engineer »

Thank you Martin,
The code actually makes me look worse at this than I am.
1)I have completed (sucsessfully) the "Intro to Microcontrollers" with all the experiments, and each worked).
But there were no Local Variables brought up in that. This was prior to me purchasing V6.

2) I did not even know I had anything on the dashboard panel! Had I known, I would have cleared it off.
I chose to not have it on my screen. I am trying to work with the System panel only at this point.
Should I be working in the Dashboard panel instead of the system panel for now? Seems it would make sense,
as it would be closer to the V4.5 I had previously, and have no desire to get mired down in anything 3D yet.

3) I knew the code was worthless, as I didn't even understand what to do next, I never tried to compile it.
Or for that matter even step though it. It was only meant to show you what I was attempting to accomplish. Although worse than I even knew.

I almost feel I should be coding back in 4.5, especially if the System and Dashboard panel are tied together, where if I put a component on one,
it shows up on the other.
It doesn't look like it here, but I have written programs in V4.5 with inputs, a LCD display and interactive outputs dependent
on answers given. with sound and lights and switches an a ADC. I have programmed a clock using macros and connection points. I am not a total rookie to FC, and I fully understand the hardware.

Please don't throw the baby out with the bath here. I didn't have the CO2 signal variable tied to nothing due to ignorance, but because that's where the local variable came in, I did not know how to use that.
So I stopped at 1 unconnected variable/ part, rather than the 16 I need to work with. I plan to multiplex all into AN0. I have already written that piece of software/hardware and it works. (Just using LED's and no variables.)

SOOOOOOOO.
1) Should I use just use the 2d dashboard panel for starters to write a working program? Sort of as in 4.5? Can it work that way?
2) Would it be better to go back to 4.5? Not my first choice, but I need to make progress.
3) If not, I *will* do the work, clean this up, and then show you where I come to a stop, which will probably be right back at the Local Variable problem.
I could use answers to the questions above, however.
Thank you Very much,
-Craig

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Macros and variable options.

Post by medelec35 »

Hi Craig,
E*2Engineer wrote: did not even know I had anything on the dashboard panel! Had I known, I would have cleared it off.
The defaults are set so when you double click on a component it goes straight on to the Dashboard panel.
There are three ways of placing components directly on to the System panel.
1. Change the defaults by selecting Application Tab (View, Global Options,)
You just untick Use dashboard as default panel.
Then when you double click a component it goes straight to System panel instead.
Or
2. Click on the down arrow of component then select System panel.
or
3. Just drag component directly onto the System panel.
E*2Engineer wrote:I almost feel I should be coding back in 4.5, especially if the System and Dashboard panel are tied together, where if I put a component on one,
it shows up on the other.
4.5 would be a backwards step.
You can do so much more with V6, its worth persevering with.
It's probably that you wanted to use the System panel and dragged components or selected it.
But you may also double clicked by accident and since not seeing on the Dashboard, selected component again.
option 1 would prevent that.
E*2Engineer wrote:1) Should I use just use the 2d dashboard panel for starters to write a working program? Sort of as in 4.5? Can it work that way?
Personally I prefer the 3D system panel, just so I can see the keypad and push type switches move:
3D system panel.png
(57.45 KiB) Downloaded 3536 times
E*2Engineer wrote:2) Would it be better to go back to 4.5? Not my first choice, but I need to make progress.
Defiantly not as answered above.

Getting on to Local variables.
I'm not well up on those but I can hep you to get going.
What I do is within the call macro:
Local1.png
(70.32 KiB) Downloaded 3535 times
Just type the name of local variable E.g Co2L
Then when the local variable is added it will be preceded by a period:
Local2.png
(22.77 KiB) Downloaded 3535 times
You can manually place any created local variable in a calculation box, Switch componet, i/p o/p components, within a call macro by staring with a period then the name e.g .Co2L.
You can have the same name in more than one call macro, but the local variables are destroyed when the macro has been exited.
For example if you create a local variable with the same name in both CO2_LO_SET and CO2_HI_SET called Co2level.
If within CO2_LO_SET .Co2level has just been assigned with a value of 40, then as soon as CO2_LO_SET is exited .Co2level will no longer contain 40 as the variable has just been destroyed.
So when CO2_HI_SET is accessed it's good just reading .Co2level as it no longer contains 40.

Hope this helps you to get started.

I'm sure people with more local variable experience will have something to say.

Martin
Martin

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Macros and variable options.

Post by kersing »

E*2Engineer wrote:Hello all,
My question is, is it possible to write a program with macros using only global variables, without using a bunch of work-arounds, that defeats the purpose of me
trying to keep this simple? I have a current count of 16 total variables.
You can use global variables in macros, so writing a program with no local variables is possible. However, macro parameters and the return value are also local variables and using them might be convenient.

Say you want to write a macro to read the input channel and have it use a variable to select the MUX channel. If you use global parameters you would need to assign a value to the MUXinput global parameter before calling the macro to read the value. In the macro you read the global parameter to switch the MUX to the correct channel, get the value (using another global variable) and assign it to the correct global variable to store it.

When you use local variables you can create a macro that accepts the channel as parameter and returns the value. In the macro you use the local variable .Channel to switch the MUX to the correct channel, get the value and assign it to .Return . In the macro call you provide the MUX channel parameter and assign the return value to the correct variable (can be a local variable or a global one).
E*2Engineer wrote: Will this be too memory intensive, which I understand is the purpose of using locals? Or am I just making to big of a deal of learning how to use local variables in my program?
Memory savings can be one advantage of local variables, the main reason to use them is to make code modular and reusable (call the same macro with different parameters to read different input channels). It also helps to prevent accidental changes to a variable. Say you use two macros with a loop using a counter. In both macros you use the same global variable to do the counting. Now you risk unexpected behaviour when one macro calls the other macro (the second macro changes the counter used by the first macro). If you use local variables this can not happen because even if the names of the variables are the same, there are two variables (Macro1.Counter and Macro2.Counter).

Best regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
E*2Engineer
Posts: 35
Joined: Wed Dec 25, 2013 6:54 pm
Has thanked: 12 times
Been thanked: 6 times
Contact:

Re: Macros and variable options.

Post by E*2Engineer »

Jac,
Thanks for the input, I believe I see what you are talking about, after seeing how variables are used in macros.

"When you use local variables you can create a macro that accepts the channel as parameter and returns the value."

I am assuming that when you use a local variable to (in this case ) select the mux channel, and assign the channel value to it,
that because locals go away when leaving the macro, the decision made ( E.G.CO2 on or off) must also be made within that macro, is this correct?
THX,
-Craig

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Macros and variable options.

Post by kersing »

E*2Engineer wrote: I am assuming that when you use a local variable to (in this case ) select the mux channel, and assign the channel value to it,
that because locals go away when leaving the macro, the decision made ( E.G.CO2 on or off) must also be made within that macro, is this correct?
You are right, local variables are only available within the macro. However, you can return the value (assign it to .Return, an implicit local variable), in the macro call you will get the value and are able to assign it to another variable. (Put a variable in the Return Value box of the macro call to do so.) That value could then be used as parameter in another macro call.

BTW, please use the quote option, not fancy colors when quoting text.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Macros and variable options.

Post by medelec35 »

Via PM
E*2Engineer wrote:Thanks so much for your input,
I have removed the default from being the dashboard, and cleared off the dash as well. You were right as to how the pot and LED ended up there. It was not a conscious choice. What you had to say was very valuable to me. I appreciate it. If I post other questions, be sure it's after I did the research and could not find the answer on my own. I am willing to do the work myself. Between the change in versions, and taking on a larger project than in the past, that's where the questions come in.
Thanks again for your valuable input.
-Craig
Hi Craig,
E*2Engineer wrote:You were right as to how the pot and LED ended up there
That's because I have seen a fair few flowcharts where this has happened, so you're certainly not alone.
E*2Engineer wrote:I am willing to do the work myself
I have noticed that.
It's by far the best way of learning.
E*2Engineer wrote:Thanks again for your valuable input.
Thank you for that.
You are welcome.

Martin
Martin

Post Reply