"if" and "else" statements ignored

My project includes the following hardware:
Uno
.96" 4-pin oled monitor
Hall effect water flow sensor

Sketch compiles and loads OK.

All program code operates as intended except:

“if” and “else” conditional statements (in the loop) are ignored, regardless of whether the value for the variable, ‘duration’ is o or a finite integer.

The program ALWAYS returns the initial value of 10.0 for the variable, ‘pulsefreq’.

Complete sketch code follows:

/*
  H2O_Flo_LED

  Measures the flow rate of water flowing through a 3/4-inch PVC pipe using a DIGITEN Hall effect sensor and displaying results on a 0.96-inch Diymall 4-pin Oled display.

  The sensor output is in the form of the duration in milliseconds of a square wave pulse and the relationship to the water flowrate is:

  F = 4.8 * Q  ( in liters per minute)

*/



#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const int sensdatapin = 2;

void setup()
{



  pinMode(sensdatapin, INPUT);

  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  //initialize with the I2C addr 0x3C (128x64)

  // init done


  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);



  display.clearDisplay();
  // clear the display before start

  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Hello, world!");
  display.display();
  delay(2000);
  display.clearDisplay();


}

void loop()
{



  const unsigned long duration = pulseIn(sensdatapin, HIGH);
  //Pulse duration in microseconds

  const float dursec = duration / 1000000.0;
  //Pulse duration in seconds


  const float pulsefreq = 10.0;



  if ( duration  != 0 )
  {
    const float pulsefreq = 1.0 / dursec;
  }
  else
  {
    const float pulsefreq = 0.0;
  }

  //Pulse frequency in Hz


  const float flolit = (pulsefreq  / 4.8);
  const float flolitr = 60.0 * flolit;
  //Flow rate in Liters/hour


  display.setCursor(22, 20);
  //x,y coordinates
  display.setTextSize(1);
  //size of the text
  display.setTextColor(WHITE);
  display.println(duration);
  display.display();
  delay(1000);
  display.clearDisplay();

  display.setCursor(22, 20);
  //x,y coordinates
  display.setTextSize(1);
  //size of the text
  display.setTextColor(WHITE);
  display.println(dursec);
  display.display();
  display.setCursor(85, 20);
  display.println("seconds");
  display.display();
  delay(1000);
  display.clearDisplay();

  display.setCursor(22, 20);
  //x,y coordinates
  display.setTextSize(1);
  //size of the text
  display.setTextColor(WHITE);
  display.println(pulsefreq);
  display.display();
  display.setCursor(85, 20);
  display.println("Hertz");
  display.display();
  delay(1000);
  display.clearDisplay();


  const float flogal = flolitr * .26417;
  //Flow rate in Gallons/hour
  const float flocuft = flolitr * .035315;
  //Flow rate in cu ft/hour


  display.setCursor(22, 20);
  //x,y coordinates
  display.setTextSize(1);
  //size of the text
  display.setTextColor(WHITE);
  display.println(flogal);
  display.display();
  display.setCursor(85, 20);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println("Gal/hr");
  //print "Gal/hr" in oled
  display.display();
  delay(1000);
  display.clearDisplay();
}

That is because you created a new pulsefreq variable.

Stop using all the consts! they should only be used for things that are known at compile time.

There should be only one

float pulsefreq;

in the program. Everywhere else, just refer to it as (for example):

pulsefreq = 0.0;

Like Keith said...

Const is for things that do not change during the program. They are not variables and will get stored in flash and not in ram.

Also you are dividing two integers and saving to a float. If duration is less than 1000000 the result will
be zero.......

const float dursec = duration / 1000000.0;

It's also important to notice that the variable is defined with Statement Block scope. As soon as the if-else block is left, pulsefreq disappears from the symbol table and no longer exists. Take a look at Nick Gammon's Useful Links post at the top of the Forum, and read about Scope in the C++ Programming section.

You should also never compare a float with another number using '==' or '!='. Floats are not stored accurately; e.g 1.0 might actually be stored as 0.9999 or 1.0001 or so.

noweare:
Also you are dividing two integers and saving to a float. If duration is less than 1000000 the result will
be zero.......

const float dursec = duration / 1000000.0;

1000000.0 is not an integer but a float.

Got rid of “const’s” and cleaned up declarations. Now everything works fine. Thanks for the help.