Hi
How to join orange line on serial plotter as red line drowned by hand?
Average is causing too much delay.
const int sensorIn = A1;
double Voltage = 0;
const int numReadings = 50;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A1;
void setup()
{
Serial.begin(115200);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop()
{
Voltage = getVPP();
total = total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
readings[readIndex] = Voltage;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
//Serial.print(" v = "); Serial.print(analog_value*5);
Serial.print(" Amps RMS = "); Serial.print( Voltage);
// Serial.print(" average = "); Serial.print(average);
Serial.println();
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here, 4095.0
//int minValue = 4095.0;
uint32_t start_time = millis();
while ((millis() - start_time) < 10)
//while((millis()-start_time) < 100) //sample for 1 Sec
{
readValue = analogRead(A1);
if (readValue > maxValue)
{
maxValue = readValue;
}
if (readValue < minValue)
{
minValue = readValue;
}
}
result = (maxValue - minValue);
return result;
}
why are maxValue and minValue local to the function? If you make them global you might get a smooth update of peak-peak voltage without smoothing. What are you trying to measure?
OK, 1. have you biased the input voltage?
2. I think you're Algorithm in vpp gives a value close to zero when you have a value close to the top of the adc scale? Is this the intended behaviour? Could be wrong but I would like you do clarify.
can use leaky integration
avg += (samp - avg) * K K < 1
with a fast attack and very slow decay to capture close to the peak
K = 2 samp > avg
K = 100 samp > avg
you can keep track of the lastValue and if set readValue to lastValue if readValue == 0
curious what you're measuring? looks like you're reading values as quickly as possibly for 10 msec. such things are typically done with a periodic sampling rate