Morning ScruffR,
I had the pressure and voltage going to the LCD and the cloud the way I wanted them, then had a Samsung SSD die on me and not realize that the .ino was on that drive.
I have the working code uploaded on a boron that I accidentally killed the power supply on. (Lesson learned of taking pin readings off the top of the board)
On the Gas pressure I want whole numbers on, the accuracy of #100 PSIG is good enough for this application, it is not time critical either. I am using analogsmoother to average the inputs. I am measuring the voltage across a resistor from a 4-20mA pressure transmitter.
Below is the section of code that I am trying to iron out. I initially thought that running that averaging the 10 readings, then running through map would be the best way to do this. I did notice on the LCD last night that when I power up with the battery the reading is nearly 3000 PSI then drops steadily ( pressure is at 0 PSIG ) It is like the Analogsmoother buffer is starting with it at 3000 and as lower values come in the output falls. I may need to add some lines in there to wait to send to the processed value to the LCD, Log, and the cloud until the buffer has been filled. I think I might be getting the info sent to the cloud before the buffer has been filled and wind up with a weird number. The code for the voltage is very similar to this, it is winding up high also.
The gas pressure is saying 2700 PSIG when it is about 2200, and the voltage is showing 15 volts when its about 12.5.
// GAS PRESSURE 1.89 @2200
AnalogSmoother gas_pressure(A1, 10);
int transmitter_lcd_power = D2;
int LCD_PIN_STATE = 0;
int gas_pressurePin = A1;
int gas_pressureValue = 0;
int gas_pressureScaled = 0;
int last_gas_pressureScaled = 0;
void setup() // SETUP ############################################
{
// PINS AND PIN MODES
Serial.begin(115200);
gas_pressure.fill();
}
// GAS PRESSURE TROUBLE and PRESSURE VALUES
gas_pressureValue = gas_pressure.read();
gas_pressureScaled = map(gas_pressureValue,750,2325,10,3000);
if(gas_pressureScaled < 5) {
Log.info("Gas P AI:=%d", gas_pressureValue);
lcd->setCursor(0,3);
lcd->print("GAS:");
lcd->print("TRBL ");
}
else
{
Log.info("GAS_pressure_Value:=%d",gas_pressureValue);
lcd->setCursor(0,3);
lcd->print("GAS:");
lcd->print(gas_pressureScaled);
lcd->print("PSI");
delay(1000);
}
String SgasP = String::format("%.f", gas_pressureScaled);
if(gas_pressureScaled < 1) {
Log.info("GAS PRESSURE BAD");
SgasP = ("BAD");
}
else {
Log.info("GAS PRESSURE SCALED:%d", gas_pressureScaled);
String SgasP = String::format("%.f", gas_pressureScaled);
}
Below is Analog Smoother.cpp
#include "AnalogSmoother.h"
AnalogSmoother::AnalogSmoother(int pin, unsigned int size) {
mI = 0;
mPin = pin;
mSize = size;
mTotal = 0;
mReadings = new int[size];
for (unsigned int i=0; i<mSize; i++) {
mReadings[i] = 0;
}
}
AnalogSmoother::~AnalogSmoother() {
delete mReadings;
}
void AnalogSmoother::fill() {
for (unsigned int i=0; i<mSize; i++) {
read();
}
}
float AnalogSmoother::read() {
mTotal -= mReadings[mI]; // Subtract old reading
mReadings[mI] = analogRead(mPin); // Read new value
mTotal += mReadings[mI]; // Add new reading
mI++; // Increment readings index
if (mI >= mSize) {
mI = 0;
}
return (float)mTotal / (float)mSize;
}