Hello guys,
this question was probably often asked. I want to measure the magnetic field for just 500 measurements and then get the average value and save it. Its like that: The programm measures the field continuesly.. when i press the 1 on the serial monitor the programm should do 500 measurements and show me the average value of these. This one value should be saved. I dont have a proper code to show.. can you help me with any advices
A for loop and a sum variable big enough to contain the sum of 500 readings.
Before that, though, take a look at Serial Input Basics to see how to read in that 1 that you need. The first example in that tutorial uses a flag called newData and uses that in a function called showNewData(). I would stick the for loop stuff in there to make sure it happens once only, for the new 1. It gives the program a nice tidy look.
What does saved mean in this context?
By the associative property of multiplication over addition:
float averageVal = 0;
for (int i=0; i < 500; i++) {
averageVal += readSensor()/500.0;
}
I mean that the average value of the 500 measurements should be saved in a variable so it can be used later in a if clause…
If the signal is above the average value do this and this…
But I can’t figure out how to measure 500 measurements just once.. if I put it in void loop it will always measure 500 times and give new average values
Should this be put in void Setup? Because I just need this once.
I thought that's what you meant but it's important to be clear. If, for example, you did many averages over the life of the program, you might have meant that you wanted all of them to remain available and not overwrite each other, like in an array. Or you might have meant saved to an SD card for posterity....
Hello hyd
Post your current sketch, well formated, with comments and in so called code tags "</>" and a schematic, not a Fritzy diagram, to see how we can help.
Have a nice day and enjoy coding in C++.
Дайте миру шанс
p.s. My crystal ball is in the dishwasher this I can´t see your design goal.
Yep, stuff that needs to run once only goes in setup().
Why do so many floating-point divisions?
To prevent the need for storing more than one sample at a time and
using float here works regardless of the range or type of the sample (which also wasn't stated.)
No "efficiency" requirement was stated.
Premature optimization is the root of much evil.
Clarity of algorithm, and "most likely to work without further explanation."
What is so much unclear about
(X1+x2+x3....+x500)/500.0
?
It saves 499 divisions...
There is no evil in simplicity...
The OP said nothing about having to store the 500 individual samples, so just keep a running total of the samples and do one division at the end.
Ah. That would work fine, and is a valid complaint.
I think my original thought was to avoid possible integer overflow if the sum were kept as an int, but didn't re-think after I decided the sum should be a float.
... or why an average of 500 samples was needed
in which case dividing each sample by 500 would cost you a lot of accuracy: the last two or three digits of the "average" would be no good.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.