const int numReadings = 10;
int readings[numReadings]={0,0,0,0,0,0,0,0,0,0};
?
-----2.
What does this do? I don't see any last reading from previous code
// subtract the last reading:
total= total - readings[index];
-----3.
I think this is incorrect. if index=10, then won't readings[10] equals to some random null number?? since array starts from 0-9, 10 is a null. am I correct?
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
-----4.
I think this code is incorrect. There is no {} for this if statement, and so won't ""average = total / numReadings; "" be calculated every loop??
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
full code
/*
Smoothing
Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.
The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0
Created 22 April 2007
By David A. Mellis <dam@mellis.org>
modified 9 Apr 2012
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Smoothing
This example code is in the public domain.
*/
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
}
No, unless the loop is in "setup" and "readings" has global scope, in which case both forms are redundant and
const int numReadings = 10;
int readings[numReadings];
is sufficient.
What does this do? I don't see any last reading from previous code
It subtracts the oldest reading in the buffer.
I think this is incorrect. if index=10, then won't readings[10] equals to some random null number?
The trick here is to ensure that index != 10, and fortuitously
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
Edit: This code may fail for larger values of "numReadings", so I'd be inclined to make "total" an "unsigned long".
In fact, the "readings" buffer would be better as "unsigned int", but that's nit-picking.
No, unless the loop is in "setup" and "readings" has global scope, in which case both forms are redundant and
const int numReadings = 10;
int readings[numReadings];
is sufficient.
> What does this do? I don't see any last reading from previous code
It subtracts the oldest reading in the buffer.
> I think this is incorrect. if index=10, then won't readings[10] equals to some random null number?
The trick here is to ensure that index != 10, and fortuitously
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
still don't get it.
1, oldest reading in the buffer? these is no values assigned to "reading" from the previous code. why need to do this subtraction?
2, you answer is unclear to me. maybe I asked unclear?
3, if you look at this simple code, index is going to be 10. because index=9 is false for " if (index >= 10) "
const int numReadings = 10;
int readings[numReadings]={0,0,0,0,0,0,0,0,0,0};
?
No, unless the loop is in "setup" and "readings" has global scope, in which case both forms are redundant and
Code:
const int numReadings = 10;
int readings[numReadings];
is sufficient.
Quote
What does this do? I don't see any last reading from previous code
It subtracts the oldest reading in the buffer.
Quote
I think this is incorrect. if index=10, then won't readings[10] equals to some random null number?
The trick here is to ensure that index != 10, and fortuitously
Code:
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
still don't get it.
1, oldest reading in the buffer? these is no values assigned to "reading" from the previous code. why need to do this subtraction?
2, you answer is unclear to me. maybe I asked unclear?
3, if you look at this simple code, index is going to be 10. because index=9 is false for " if (index >= 10) "
index = index + 1;
if (index >= numReadings)
1). The oldest reading in the buffer will be zero. until you write something into it, because that's what you (or crt0) wrote to it.
2) ?
3) Read it again. Pay particular attention to index = index + 1;
const byte inputPin = A0;
const byte numReadings = 10;
unsigned int readings[numReadings];
int index;
long total;
void setup()
{
Serial.begin(9600);
}
void loop()
{
total -= readings[index];
readings[index] = analogRead(inputPin);
total += readings[index++];
// if we're at the end of the array...
if (index >= numReadings)
index = 0;
int average = (total + (numReadings / 2)) / numReadings; // with rounding
Serial.println(average);
delay(1); // delay in between reads for stability
}
Re: officially Smoothing example code doubt and question
« Reply #2 on: Today at 11:08:05 »
Bigger Bigger Smaller Smaller Reset Reset Reply with quoteQuote Modify messageModify Remove messageRemove Split TopicSplit Topic
Quote from: AWOL on Today at 10:54:08
Quote
Is this
Code:
const int numReadings = 10;
int readings[numReadings]={0,0,0,0,0,0,0,0,0,0};
?
No, unless the loop is in "setup" and "readings" has global scope, in which case both forms are redundant and
Code:
const int numReadings = 10;
int readings[numReadings];
is sufficient.
Quote
What does this do? I don't see any last reading from previous code
It subtracts the oldest reading in the buffer.
Quote
I think this is incorrect. if index=10, then won't readings[10] equals to some random null number?
The trick here is to ensure that index != 10, and fortuitously
Code:
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
still don't get it.
1, oldest reading in the buffer? these is no values assigned to "reading" from the previous code. why need to do this subtraction?
2, you answer is unclear to me. maybe I asked unclear?
3, if you look at this simple code, index is going to be 10. because index=9 is false for " if (index >= 10) "
index = index + 1;
if (index >= numReadings)
1). The oldest reading in the buffer will be zero. until you write something into it, becaue that's what you (or crt0) wrote to it.
2) ?
3) Read it again. Pay particular attention to index = index + 1;
I have read this code many times, and again, read it once more. I still find it problematic. I remove the comments, and clearly to me, " average = total / numReadings; " this line is running for every loop.
void loop() {
total= total - readings[index];
readings[index] = analogRead(inputPin);
total= total + readings[index];
index = index + 1;
if (index >= numReadings)
index = 0;
average = total / numReadings;
Serial.println(average);
delay(1);
}
AWOL:
If you have a problem, get a sheet of paper and a pencil, and run the code on the paper for a small value of "numReadings", say "4".
I remove the comments, and clearly to me, " average = total / numReadings; " this line is running for every loop.
I really cannot see why you think that is a problem.
is this
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
equals to this
// if we're at the end of the array...
if (index >= numReadings) {
// ...wrap around to the beginning:
index = 0; }
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
??
there is no brackets after the if function. It's confusing, but either ways I don't think it works.
The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code.
The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.
if (x > 120) digitalWrite(LEDpin, HIGH);
if (x > 120)
digitalWrite(LEDpin, HIGH);
if (x > 120){ digitalWrite(LEDpin, HIGH); }
if (x > 120){
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
} // all are correct
That's correct - in C, if there's only one conditional statement, you don't need the braces.
but either ways I don't think it works.
For the first numReadings, it doesn't return a true average, but after that, it's fine.
The fix is trivial
const byte inputPin = A0;
const byte numReadings = 10;
unsigned int readings[numReadings];
int index;
long total;
bool primed;
void setup()
{
Serial.begin(9600);
}
void loop()
{
total -= readings[index];
total += readings[index++] = analogRead(inputPin);
if (index >= numReadings)
{
index = 0;
primed = true;
}
if (primed)
{
int average = (total + (numReadings / 2)) / numReadings; // with rounding
Serial.println(average);
}
delay(1); // delay in between reads for stability
}
average is a+b+c/3=average, but here the average is a/3=average; b/3=average, c/3=average. because this line of code " average = total / numReadings; " will run for every loop, regardless what index number it's .
AWOL:
Start
numReadings = 4
readings = 0 0 0 0
index = 0
total = 0
First pass
readings = 500 0 0 0
index = 1
total = 500
average = 500 /4 = 125
Second pass
readings = 500 505 0 0
index = 2
total = 1005
average = 1005 /4 = 251
Third pass
readings = 500 505 495 0
index = 3
total = 1500
average = 1500 /4 = 375
Fourth pass
readings = 500 505 495 502
index = 0
total = 2002
average = 1500 /4 = 500
Fifth pass
readings = 480 505 495 502
index = 1
total = 1982
average = 1982 /4 = 495
thanks, It makes sense now. SO the average is being calculated before all the 10 numbers are acquired. I also run the code with my uno, and the result is really the real average. thanks for the time.