If you are making some measuring device with display it is always a challenge to select proper rate of refresh. Usually your measurement takes only small amount of time and it is very hard to resist updating display after each one. I saw number of devices that have displays that are just too fast to read.
Slowing rate at which measurement is taken is almost always beneficial for both user comfort and battery life. And that is valid solution, especially if value is relatively stable. However, if measurement fluctuates a bit, that results in jumps between values.
To cure that you should be doing averaging. If your measurement takes 10ms to complete, you can do 10 of them, average the result and still have quite a decent 10/second refresh rate. This is probably solution gets most use but it is not the only one.
My favorite way of slowing display is simplified weighted average. Between two measurements one that is current always carries more weight than newer one. Exact weight is matter of trial but I found small numbers like 23% work the best.
To clarify it a bit, let’s say that we have measurement of 10 and measurement of 20. Our new “average” value will become 12.3. If third measurement is also 20, value becomes 14.1, then 15.4 and so on. Value keeps getting closer and closer to real reading but speed with which it does that is very limited.
If you have measurements that are relatively stable this method works almost like an average. If value jumps occasionally this method will smooth such temporary change. That gives much nicer end user feeling as far as measurement goes. And since we are doing this at much faster rate than actually showing data, if permanent jump does occur, user will see such change relatively quickly.
Code for this might look like:
float value =measure();while(1){showValue(value);for(int i=0; i<10; i++){float newValue =measure();
value = value +(newValue - value)*0.23;//to smooth it a little
value =(int)(value *1000.0)/1000.0;//rounding//do other stuff}}
In this particular code, we show a value to user as soon as we can (to enhance perceived speed). After that we average next 10 values (each new one is given 23% of consideration). Then we display new average to user. Rinse and repeat. Optional rounding step additionally limits small changes.
This code is not that good if measurement takes a long time. E.g. If you have one measurement per second you will find it takes eternity to change a value. For such cases you are probably better off just displaying current measurement to user. Or, if some smoothing is required, using higher values (e.g. 0.79 or similar).
P.S. This method might not work as expected for your measurements. Do test first.
P.P.S. This is intended for human display only. If you are logging values, it is probably best to write measurements without any adjustment. If you average them before writing, you are losing details.
P.P.P.S. If you are doing averaging in full-blown desktop application, ignore this code. You can use proper moving average (linear, exponential, weighted…) that allows for much greater control. This method is just a workaround to get similar results when working on memory-limited PIC.
When I am reviewing code, I always check how program retrieved items from dictionary. Most often I find following pattern:
if(dict.ContainsKey(key)){object obj = dict[key];//do something with obj}
Whenever I see it, I wonder why somebody would use it over TryGetValue:
object obj;if(dict.TryGetValue(i,out obj)){//do something with obj}
If you deal with dictionary of objects, later code is something around 25% (depends on size of dictionary and bunch of other stuff) faster. If you deal with dictionary of structures, difference is much smaller (7-8%) since in later case you need to deal with memory allocations (remember that there is no null for structures).
Most of the time, dictionaries are used in time critical code so changing code to later is almost a no-brainer.
I only ever came to see one single scenario where key check separated from retrieval is desired. On case your dictionary has structures in it and you expect lot of look-up failures you will be better off using first example. Second code will use much more memory and need to create structure before each key check will offset any time savings you might get when item is found.
Next version of Windows will be cheaper. At least that is how I understand it after Windows 8 engineering team brought us nice DVD playback guide.
DVD playback has it’s cost. And that cost was shared among all versions of Windows. Microsoft stated that $2 will get you MPEG-2 decoder. And frankly that is probably everything you need since PCM codec is available on every DVD for your stereo. Since Dolby codec is nice thing to have, let’s say that it costs additional $2 (Microsoft didn’t state cost).
My expectation is that next version of Windows will cost $4 less than Windows 7. If you are among those Windows users with lot of legacy MPEG-2 based media, you can upgrade your Windows for $5 (additional $1 is for covering distribution cost). I think that everybody wins!
And I am taking bets.
Who really thinks that Windows will be cheaper and that you will be able to buy DVD playback option for $5?
Last half of year I live in a hotel. I do not have my workbench here nor do I have extensive electronics part collection but I do have some basic equipment. And I do use it.
Pictured here is last thing that I made - voltage and current monitor. It has one input, three outputs, display and two switches (that I forgot to order). On bottom there is an PIC, sense and LED resistors. Pretty simple SMD board (45x72 mm) as it goes.
What it does? Basically it just displays voltage, current or power on it’s display for selected input or output.
Once I assembled it, I left it turned on for a while, pressing button here and there. Ok, since buttons are missing, pressing is probably not the correct word for what I was doing, but you get the drift. There is nothing like long-term abuse to bring code bugs out.
As I was leaving for work, I took one last glance at device, stopped, powered it off and hid it in the drawer. Call me crazy but I haven’t dared to leave it on like that for cleaning lady to see.
Somehow I think that she would only see some clock-like device with some wires coming out and numbers that change. Even more innocent-looking things were misidentified.