[Solved]officially Smoothing example code doubt and question

I have been read some officially example codes and notice some problem from one example.
-----1.
Is this

  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;

equals to this

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            
}

Is this
Code:

for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

equals to 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

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.

I see no reason to give "average" global scope.

AWOL:

Is this
Code:

for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

equals to 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

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) "

index = index + 1;
if (index >= numReadings)

Quote from: AWOL on Today at 10:54:08
Quote
Is this
Code:

for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

equals to this

Code:

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, because that's what you (or crt0) wrote to it.
2) ?
3) Read it again. Pay particular attention to index = index + 1;

Edit: tidy-up

(uncompiled, untested)

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            
}

AWOL:

Personal Message (Online)

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:

for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

equals to this

Code:

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);                 
}

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.

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

there is no brackets after the if function.

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            
}

AWOL:

there is no brackets after the if function.

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 .

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 = 2002 /4 = 500 (Edit: oops)

Fifth pass
readings = 480 505 495 502
index = 1
total = 1982
average = 1982 /4 = 495

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.

reading:404
total:404
---average::40
reading:352
total:756
---average::75
reading:320
total:1076
---average::107
reading:305
total:1381
---average::138
reading:297
total:1678
---average::167
reading:294
total:1972
---average::197
reading:291
total:2263
---average::226
reading:293
total:2556
---average::255
reading:294
total:2850
---average::285
reading:295
total:3145
---average::314
reading:296
total:3037
---average::303
reading:297
total:2982
---average::298
reading:298
total:2960
---average::296
reading:299
total:2954
---average::295
reading:302
total:2959
---average::295
reading:303
total:2968
---average::296
reading:303
total:2980
---average::298
reading:304
total:2991
---average::299
reading:305
total:3002
---average::300
reading:306
total:3013
---average::301
reading:307
total:3024
---average::302
reading:305
total:3032
---average::303
reading:305
total:3039
---average::303
reading:304
total:3044
---average::304
reading:302
total:3044
---average::304
reading:299
total:3040
---average::304
reading:298
total:3035
---average::303
reading:294
total:3025
---average::302
reading:292
total:3012
---average::301
reading:290
total:2996
---average::299
reading:289
total:2978
---average::297
reading:286
total:2959
---average::295
reading:282
total:2936
---average::293
reading:283
total:2915
---average::291
reading:282
total:2895
---average::289
reading:284
total:2880
---average::288
reading:285
total:2867
---average::286
reading:289
total:2862
---average::286
reading:289
total:2859
---average::285
reading:292
total:2861
---average::286
reading:292
total:2864
---average::286
reading:294
total:2872
---average::287
reading:294
total:2884
---average::288
reading:295
total:2896
---average::289
reading:303
total:2917
---average::291
reading:303
total:2936
---average::293
reading:306
total:2957
---average::295
reading:307
total:2975
---average::297
reading:305
total:2991
---average::299
reading:305
total:3004
---average::300
reading:304
total:3016
---average::301
reading:302
total:3024
---average::302
reading:299
total:3029
---average::302
reading:299
total:3033
---average::303
reading:297
total:3027
---average::302
reading:294
total:3018
---average::301
reading:292
total:3004
---average::300
reading:290
total:2987
---average::298
reading:288
total:2970
---average::297
reading:286
total:2951
---average::295
reading:284
total:2931
---average::293
reading:279
total:2908
---average::290
reading:280
total:2889
---average::288
reading:278
total:2868
---average::286
reading:283
total:2854
---average::285
reading:286
total:2846
---average::284
reading:289
total:2843
---average::284
reading:292
total:2845
---average::284
reading:295
total:2852
---average::285
reading:297
total:2863
---average::286
reading:299
total:2878
---average::287
reading:302
total:2901
---average::290
reading:304
total:2925
---average::292
reading:304
total:2951
---average::295
reading:306
total:2974
---average::297
reading:305
total:2993
---average::299
reading:304
total:3008
---average::300
reading:304
total:3020
---average::302
reading:301
total:3026
---average::302
reading:299
total:3028
---average::302
reading:298
total:3027
---average::302
reading:295
total:3020
---average::302
reading:291
total:3007
---average::300
reading:289
total:2992
---average::299
reading:285
total:2971
---average::297
reading:283
total:2949
---average::294
reading:282
total:2927
---average::292
reading:280
total:2903
---average::290
reading:285
total:2887
---average::288
reading:287
total:2875
---average::287
reading:291
total:2868
---average::286
reading:295
total:2868
---average::286
reading:297
total:2874
---average::287
reading:300
total:2885
---average::288
reading:301
total:2901
---average::290
reading:303
total:2921
---average::292
reading:304
total:2943
---average::294
reading:303
total:2966
---average::296
reading:304
total:2985
---average::298
reading:303
total:3001
---average::300
reading:302
total:3012
---average::301
reading:298
total:3015
---average::301
reading:295
total:3013
---average::301
reading:293
total:3006
---average::300
reading:296
total:3001
---average::300
reading:293
total:2991
---average::299
reading:293
total:2980
---average::298
reading:294
total:2971
---average::297
reading:293
total:2960
---average::296
reading:293
total:2950
---average::295
reading:290
total:2938
---average::293
reading:291
total:2931
---average::293
reading:289
total:2925
---average::292
reading:292
total:2924
---average::292
reading:292
total:2920
---average::292
reading:291
total:2918
---average::291
reading:291
total:2916
---average::291
reading:292
total:2914
---average::291
reading:292
total:2913
---average::291
reading:292
total:2912
---average::291
reading:293
total:2915
---average::291
reading:294
total:2918
---average::291
reading:299
total:2928
---average::292
reading:301
total:2937
---average::293
reading:304
total:2949
---average::294
reading:304
total:2962
---average::296
reading:304
total:2975
---average::297
reading:303
total:2986
---average::298
reading:301
total:2995
---average::299
reading:300
total:3003
---average::300
reading:298
total:3008
---average::300
reading:295
total:3009
---average::300
reading:293
total:3003
---average::300
reading:290
total:2992
---average::299
reading:287
total:2975
---average::297
reading:285
total:2956
---average::295
reading:283
total:2935
---average::293
reading:282
total:2914
---average::291
reading:282
total:2895
---average::289
reading:283
total:2878
---average::287
reading:284
total:2864
---average::286
reading:286
total:2855
---average::285
reading:288
total:2850
---average::285
reading:290
total:2850
---average::285
reading:292
total:2855
---average::285
reading:294
total:2864
---average::286
reading:296
total:2877
---average::287
reading:296
total:2891
---average::289
reading:299
total:2908
---average::290
reading:297
total:2922
---average::292
reading:300
total:2938
---average::293
reading:303
total:2955
---average::295
reading:304
total:2971
---average::297
reading:294
total:2975
---average::297
reading:309