Re: QRP SWR meter recommendation? #ubitx

Tom, wb6b
 

Here are some code snippets from a project I did. It calculates SWR from the voltages read from analog pins A6 and A7.

It does a number of other things, so here are the basic pieces of code for SWR extracted out into small snippets.

This first code segments captures selected analog pin voltages and applies an exponential smoothing filter to them.

for (int pin = 0; pin < 8; pin++) {
    if (_analogActive[pin]) {
      float scaledReading = analogScalingFactor * (float)analogRead(pin);
      _analogValue[pin] +=
        (scaledReading - _analogValue[pin]) * _analogFilterValue[pin];
   }
}


This next slice of code captures the max voltage and applies a slow decay to the max value.

  // Capture the peak analog values with a slow decay time.
  // The decay time is a starting point. May want to adjust it to taste.
  float decayValue = 0.001;

  for (int pin = 0; pin < 8; pin++) {
    if (_analogActive[pin]) {
      float analogValue = _analogValue[pin];

      _analogValueMax[pin] = max(_analogValueMax[pin], analogValue);
      _analogValueMax[pin] += (analogValue - _analogValueMax[pin]) * decayValue;

    } else {
      _analogValueMax[pin] = 0.0;
    }
  }


And here the SWR is calculated from the Max values.

    // "SWR"

    float vf = _analogValueMax[6];
    float vr = _analogValueMax[7];
    float vd = (vf - vr);
    vd = max(vd, 0.002);  // Limit max SWR reading before divide by zero
    // Calculate SWR from directional coupler voltages.
    result = String((vf + vr) / vd);


You can see the full code here:
https://github.com/mountaintom/SmartLCDandIOexpander_I2C/blob/master/examples/SmartLCDandIOexpander_I2C_Backpack/SmartLCDandIOexpander_I2C_Backpack.ino

Tom, wb6b

Join BITX20@groups.io to automatically receive all group messages.