#define speakerPin1 5
#define speakerPin2 6
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

  /*
Voltage Input	Expected A to D result
200	    4.710	    3858
100	    3.594	    2944
50	    3.032	    2484
0	      2.471	    2024
-50	    1.909	    1564
-100	  1.347	    1103
-200	  0.223	    183

  */

  int voltageSamples = 10;
  int minPeak1 = 1103;  //-100
  int maxPeak1 = 2944 ;  //+100

  int minPeak2 = 1103;  //-100
  int maxPeak2 =  2944 ;  //+100

  float factor = 0.0; //no damping
  float acc = 0;
  float calibrationFactor = 0.1;
  
  /*
  for (int sampleCount = 0; sampleCount < voltageSamples; ++sampleCount) {
  int rawValue1 = analogRead(speakerPin1);
  if (rawValue1 > maxPeak1) maxPeak1 = rawValue1;
  if (rawValue1 < minPeak1) minPeak1 = rawValue1;
  rawValue2 = analogRead(speakerPin2);
  if (rawValue2 > maxPeak2) maxPeak2 = rawValue2;
  if (rawValue2 < minPeak2) minPeak2 = rawValue2;
  }
  */

  int rawValue1 = maxPeak1 - minPeak1; // final peak to peak value
  int rawValue2 = maxPeak2 - minPeak2;
  int Vrms = rawValue1 + rawValue2;
  acc = acc * factor + Vrms * (1.0 - factor);
  float voltage = acc * calibrationFactor;
  Serial.print("Voltage: ");
  Serial.print(voltage, 1);
  Serial.println(" V");
}

void loop() {

}