I have developed a very basic formula generator for use in Flowcode.
It could be useful if you have a variable with a number range,and you want the result to be a different number range.
E.e you have a lowest number of 80 and highest number of 254 and you want the result to be a number between 260 and 920
If you enter in attached excel sheet in correct boxes: 80, 254 , 260 and 290
A formula will be automatically shown, what is required to put in the calculation box of Flowcode
In the above example formula shown is: Variable /100 * 379 + 60 * -72 /100
so in my case if mark_time = 80 then fet_on_time after calculation will be 260
if mark_time = 254 then fet_on_time after calculation will be 920
mark_time = 150 then fet_on_time after calculation will be 525 etc.
I have not made excel sheet professional looking in anyway (lack of time ).
It was created in a short amount of time.
The sheet is protected, but only to stop user form entering numbers in wrong cells.
There is no password to un-protect sheet, so feel free to give sheet a professional look, and even post it on this thread if you wish to.
Hope it comes in handy, as it has done with my Flowcode application. If it has then feed back would be appreciated. Thank you.
Just one suggestion, if the formula generates a series of hashes #######, just unlock worksheet, make columns C and E wider then re-protect worksheet.
Formula Generator
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
-
- 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: Formula Generator
After developing different applications that required different formulas,my conclusion is:
The formulas generated are correct. To enable them to successfully work in Flowcode, they must be simplified.
i.e Variable /100 * 379 + 60 * -72 /100
has to be reduced to: 379 * Variable/100 - 216/5
The formulas generated are correct. To enable them to successfully work in Flowcode, they must be simplified.
i.e Variable /100 * 379 + 60 * -72 /100
has to be reduced to: 379 * Variable/100 - 216/5
Martin
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Formula Generator
Hello
One thing to keep in mind when performing calculations on microcontrollers is that all of the calculations will be carried out using integer maths. (Unless you are using floating point variables)
If we look at your calculation there are certain pieces that will limit the accuracy etc.
379 * Variable/100 - 216/5
Variable / 100 - The result of this portion can either be 0, 1 or 2 so multiply by 379 and you get 0, 379 or 758.
216 / 5 - The result of this is 43 not 43.2
If possible it is better to use integer variables and scale up values first so you get a better resolution.
One thing to keep in mind when performing calculations on microcontrollers is that all of the calculations will be carried out using integer maths. (Unless you are using floating point variables)
If we look at your calculation there are certain pieces that will limit the accuracy etc.
379 * Variable/100 - 216/5
Variable / 100 - The result of this portion can either be 0, 1 or 2 so multiply by 379 and you get 0, 379 or 758.
216 / 5 - The result of this is 43 not 43.2
If possible it is better to use integer variables and scale up values first so you get a better resolution.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Formula Generator
Don't be afraid to use brackets like this to make sure the calculations do not lose accuracy:
((379 * Variable)/100) - (216/5)
((379 * Variable)/100) - (216/5)
-
- 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: Formula Generator
Thanks for the input guys.
Yes I realised about integer maths since Flowcode will not allow decimals to be entered.
I have changed Formula Generator slightly to enable formula to be reduced easier.
E.g. I wanted a time delay varying form 2.6 ms to 9.2 ms.
So when I entered values required, I entered 26 and 92 for lower and upper limits.
which had to correspond with 0 and 255 respectively
Formula Generator displayed following formula :Variable/100*26+1*2600/100
Reducing = (Variable * 13/50)+26
To test by substitution:
Variable = 0:
a=0*13/50+26=26
Variable = 255:
a=255*13/50+26=92.3 = int 92 which was good enough for my application
Then I use C Code delay_100us(FCV_VARIABLE);
You can also use formula Generator if you want PWM with e.g. a duty of 20 - 50% should correspond with ADC giving i/p 0 to 5V = 0x0 to 0x255 etc.
Re brackets. Does calculations within Flowcode and PIC's follow BODMAS?
Update!
I have updated Formula Generator to auto reduce fractions, so you don't have to!
It may not be perfect (just look at the colours ) or fool proof but I'm sure it will save time,
since you should be able to enter displayed formula directly in to Flowcode calculation box.
Yes I realised about integer maths since Flowcode will not allow decimals to be entered.
I have changed Formula Generator slightly to enable formula to be reduced easier.
E.g. I wanted a time delay varying form 2.6 ms to 9.2 ms.
So when I entered values required, I entered 26 and 92 for lower and upper limits.
which had to correspond with 0 and 255 respectively
Formula Generator displayed following formula :Variable/100*26+1*2600/100
Reducing = (Variable * 13/50)+26
To test by substitution:
Variable = 0:
a=0*13/50+26=26
Variable = 255:
a=255*13/50+26=92.3 = int 92 which was good enough for my application
Then I use C Code delay_100us(FCV_VARIABLE);
You can also use formula Generator if you want PWM with e.g. a duty of 20 - 50% should correspond with ADC giving i/p 0 to 5V = 0x0 to 0x255 etc.
Re brackets. Does calculations within Flowcode and PIC's follow BODMAS?
Update!
I have updated Formula Generator to auto reduce fractions, so you don't have to!
It may not be perfect (just look at the colours ) or fool proof but I'm sure it will save time,
since you should be able to enter displayed formula directly in to Flowcode calculation box.
- Attachments
-
- formula generator.xls
- (34.5 KiB) Downloaded 400 times
Martin
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Formula Generator
Yes, but my point is that brackets add nothing to the size of the code and can drastically improve the readibility of formulae.medelec35 wrote:Re brackets. Does calculations within Flowcode and PIC's follow BODMAS?
Also, I cannot answer with any certainty what the answer to the following calculation is either in simulation or in the compiled code itself, because the operators "*" and "/" have the same order of precedence in C:
Code: Select all
a = b * c / d
Code: Select all
a = b * (c / d)