Trouble with averaging giving correct value

Hey guys, So I am expanding on Electronoobs Inductance tester (How to make a inductance meter using arduino)

And I am trying to avg/smooth the values for “inductance” and my values that are spit out are about a little less than half the original value. Ive tried different methods of smoothing and avging the value.

Anyone know whats going on here? I assume the program is handling the value differently? Attached is the code.

#include <SoftwareSerial.h>

//LCD config
#include <Wire.h>

//13 is the input to the circuit (connects to 150ohm resistor), 11 is the comparator/op-amp output.
double pulse, frequency, capacitance, inductance;
const int numReadings = 10;
double readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
double average = 0; 
int inputPin = A0;

SoftwareSerial LCD(9,10);

void setup(){
  LCD.begin(9600);
  Serial.begin(115200);
  pinMode(11, INPUT);
  pinMode(13, OUTPUT);
  Serial.println("Why hello!");
  delay(200);
  for (int thisReading = 0; thisReading < numReadings; thisReading ++){
    readings[thisReading] = 0;
  }
}
void loop(){
  digitalWrite(13, HIGH);
  delay(10);//give some time to charge inductor.
  digitalWrite(13,LOW);
  delayMicroseconds(100); //make sure resination is measured
  pulse = pulseIn(11,HIGH,5000);//returns 0 if timeout
  if(pulse > 0.1){ //if a timeout did not occur and it took a reading:
    
//  #error insert your used capacitance value here. Currently using 2uF. Delete this line after that
  capacitance = 1.E-6; // - insert value here
  frequency = 1.E6/(2*pulse);
  inductance = 1./(capacitance*frequency*frequency*4.*3.14159*3.14159);//one of my profs told me just do squares like this
  inductance *= 1E6; //note that this is the same as saying inductance = inductance*1E6
  }

  total = total - readings[readIndex]; //Sub the last reading
  readings[readIndex] = inductance; //add readings to total  
  total +=  readings[readIndex]; //Advance to next pos in array
  readIndex = readIndex + 1;

if (readIndex >= numReadings) {
  readIndex = 0; //reset array if full
}
   
  average = total / numReadings; // calc the avg

  Serial.print("High for uS:");
  Serial.print( pulse );
  Serial.print("\tfrequency Hz:");
  Serial.print( frequency );
  Serial.print("\tinductance uH:");
  Serial.println( inductance );
  Serial.println( average );

  delay(10);   

  // clear display by sending spaces
  LCD.write("                "); 
  LCD.write("                ");

 // move cursor to beginning of first line
  LCD.write(254); 
  LCD.write(128);
  LCD.print("mH ");

  // move cursor to beginning of second line
  LCD.write(254);
  LCD.write(192);

  LCD.print(inductance); 
        
  delay(500);
  }

That code doesn't even compile, so any results it doesn't produce can't be valid.

What happened to setup()? There’s a lot of code not in any function that I bet goes in setup. Did we lose something in the copy-paste?

I do notice that you are taking your readings in double and then adding them all up and trying to store the total in an int. Are you sure you want to truncate each reading like that? Are you sure the total will fit in an int? Should total maybe be a double too?

Delta_G:
What happened to setup()? There's a lot of code not in any function that I bet goes in setup. Did we lose something in the copy-paste?

I do notice that you are taking your readings in double and then adding them all up and trying to store the total in an int. Are you sure you want to truncate each reading like that? Are you sure the total will fit in an int? Should total maybe be a double too?

Yeah I removed some test comment code and mustve got overzealous. I put void setup back in and now it complies.

The double has been tested as an int as well and I did not get the expected result again. I do believe that there could be some issue with the way the values are declared but I havent been able to confirm. I believe I have also made total a double and it didnt give the expect result again. I can re-test that thought though.

Please don't modify your post (save for minor spelling mistakes). You make changes to your code, post anew. Modifications will take comments out of context and destroy the audit trail making it extremely difficult for those who might choose to help to pick up on the history.

A lot of your code is not in the right block, you will re-use “inductance” for each loop even if this is wrong. Try to replace the following block:

if(pulse > 0.1){ //if a timeout did not occur and it took a reading:
   
//  #error insert your used capacitance value here. Currently using 2uF. Delete this line after that
  capacitance = 1.E-6; // - insert value here
  frequency = 1.E6/(2*pulse);
  inductance = 1./(capacitance*frequency*frequency*4.*3.14159*3.14159);//one of my profs told me just do squares like this
  inductance *= 1E6; //note that this is the same as saying inductance = inductance*1E6
  }

with this:

if(pulse <= 0.1) return; //Leave if under threshold
   
//  #error insert your used capacitance value here. Currently using 2uF. Delete this line after that
  capacitance = 1.E-6; // - insert value here
  frequency = 1.E6/(2*pulse);
  inductance = 1./(capacitance*frequency*frequency*4.*3.14159*3.14159);//one of my profs told me just do squares like this
  inductance *= 1E6; //note that this is the same as saying inductance = inductance*1E6
  //Curly bracket "}" removed

Danois90:

if(pulse <= 0.1) return; //Leave if under threshold

Return to where? Leave what? You’re in loop().

DKWatson:
Return to where? Leave what? You're in loop().

Yes and the entire code after reading "pulse" depends on "pulse" being above 0.1. The return statement would basically cause the pulse to be re-read until it is above the wanted threshold and then it will be processed by the averaging code.

You're in loop().

loop() is a function, and it is perfectly valid to return from it to main().

The return will cause the loop to restart.

DKWatson:
The return will cause the loop to restart.

Which is exactely what is wanted. You should be able to see the purpose of this by looking at the code.

The return will cause the loop to restart.

That is exactly what happens, even if an explicit "return" statement is not present in loop(), and is the intent of the designers.

Just as an update guys, making my values be in line with eachother (as in Double) seems to have fixed the issue. Thank you everyone.

What do you mean "in line with eachother" ?

AWOL:
What do you mean "in line with eachother" ?

Maybe he means that "int total" should have been "double total" for this to work properly - which makes good sense.

Danois90:
Maybe he means that "int total" should have been "double total" for this to work properly - which makes good sense.

Yes. inline as in have all your ducks in a row. :wink:

originally thought I tested that and it didnt make a difference but I can only assume I tested making everything else a double other than the value that needed it.