ADC Averaging macro

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

ADC Averaging macro

Postby daveb0360 » Mon Aug 20, 2012 2:45 pm

Hi all,
I posted this elsewhere by accident so apologies for the confusion..

Benj wrote:Hello Uli,

You can also do ADC averaging by taking multiple samples, adding the results together in a INT variable and then dividing by the number of samples. Remember not to let the INT variable accumulate more then 32767 or the values will start turning negative.

If the sample count is a factor of two then you can do a right shift instead of a divide to save a few program clock cycles.

eg "samples = samples >> 3" is the more efficient way of achieving "samples = samples / 8"


Could someone show a flowcode to do exactly this please.
I wish to read a temperature via adc1 (as int), assign the value to a variable 'Temp', run this in a loop 8 times @ 12ms per reading and output the average to another variable 'degc' for use by another part of the program.

The idea is that the reading used by the other parts of the program, will be devoid of jitter.........a sort of digital de-glitching.

Any V4 code examples please?

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC Averaging macro

Postby Enamul » Mon Aug 20, 2012 2:56 pm

Hi Dave,
Here is just an example using Ben's idea and your requirement..you can work on that to adopt to your complete requirement.
Attachments
AVG_ADC.fcf
In FCV4
(5 KiB) Downloaded 220 times
These users thanked the author Enamul for the post:
daveb0360 (Mon Aug 20, 2012 4:14 pm)
Rating: 5.26%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: ADC Averaging macro

Postby daveb0360 » Mon Aug 20, 2012 4:10 pm

Hi Enamul & thanks.

I regret, this is close but not quite what I wanted.
The final value 'degC' is only required to update after each count of 8 without returning to zero inbetween.
so...it begins
read temp (int) store as temp1
read temp again......add to temp1 - loop 8 times
at the end of the 8 loops, loop exits with the sum of .......
Hell, I think I worked it out......see file attached

Cheers

Dave
Attachments
AVG_ADCV1.fcf
(5 KiB) Downloaded 184 times

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: ADC Averaging macro

Postby daveb0360 » Mon Aug 20, 2012 4:14 pm

Hi Enamul,
Now then, since I can't export this 'main' routine as a macro, and I can't cut and paste from one instance of FC to another.........do I have to write it all out in my main program?
Or is there a trick shortcut?

Funny one this.......just being lazy....lol

Dave

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: ADC Averaging macro

Postby daveb0360 » Mon Aug 20, 2012 4:17 pm

Hi again..........ermmmm
Just ignore me......you can cut and paste from another instance of FC.......just done it....get a warning about unreferenced variables but it works...
Genius !!

Simples......to quote our famous meerkat friends...

Dave

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC Averaging macro

Postby Enamul » Mon Aug 20, 2012 4:34 pm

Hi Dave,
I am bit confused by all your posts...is that sorted now? I have posted one like your post earlier but I have edited that bcz....

Code: Select all

degC = tempnew>>3

for first time tempnew=0..so it holds only Temp*8.
But next time while the loop runs it had already tempnew=Temp/8..this time after 8 loops tempnew = (new Temp*8)+ old tempnew...

That's why I put tempnew = 0 at the end of loop..In this program although it seems you are loosing the value of tempnew but actually you retained that in degC. But next time you get tempnew initiated with 0.

Hope this is clear now :)
Attachments
AVG_ADCV2.fcf
I have changed back to V1
(5 KiB) Downloaded 202 times
These users thanked the author Enamul for the post:
daveb0360 (Mon Aug 20, 2012 5:04 pm)
Rating: 5.26%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: ADC Averaging macro

Postby daveb0360 » Mon Aug 20, 2012 5:03 pm

Hi Enamul,
Yes I am clear now thanks so much. :D
The last file you sent, behaves the same with or without the first 'tempnew=0' statement. :?

I didn't realise you could put 2 lines of instruction in a single calculation icon....duhhh. Something else learnt!

Project is working as expected so far.
In the main program, I call a single macro called 'measurements' this macro, measures both voltage adc and temperature adc just once, in each call. The value of the integer variables returned from the measurement macro will then be used to control the rest of the program.
I will call this measurement macro at regular intervals, during times when led is not lit.......for better accuracy.

I hope this structure is seen as good practice and is efficient with code. I will see later on when I complete it but it simulates perfectly so far.

Thanks to you and Martin......bear with me, I'm learning fast but have to accept, it's not my strongest set of skills...especially at my age.

Dave
These users thanked the author daveb0360 for the post:
Enamul (Mon Aug 20, 2012 5:29 pm)
Rating: 5.26%
 

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC Averaging macro

Postby Enamul » Mon Aug 20, 2012 5:14 pm

Yes..I do agree you are learning fast. Keep it up. :)
The last file you sent, behaves the same with or without the first 'tempnew=0' statement.
I didn't realise you could put 2 lines of instruction in a single calculation icon....duhhh. Something else learnt!

Sorry :oops: it's my fault..I didn't noticed that you put tempnew = 0 in main loop...that's why I put in the bottom of main loop...your V1 is fine and ok. Thanks for letting me know..
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8536
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2456 times
Been thanked: 3525 times
Contact:

Re: ADC Averaging macro

Postby medelec35 » Mon Aug 20, 2012 5:30 pm

daveb0360 wrote:you can cut and paste from another instance of FC.......just done it....get a warning about unreferenced variables but it works...
Genius !!

Hi Dave, when you copy and paste a flowchart, If variables have not been created before pasting flowchart, then you will have errors when compining e.g:

Code: Select all

Flowcode1.c(84:2): error: unknown identifier 'FCV_DEGC'
Flowcode1.c(84:2): error: invalid operand 'FCV_DEGC'
Flowcode1.c(84:11): error: failed to generate expression
Flowcode1.c(93:2): error: unknown identifier 'FCV_TEMP'
Flowcode1.c(93:2): error: invalid operand 'FCV_TEMP'
Flowcode1.c(93:11): error: failed to generate expression
Flowcode1.c(99:2): error: unknown identifier 'FCV_DEGC'
Flowcode1.c(99:2): error: invalid operand 'FCV_DEGC'
Flowcode1.c(99:11): error: failed to generate expression
Flowcode1.c(123:2): error: unknown identifier 'FCV_DEGC'
Flowcode1.c(123:2): error: invalid operand 'FCV_DEGC'
Flowcode1.c(123:11): error: failed to generate expression
Flowcode1.c success

failure

........
Return code = 1

You would also get a warning after pasting:
warning.png
warning.png (28.13 KiB) Viewed 5146 times

If you want to copy a macro which will include all the variables, then best way is to use macro menu then import/export
For how do do this with Main, just create a new macro and call it Main1 for example, copy the whole flowchart in Main and paste in Main1.
Just export Main1.
In your new flowchart just import Main1 macro, then highlight all, copy and paste in Main. then delete Main1 macro
I know it may sounds long winded but less hassle in the end.

Another way is after pasting chunk of flowchart, double click on calculation box, highlight a variable name, Click Variables, Add New Variable, then paste variable name.

Martin
These users thanked the author medelec35 for the post (total 2):
Enamul (Mon Aug 20, 2012 5:50 pm) • daveb0360 (Mon Aug 20, 2012 5:59 pm)
Rating: 10.53%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC Averaging macro

Postby Enamul » Mon Aug 20, 2012 5:48 pm

Just ignore me......you can cut and paste from another instance of FC.......just done it....get a warning about unreferenced variables but it works...

You can follow the way as Martin said for transferring variable between two FCs...which is pretty useful when you have numerous variables and need to re-declare in the new FC.
But for one like this one where I have used just 2/3 variables you can simply copy paste icons but need to declare those variables which are not declared in the new FC...In simulation although it runs after one warning as you said in your post..But if you compile the code for generating hex, it will show error as shown by Martin. So if you get this warning it good to resolve that instantly by declaring the variable in new FC...
But remember to create the variable in same type as that in the original FC from where you are copying otherwise if you declare an int as byte compiler might not say anything but in hardware you will see wrong behavior.
These users thanked the author Enamul for the post:
daveb0360 (Mon Aug 20, 2012 6:09 pm)
Rating: 5.26%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: ADC Averaging macro

Postby daveb0360 » Mon Aug 20, 2012 6:09 pm

Thanks guys,
I got the warning about the variables and immediately understood what it was saying. Modified, added the necessary variables and trialled a compile.......no errors !! (I'd be on my bugle now if I had one... :lol:

Now just the switch to configure.....(left the hardest bit till the end.. :) )

Wish I could just replicate Martin's from earlier versions but this timer thingy completely loses me...... :oops:

Oh well, here goes.

Would I be right in saying that the timer activated led pattern, switchy thing (excuse the description!) is the best route forwards because it functions independently of the rest of the code?.........If that's the case, then I need to learn it's use and implementation otherwise I will compromise other bits of my code which rely on timings......hmmmmmm :roll:

Dave

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC Averaging macro

Postby Enamul » Mon Aug 20, 2012 6:27 pm

Hi Dave,
Actually I didn't interfere although I have seen you guys struggling with LED pattern as Martin was helping you and he knows very well what is he doing. As you are trying to learn and make your own code with the help from forum, I come forward although me and Martin (I guess) both understand you are trying to sort your earlier issue step by step. I really appreciate that as this is the great way of learning.
Now comes to your LED pattern issue..I personally deals with numerous control system where timing is an issue but not such critical that you need monitoring and controlling in us or ms environment. In those case I never use TMR although it's a great tool to use. Some of expert friends will say why? I will say it's just experience!. I have couple of bitter experience of using that..That's why I prefer to bit slow down in simple logic but much more stable in execution.
To my knowledge, I understood that your application is not a critical one where timing is a great issue rather I feel stability will be a great concern. As your dealing with water heater which is ultimately used by human being! Any failure in the control system could cause danger.

That's why simple LED and control pattern I would prefer. I would rather concentrate how could I assure things like if PIC hangs or stuck in any situation how my circuitry could avoid that unwanted condition. This is the reason I have posted LED pattern using switch logic which is pretty easy to understand..so if you feel you want to add the LED pattern using switch or any simple logic let me know..I won't prefer using TMR0 or TMR1 for LED or Heater control logic.
Enamul
University of Nottingham
enamul4mm@gmail.com

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: ADC Averaging macro

Postby daveb0360 » Mon Aug 20, 2012 6:37 pm

Hi Enamul,
I never thought you interfered at all ! please don't think that.
Martin and yourself have been tremendously helpful.

I went my own way because, for whatever reason, Martin and I could not get the code to work reliably and it was eating up loads of his time! I haven't lost faith, I just didn't have the skills to debug the code. That's reason number 1.
Reason number 2 is simply that I didn't just want to 'use' you guys to design my controller for me, the idea is to learn.......so I hugely appreciate all your time and efforts in teaching these things.

I forgot, you already proposed code for the led patterns.....I will go back and have a look at how it works and see if I can configure it for use here rather than you go off and do it all over again.
So far, my code is using far less than half the space on chip.........a good sign?

Cheers
Dave

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ADC Averaging macro

Postby Enamul » Mon Aug 20, 2012 6:56 pm

Yes.of course an efficient program will always use minimum possible memory while doing all necessary works. That's pretty good to know that you have still good amount of memory left.
About Martin, he is undoubtedly nice and helpful guy. I have learnt so many things from his post in last few weeks I am in the forum.
Enamul
University of Nottingham
enamul4mm@gmail.com