Pages: [1]   Go Down
Author Topic: Floating pointing incrementing, not working  (Read 907 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm currently working on a flow meter, which measures in GPH.  I'm trying to set it up so it displays the current gph usage and two resettable total gallon used counters.  The sketch counts the number of pulses from the sensor every second or so.  I then take the flow measurement( which is pulses * gals per pulse) and add it to totalgallonsA.     
TotalgallonsA += flowgal;   // both are float

The problem I'm having is totalgallonsA instead of increasing always goes back to zero?
I can post the complete code if needed, I have it on my other computer
I'm very new to programming, and don't know where to begin??
Matt
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you print both variables before and after the add please? (Serial.print).

Possibly one or the other is NaN (not a number).

Preferably post all your code inside code tags.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12518
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Floating point doesn't seem like a good bet for that sort of thing - you're going to be getting unpredictable rounding errors that could get quite large. Better IMO to define the resolution you're going to measure at (tenths of gallons, hundredths, thousandths etc) and store your counters as fixed point integer numbers.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17253
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I agree. A flow meter that generating pulses per unit of material flowed is best dealt with using integer math. it's rather straight forward counting of pulses in a fixed time interval and of course updating the total flow value, I see no advantages to using floating point math and several disadvantages.

Lefty
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13469
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

probably you need to define the total counter as a global variable.

to be sure please post your code.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Edison Member
*
Karma: 32
Posts: 1282
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This should work:
Code:
TotalgallonsA = TotalgallonsA + flowgal;   // both are float


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
TotalgallonsA = TotalgallonsA + flowgal;
And how is that different to the code the IP posted?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 49
Posts: 2191
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can't tell unless you post your whole code.

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my code so far.  Im pretty new to the language, any help would be great
Matt

Code:

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,10,9,8,7);




byte sensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte sensorPin       = 2;

// The hall-effect flow sensor outputs aproximatly one pulse per .00038L

volatile byte pulseCount; 

float flowRate;
unsigned int flowGallons;
float totalGallonsA;
unsigned int totalGallonsB;
int pulseCounttot;
unsigned long oldTime;

void setup()
{
 Serial.begin(9600);
 lcd.begin(16, 2);
 lcd.setCursor(0, 0);
 lcd.print("BRADSHAW ");
 lcd.setCursor(0, 1);
 lcd.print("INDUSTRIES ");

delay(3000);
 lcd.clear();





 pinMode(sensorPin, INPUT);
 digitalWrite(sensorPin, HIGH);

 pulseCount        = 0;
 flowRate          = 0.0;
 flowGallons       = 0;
 totalGallonsA     = 0;
 totalGallonsB     = 0;
 oldTime           = 0;
 pulseCounttot     = 0;
 // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
 // Configured to trigger on a FALLING state change (transition from HIGH
 // state to LOW state)
 attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

/**
* Main program loop
*/
void loop()
{




 if((millis() - oldTime) >= 1000)    // Only process counters once per second
 {
   // Disable the interrupt while calculating flow rate and sending the value to
   // the host
   detachInterrupt(sensorInterrupt);


   // Because this loop may not complete in exactly 1 second intervals we calculate
   // the number of milliseconds that have passed since the last execution and use
   // that to scale the output.
 
       unsigned long time = millis() - oldTime;
      float time_in_sec = time / 1000;
      float impuls = pulseCount * time_in_sec; //scale pulse number


     float flowG = impuls * .00010;  // amount of gals
      float GPH = flowG * 120 ;  // convert to gph


      totalGallonsA =+ flowG ;


   oldTime = millis();
   {

   }
   lcd.setCursor(0,0);
   lcd.print(GPH);
   lcd.setCursor(6,0);
   lcd.print("GPH");   // Print the integer part of the variable
   lcd.setCursor(0,1);
   lcd.print(totalGallonsA,4);



   // Reset the pulse counter so we can start incrementing again
   pulseCount = 0;

   // Enable the interrupt again now that we've finished sending output
   attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
 }
}

/**
* Invoked by interrupt0 once per rotation of the hall-effect sensor. Interrupt
* handlers should be kept as small as possible so they return quickly.
*/
void pulseCounter()
{
 // Increment the pulse counter
 pulseCount++;
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
    totalGallonsA =+ flowG ;

That isn't the same as:

Code:
    totalGallonsA += flowG ;

Your version merely sets totalGallonsA to the value in flowG. It doesn't add.



Quote
Code:
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);

Better is:

Code:
noInterrupts();



Quote
Code:
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

Better is:

Code:
interrupts ();
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
noInterrupts();
Why is it better to disable all of the interrupts instead of just the one?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2100
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Disabling global interrupts is a single bit manipulation, attach/detach interrupt are not as efficient.
The operation could be done before detachInterrupt even returns.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I agree, but if you wanted finer tuning you could disable that particular interrupt, eg.

Code:
EIMSK &= ~_BV (INT0);

That is also a single-bit operation.

And turn it on later with:

Code:
EIMSK |= _BV (INT0);

In this case I don't think having interrupts off for a moment will be a big deal.
Logged

Pages: [1]   Go Up
Jump to: