Processing of measured values

Go To Last Post
32 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys,

 

I have application when i measure temperature.

I want i write 10 values and after that 11 value write on place of 1 value i measure.

 

i thought i use 10 dimensional array, but i dont know how to repeat this process over and over.

 

Or is a more elegant solution how to get 10 values and after that rewrite it from 1?

 

Thank you for your ideas.

 

 

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You want a 1 dimensional array of 10 entries!

 

Why keep the last 10 values? Do you want to draw a graph or average the values?


#define NUM_TEMPS 10
int8_t temps[NUM_TEMPS];

void tempArray(int8_t temp)
{
    static int index = 0;
    
    temps[index++] = temp;
    if (index >= NUM_TEMPS)
        {
            index = 0;
        }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, my mistake with that dimensions.

 

I want my ATmega connected with PC.

When requirement from App in PC come to ATmega sends last value and from that array calculeted expected value.

 

I use serial bus UART/RS485, so if i measure continually i will utilize interuption from UART right?

After that i jump back to measurement and filling the field.


 

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It sounds like what you need is a so-called "Ring Buffer" or "Circular Buffer"

 

https://en.wikipedia.org/wiki/Circular_buffer

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Feb 13, 2020 - 10:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You really don't need to sample temperature fast - unless you're dealing with chemical reactions or nuclear physics! Once per second is oversampling!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Final application should be used for compressor testing.

Temperature and humidity is measured in this testing.

 

So you have right, i dont need many samples.

 

It depends how i set frequency of bus.

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR_Castieloo wrote:
It depends how i set frequency of bus.

Why?

 

Surely, the sample rate is defined according to what you're trying to measure? That's what defines the requirement.

 

You then choose a " frequency of bus" that's suitable for that requirement

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah its that what i need.

 

But its almost that same like wrote Kartman.

 

Difference is that there is not pointer on the space where he write.

 

 

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think Kartman just gave you a very basic outline to illustrate the idea - it's not a complete solution.

 

  • You can't tell how many samples are available in the array
  • You don't know which is the most recent

 

The additions to support those would end up giving you a Circular Buffer!

 

Difference is that there is not pointer

You can implement a Circular Buffer just as easily using indexes as with pointers.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't make a clear case...if you are sending the temperatures to the PC , why does your avr need a dimensional array  or anything else?

 

1) You send a command to the avr to read temperature & it replies with the reading. 

2) The PC can do what it wants with all of the readings...make plots, averages, min/max, calculations, etc.

 

Do you need to do something different than 1) 2)?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that none of this is specific to AVR: it is all standard data handling stuff - it would be the same on any platform.

 

A useful benefit of this is that you could test it & experiment with it on a PC - without all the added complications of the AVR.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got a reguest for continuous measurement of temperature and humidity and when superior App reguest for data ATmega sends value of humidity, temperature and expected value from circular buffer.

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i will try in Visual Studio thank you :)

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
A useful benefit of this is that you could test it & experiment with it on a PC - without all the added complications of the AVR.

For example,

 

see: https://www.avrfreaks.net/commen...

 

and: https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
if you are sending the temperatures to the PC , why does your avr need a dimensional array  or anything else?

Indeed.

 

Only reason would be if the PC was not permanently connected - ie, the AVR is a data logger, so the PC is retrieving the logged data ...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR_Castieloo wrote:
I got a reguest for continuous measurement

You need to define, "continuous"

 

ATmega sends value (sic?) of humidity, temperature

Just one value, or a list of historical values ?

 

 

 from circular buffer

 

So "Circular Buffer" had been in the requirement from the start??

 

Why did you not say that?!

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR measure and display data on LCD.

 

AVR is connected with PC and when user want data, AVR sends data to PC.

 

Its case 2, but ATmega running independet on the PC, just when is interruption sends data do PC and after that returns to measurement.

 

 

 

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So why the circular buffer? why not just send the latest data? What is this 'interruption' you speak about?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If think in practice that it is not connected constantly to PC.

 

But if user want he connect and downloand data.

 

So we can say it "data logger " with LCD display.

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not much of a data logger with only 10 readings!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I know i can just sends measured data to PC and there do what i want.

 

But i got requirement that calculation was done in ATmega.

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tell my teacher ..

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In future, be upfront with the requirements. Don’t spoon feed us little bits of information.
So, do you still have a question?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry guys.

 

Just one.

 

Is usseless create that "Circular buffer" when ATmega is still connected to PC right?

 

But if I have to do the calculations in ATmega is this only way?

 

Thank you

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A circular buffer is used to ‘decouple’ moving data from one task to another. By ‘decoupling’ we mean the consumer is not tied to the rate of the producer. The producer can put data into the circular buffer when he chooses and the consumer can do the same. The requirement is that the consumer has to consume the data the same or faster than the producer can produce it as an aggregate otherwise the buffer will overflow.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
A circular buffer is used to ‘decouple’ moving data from one task to another. By ‘decoupling’ we mean the consumer is not tied to the rate of the producer. The producer can put data into the circular buffer when he chooses and the consumer can do the same. The requirement is that the consumer has to consume the data the same or faster than the producer can produce it as an aggregate otherwise the buffer will overflow.

 

Can you just produce ‘data’ and in some point just use all data in buffer?

Can i with this solution overwrite ‘data’ with new ‘data’?

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But i got requirement that calculation was done in ATmega.

What calculation?

 

It is best for you to start over & this time give a complete & clear explanation of what you are trying to do & why. 

 

Provide a complete paragraph or two so there will be enough details to discuss.  Giving out one sentence here & there  leads to problems understnading. 

 

 

 

 

 

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. Feb 13, 2020 - 01:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR_Castieloo wrote:
i thought i use 10 dimensional array, but i dont know how to repeat this process over and over.
The key thing is that if you have an array with elements [0] .. [9] that when the index reaches [9] and you are about to make it [10] (which does not exist) then, instead, you set it back to 0 so the oldest [0] element now starts to get over-written. This makes the array "circular" or, more commonly known as a "ring".

 

But like others here I think you need to take a step back and don't worry about the actual implementation just yet but spend some more time thinking about the overall design and what you want to achieve. How often wlil readings be taken, how many should be buffered/held, how does the final delivery to the PC proceeed, how long will the device be disconnected from the PC, how often will it get a chance to connect to the PC, will it ever lose power, does it need to maintain "readings so far and not delivered to PC" even if the power goes etc etc

 

Only when you have all the questions and all the sensible answers can you decide what you actually want the thing to do. THEN you can worry about how you actually implement things.

 

The usual goal with "data loggers" is to actually store as much as the resources will allow (not some arbitrary choice like 10) and in many further "external", non-volatile storage may be added so days or weeks of data can be held while the device is "out in the field".

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

But i got requirement that calculation was done in ATmega.

What calculation?

 

It is best for you to start over & this time give a complete & clear explanation of what you are trying to do & why. 

 

Provide a complete paragraph or two so there will be enough details to discuss.  Giving out one sentence here & there  leads to problems understnading. 

 

 

I have App which measures and displays values on the LCD. (humidity and temperature)

I must program some more stuff.

 

Data transfer to PC with UART/RS485 with using interruption.

Data should include measurement uncertaity of humidity and temperature also from last 10 values calculeted expected value.

They told me measurement should be continually.

 

 

 

 

 

 

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of course! You write the code to implement the behavior you want.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR_Castieloo wrote:
from last 10 values calculeted expected value
"expected"? Are you talking about implementing a Kalman filter or something?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok, now you are making some sense....the expected value is also known as the average value.  

 

So you want to have a moving average of the last 10 values....you then do need to keep track of the values so you can "remove the oldest" and add the newest.   A ring buffer with ten positions (chairs) is perfect. 

 

Then you can simply take the average of all the values in the ring.  There are "smarter" ways, where you don't have to reread all the locations, since you are only removing the oldest & adding the newest (so 9 of them are unchanged)...but just working all 10 is the simplest & recommended.  If you had a million locations this would be a major difference in efficiency, since 999999 remain the same

 

To form the ring, always point to the oldest of positions 0-9...say position 3.  A new number comes in & replaces whatever is at 3.  Now you point to position 4 & repeat the same idea.  Rather than going from position 9 to 10 (position 10 does not exist), you go to position 0.

So the ring (locations 0 to 9) contain the last 10 readings, which you can average & send to the PC whenever you want. 

 

Here is an example...note newest pointer is not needed, only oldest (or really either one)

 

     34      67      88      99     23      55      67 

                         ||       ||

                   newest  oldest   ....get a 77:

 

     34      67      88      77     23      55      67 

                                  ||       ||

                            newest  oldest ...get a 41

 

 

     34      67      88      77     41     55      67 

                                           ||      ||

                                    newest  oldest ...get a  25

 

     34      67      88      77     41      25     67 

                                                    ||       ||

                                              newest  oldest ...get a 56

 

     34      67      88      77     41      25     56 

     ||       ||

newest  oldest ...get a 56

   

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. Feb 13, 2020 - 02:02 PM