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