Remote access to 4-20ma signals

Thread Starter

Wellarmedlamb

Joined Apr 16, 2016
16
everything is working now except for adding my flow reading to itself each loop to then divide by the number of total reads.

I'm trying to add a sample of my smoothedflow every time around as this variable has already been averaged over 5 seconds. It is just remaining at the value of smoothedflow without adding anything to it. The number of total reads is now increasing as it should.
I've tried the method shown and tried smoothedflow++ and a couple other things I've seen online but I'm stuck.

Code:
  // Smoothed Flow Rate
  float sumvala = 0; //create sumval variable
  for (int i=0; i <= 49; i++){ //repeat this code 50 times
  sumvala = flow + sumvala; //add read value to sum
  delay(100); //0.1 second delay
  }
  float smoothedflow = 0; //create smoothedflow variable
  smoothedflow = sumvala/50; //divide sum by 50
  Serial.print("Smoothed flow is : "); //print out results
  Serial.print(smoothedflow);
  Serial.println("gpm");

  float sumvalb = 0;
  sumvalb = smoothedflow + sumvalb;
  totalreads++;
  float avgflow = 0;
  avgflow = sumvalb/totalreads;
  Serial.print("Average flow is : "); //print out results
  Serial.print(avgflow);
  Serial.println("gpm");

  Serial.print("sumvalb is : ");
  Serial.print(sumvalb);
  Serial.println("gal"); 

  Serial.print("Total Reads : ");
  Serial.print(totalreads);
  Serial.println("Reads");
 

Thread Starter

Wellarmedlamb

Joined Apr 16, 2016
16
After working on this on and off all weekend I seem to have gotten everything working.

Can someone tell me how to get the totalFlow to display without any decimal points? To how many digits is an int viable? I tried unsigned long and it gave me a crazy number.

Code:
// Include this library for using current loop functions.

#include <currentLoop.h >

#define CHANNEL CHANNEL1

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

//Addr: 0x3F, 20 chars & 4 lines
LiquidCrystal_I2C lcd(0x3F,20,4);

int totalReads = 0;

float totalFlow = 0;

float smoothedFlow = 0;

float smoothedTotal = 0;

unsigned long time;

void setup()
{ 
  // Switch ON the 24V DC-DC converter
  sensorBoard.OFF();

  // Inits the Serial for viewing data in the serial monitor
  Serial.begin(9600);
  Serial.print("4-20mA Board Switched On : ");

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0); 
}
void loop()
{   
  // 1. Get the sensor value as a current in mA.
  float current = sensorBoard.readCurrent(CHANNEL);
  Serial.print("Current Read from Channel 1 : ");
  Serial.print(current);
  Serial.println("mA"); 

  // 2. Get the sensor value corrected for flow in gpm.
  // Sensor constant 4mA = 0 gpm and 20mA = 400 gpm
  float flow = (current - 4) * 25;
  Serial.print("Flow : ");
  Serial.print(flow);
  Serial.println("gpm");
 
  // 3. Get the flow rate smoothed over 5 seconds in gpm.
  float sumval = 0; //create sumval variable
  for (int i=0; i <= 49; i++){ //repeat this code 50 times
  sumval = flow + sumval; //add read value to sum
  delay(100); //0.1 second delay
  }
  smoothedFlow = sumval/50; //divide sum by 50

  //print out results
  Serial.print("Smoothed flow is : "); //print out results
  Serial.print(smoothedFlow);
  Serial.println("gpm");
 
  // 4. Get the running average in gpm.
  totalReads++; 
  smoothedTotal = smoothedFlow + smoothedTotal;
  float avgFlow = 0;
  avgFlow = smoothedTotal / totalReads;

  //print out results
  Serial.print("Average flow is : "); //print out results
  Serial.print(avgFlow);
  Serial.println("gpm");

  //print out time
  Serial.print("Time: ");
  time = millis();
  Serial.println(time);
 
  // 5. Get total flow in gal.  
  totalFlow = ((avgFlow / 60000) * time);

  //print out results
  Serial.print("Total flow is : ");
  Serial.print(totalFlow);
  Serial.println("gal"); 

  //Display smoothed gpm on first row
  lcd.setCursor(0, 0);
  lcd.print("Flow:           gpm ");
  lcd.setCursor(6, 0);
  lcd.print(smoothedFlow);

  //Display running average on second row
  lcd.setCursor(0, 1); 
  lcd.print("Avg:            gpm "); 
  lcd.setCursor(6, 1);
  lcd.print(avgFlow);

  //Display running total on third row
  lcd.setCursor(0, 2); 
  lcd.print("Tot:            gal "); 
  lcd.setCursor(6, 2);
  lcd.print(totalFlow);
 
  lcd.setCursor(19, 3);
  Serial.println("***************************************");
  Serial.print("\n");
}
 

Thread Starter

Wellarmedlamb

Joined Apr 16, 2016
16
My current sketch is working pretty well. The "Smoothed Flow" is almost spot on and the "Total Average" is pretty good. The problem I'm running into now is that I switched my "Time" variable to a "long" because "unsigned long" was returning odd readings. But now "long" only lasts somewhere between 9 and 24 hours and then my total becomes a negative number on the screen. I need this to be stable for multiple days at a time.

The sketch I post previously is pretty close to what I'm currently using except "unsigned long time;" is now "long time;" as neither one worked.

Any suggestions would be great as once I get this working it'll be time to figure out how best to connect to at&t's 3g network.
 
Top