remove a zero

Page: << < ..678 > Showing page 6 of 8
Author
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3118
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/02 15:39:22 (permalink)
0
Typo, I just copied the op code.
 
Every time you enter the interrupt the value <reading> (your code)
is added to total.  After 4 calls, total is divided by 4 to get your average.
 
static keeps the vars safe in memory so that when you return to the interrupt the last value will still be in memory.
"volatile int avg" (average) is a global var that you can read to see the average.
 
That are better ways to do it since you only get the average every 4 calls and it will not be up to date.
A running total and average would be better.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3118
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/02 15:55:58 (permalink)
0
I find it useful to get the lowest and highest temperature.
low=1000, high=0;
...
t=rtc_gettemp();
if(t>high) high=t;
if(t<low) low=t;
After one day timestamp and save that to eeprom then reset high and low.
Read the temperature every ten minutes.  144 readings in 24hrs.  Get the average from that rather that every 4.  You can work out dips and spikes and even plot graphs.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
PStechPaul
Super Member
  • Total Posts : 2155
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: online
Re: remove a zero 2018/03/02 19:03:51 (permalink)
0
Rolling average of 5 samples:
 
Average = ( 4 * Average + NewReading ) / 5
 
For readings of 1000:
 
200,
360, 488, 590, 672,
738, 790, 832, 865,
892, 914, 931, 945,
956, 965, 972, 982,
986, 988, 991, 993
 
This is analogous to an R-C time constant, which is the time where the reading reaches 63% of its final value. The rolling average reaches this in 4 readings, or a time of four. The effective TC of the Samples-1. The formula is:
 
V(t) = V0 * ( 1 - e^(-t/RC) )
 
For RC = 5, V0 = 1000:
 
t=1  V(t) = 181
t=5  V(t) = 632
t=10  V(t) = 865
t=15  V(t) = 950
t=20  V(t) = 982
t=25  V(t) = 993
 
The corresponding values of the calculated rolling average every four samples are:
 
5: 672
9: 865
13: 945
17: 982
21: 995
 
Thus the "rule of thumb" that 5 time constants provides a value within 1%.

 
1and0
Access is Denied
  • Total Posts : 8842
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: remove a zero 2018/03/02 20:03:35 (permalink)
0
NorthGuy has a rolling average formula that does _not_ involve multiplication or division.  Search this forum.
 
Edit: Here: http://www.microchip.com/forums/FindPost/1007458
post edited by 1and0 - 2018/03/02 23:43:55
kurt
Junior Member
  • Total Posts : 110
  • Reward points : 0
  • Joined: 2017/05/07 13:18:17
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 04:50:39 (permalink)
0
that dividing is not the issue,  i want to know how i transfer the measured value every time the interrupt occurs to 
the next measure point
1and0
Access is Denied
  • Total Posts : 8842
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: remove a zero 2018/03/03 05:20:19 (permalink)
0
Do you want to average every four new readings or average the four most recent readings?  The latter is a rolling average.
kurt
Junior Member
  • Total Posts : 110
  • Reward points : 0
  • Joined: 2017/05/07 13:18:17
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 12:23:59 (permalink)
0
with my siemens plc i would use another approach i find it 'funny' to see that there are different roads to the same target, especially that rolling average, and that it can be so simple.
for Gort2015,  i understand the logica in which your code should work but are you sure that the program from which you copied this works, because the part that keeps track of the 'four calls' seems not so working to me. 
 
by looking some more.  i think it's my misunderstanding of some techniques.  i encountered some more but has anyone a manual or link to a site in which they describe all such things like :'++', '!--', '+-' such of these i know but just to give the example  
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: remove a zero 2018/03/03 13:52:28 (permalink)
0
kurt
...
by looking some more.  i think it's my misunderstanding of some techniques.  i encountered some more but has anyone a manual or link to a site in which they describe all such things like :'++', '!--', '+-' such of these i know but just to give the example 

These are basic operators in the C language.
ANY C textbook should document them.
 
"++" is the increment operator. It can appear before or after a variable, which does make a difference.
Before; means the variable is incremented, then used elsewhere in the expression.
After; means the value of the variable is used in thte expression, then when everything else is done the variable is incremented.
 
"!--" is in fact two operators next to each other.
"!" is a logical "not" condition, so it negates the following logical value (true or false)
"--" is the decrement operator. It can appear before or after a variable, with the same rules as for "++"
"!--" together will decrement the following variable, then take the logical "not" condition on the result.
zero is false, non-zero is true, so "!--" will give "true" if the result of the decrement is zero.
 
"+-" I'm not sure where you saw this. It's probably just a normal "+" before a negative number.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
kurt
Junior Member
  • Total Posts : 110
  • Reward points : 0
  • Joined: 2017/05/07 13:18:17
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 14:17:44 (permalink)
0
so what exactly happens in these two codes 
 
-static int cnt = 4 - 1, total = 0;
 
-if(!--cnt){cnt = 4 - 1; avg = total << 2; total = 0;
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: remove a zero 2018/03/03 14:22:40 (permalink)
0
-static int cnt = 4 - 1, total = 0;

Two "static int" variables are created. "cnt" and "total".
"static" means they will retain their values between calls to the function.
The initial value of "cnt" will be 4-1, which is 3.
The initial value of "total" will be 0
 
if(!--cnt){cnt = 4 - 1; avg = total << 2; total = 0;}

if(!--cnt) -> the "cnt" variable is decremented. If the value after decrementing is zero, then all the code between "{}" is executed.
That means it will only happen each 4th time this code is called.
 
 
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: remove a zero 2018/03/03 14:24:40 (permalink)
0
The second line could be more readable as:
if (! --cnt)
{
    cnt = 4 - 1;    // initialise to count 4 more loops
    avg = total / 4;  //calculate the average of four values
    total = 0;    //prepare to start again
}

 
Note I have corrected the divide by 4 code.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3118
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 14:41:09 (permalink)
0
Type into Google: c unary operators.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
jtemples
Super Member
  • Total Posts : 11098
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: remove a zero 2018/03/03 14:47:20 (permalink)
+1 (1)
"+-" I'm not sure where you saw this. It's probably just a normal "+" before a negative number.


Useless trivia: C does not actually have negative constants; the - is the same unary minus operator you could apply to any object.  It's not syntactically part of the constant, so something like "x  +-  1" is legal.
kurt
Junior Member
  • Total Posts : 110
  • Reward points : 0
  • Joined: 2017/05/07 13:18:17
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 15:06:43 (permalink)
0
but why    cnt = 4 - 1;   and not cnt = 3;  (you say this is the same)
 
you say: 'if the value after decrementing is zero, then all the code between "{}" is executed.
That means it will only happen each 4th time this code is called.'
 
so if i calculate, we start with cnt= 3   code is called 1 time cnt =2, code is called second time so cnt = 1 code is called third time so cnt = 0           "code between "{}" is excecuted"   which is not 4th time
1and0
Access is Denied
  • Total Posts : 8842
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: remove a zero 2018/03/03 15:11:30 (permalink)
0
qɥb
if(!--cnt){cnt = 4 - 1; avg = total << 2; total = 0;}

if(!--cnt) -> the "cnt" variable is decremented. If the value after decrementing is zero, then all the code between "{}" is executed.
That means it will only happen each 4th time this code is called.

I think the initial/reset value for 'cnt' should be 4 instead of 3.
 
Edit: Kurt beats me to it.
1and0
Access is Denied
  • Total Posts : 8842
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: remove a zero 2018/03/03 15:19:58 (permalink)
0
Kurt, the corrected long version of Gort code is this
static int total = 0;
static unsigned char cnt = 4;
 
total = total + new_reading;
cnt = cnt - 1;
 
if (cnt == 0) {
  cnt = 4;
  avg = total / 4;
  total = 0;
}

Notice I've changed the type of cnt to 8-bit.
 
Edit: As other has pointed out, <<2 is multiplying by 4, not dividing by 4.
post edited by 1and0 - 2018/03/03 15:41:33
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3118
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 15:41:37 (permalink)
0
The reason I put cnt = 4 - 1 is to make it easier to read.  If I had put 3 then someone reading that might thing there are only 3 iterations.
If you initialize an array to 10, you would have elements 0 to 9 (Not 1 to 10).
It's just the sort of thing you do in coding.
add
cnt = 3
add
cnt = 2
add
cnt = 1
add
cnt = 0  job done...

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
1and0
Access is Denied
  • Total Posts : 8842
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: remove a zero 2018/03/03 15:47:43 (permalink)
0
Gort2015
The reason I put cnt = 4 - 1 is to make it easier to read.  If I had put 3 then someone reading that might thing there are only 3 iterations.
If you initialize an array to 10, you would have elements 0 to 9 (Not 1 to 10).
It's just the sort of thing you do in coding.
add
cnt = 3
add
cnt = 2
add
cnt = 1
add
cnt = 0  job done...

The initial/reset value for 'cnt' should be 4 for four readings:
 
cnt = 4
add, --cnt = 3
add, --cnt = 2
add, --cnt = 1
add, --cnt = 0, done
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3118
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 15:51:27 (permalink)
0
Edit: As other has pointed out, <<2 is multiplying by 4, not dividing by 4.
 
I made a typo, stick to division.
 
 
Probably because I was writing some code at the same time that involved a lot of shifting.
I get in the habit of doing that since division (time critical) is not always feasible and powers of 2 are.
 
I was re-writing a binary converter that involved some complex code.
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3118
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: remove a zero 2018/03/03 15:57:01 (permalink)
0
That is what happens when you type code in without double checking.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
Page: << < ..678 > Showing page 6 of 8
Jump to:
© 2019 APG vNext Commercial Version 4.5