Compiling error when incorporating hardware supplied demo code

I have an ever evolving/growing project that monitors water parameters. Recently I’ve added a flow meter to aid in optimizing the flow across the sensors. The flow meter’s supplied code (obviously) compiles and works as expected. However, when I’ve tried to incorporate it into my projects code I’m getting the “___ not declared in this scope”. I understand this is a pretty straight forward problem to solve but I’m stuck since I’m failing to understand how I’ve copied/pasted the demo into my code is cause the error. Thank you for guidance!

(Side note: I have an RTC in hand and will incorporate that next to eliminate that inelegant run timer as well as used for time stamps on data written to the SD card.)

The supplied code:

volatile double waterFlow;
void setup() {
  Serial.begin(9600);  //baudrate
  waterFlow = 0;
  attachInterrupt(0, pulse, RISING);  //DIGITAL Pin 2: Interrupt 0
}
void loop() {
  Serial.print("waterFlow:");
  Serial.print(waterFlow);
  Serial.println("   L");
  delay(500);
}

void pulse()   //measure the quantity of square wave
{
  waterFlow += 1.0 / 450.0;
}

My attempt at incorporating that demo code:

#include <SPI.h>
#include <SD.h>
#include <Time.h>
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

#define FLOW_INTERVAL             1000ul   // 1sec
#define TEMP_INTERVAL             3600ul   // 6min      
#define OD_INTERVAL               3600ul   // 6min      
#define PH_INTERVAL               3600ul   // 6min      
#define PH_CO2_ADD_INTERVAL       20000ul     // 20sec  
#define PH_CO2_CHECK_INTERVAL     1200000ul    // 20min

File myFile;

const byte Flow_Meter        = 2;
const byte OD_Probe          = A2;   //
const byte Temp_Probe        = A0;   //
const byte pH_Probe          = A1;   //
const byte CO2_GateValve     = 3;
const byte pH_HIGH_THRESHOLD = 16.00;

unsigned long timeFlow;
unsigned long timeOD;
unsigned long timepH;
unsigned long timeTemp;
unsigned long timeCo2Add;
unsigned long timeCo2Chk;
unsigned long timeNow;
unsigned long int avgValue;   //Store the average value of the sensor feedback
float b;
int buf[10], temp;

volatile double waterFlow;

enum { ST_INIT, ST_ADD, ST_CHK };
int state;
time_t elapsedTime;


// -----------------------------------------------------------------------------

void setup () {
  Serial.begin (9600);

  lcd.setCursor(0, 0);
  lcd.print("Flow: ");
  lcd.setCursor(10, 0);
  lcd.print("RT: ");
  lcd.setCursor(0, 1); // Set the cursor on the 1st column and 2nd row.
  lcd.print("pH: ");   //
  lcd.setCursor(0, 2); // Set the cursor on the 1st column and 3rd row.
  lcd.print("OD: ");   //
  lcd.setCursor(0, 3); // Set the cursor on the 1st column and 4th row.
  lcd.print("Temp: "); //

  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  if (!SD.begin(4)) {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("Initialization done.");
  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to test.txt...");
    // myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }

  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    //myFile.close();
  } else {
    Serial.println("error opening test.txt");
  }

  pinMode (OD_Probe, INPUT);
  pinMode (CO2_GateValve, OUTPUT);
  pinMode (Temp_Probe, INPUT);
  pinMode (pH_Probe, INPUT);
  pinMode (Flow_Meter, INPUT);
  waterFlow = 0;
  attachInterrupt(0, pulse, RISING);  //DIGITAL Pin 2: Interrupt 0

  timeNow  = millis ();
  timeOD   = timeNow;
  timepH   = timeNow;
  timeTemp = timeNow;
}


void loop ()
{
  timeNow  = millis ();

  lcd.setCursor(10, 0);
  lcd.print(timeNow / 3600000);

  // -----------------------------------------------------------------------------

  // * * * * * Flow INTERVAL * * * * *
  if ((timeNow - timeFlow) >= FLOW_INTERVAL)
  {
    timeFlow = timeNow;
    volatile double waterFlow = digitalRead (Flow_Meter);

    lcd.setCursor (14, 0);
    lcd.print(waterFlow);

    waterFlow += 1.0 / 450.0;
  }
  // -----------------------------------------------------------------------------

  // * * * * * Temp INTERVAL * * * * *

  if ((timeNow - timeTemp) >= TEMP_INTERVAL)
  {
    timeTemp = timeNow;
    int temp_Val = analogRead (Temp_Probe);

    lcd.setCursor(6, 3); // Set the cursor on the 7th column, 4th row
    lcd.print(((temp_Val) - 500) / 10);

    myFile.print("RT Min: ");
    unsigned long Now = millis() / 3600000;
    myFile.print(Now);
    myFile.print(". Temp: ");
    myFile.println(((temp_Val) - 500) / 10);
    Serial.print("Temp: ");
    Serial.println(((temp_Val) - 500) / 10);
    //myFile.close();
  }

  // -----------------------------------------------------------------------------

  // * * * * * OD INTERVAL * * * * *

  if ((timeNow - timeOD) >= OD_INTERVAL)
  {
    timeOD = timeNow;
    int od_Val = analogRead (OD_Probe);

    lcd.setCursor(4, 3); // Set the cursor on the 5th column, 3rd row
    lcd.print(od_Val);

    myFile.print("RT Min: ");
    unsigned long Now = millis() / 3600000;
    myFile.print(Now);
    myFile.print(". OD: ");
    myFile.println(od_Val);
    Serial.print("OD: ");
    Serial.println(od_Val);
    //myFile.close();
  }

  // -----------------------------------------------------------------------------

  // * * * * * pH INTERVAL * * * * *

  if ((timeNow - timepH) >= PH_INTERVAL)
  {
    int pH_Val;

    switch (state)  {
      case ST_INIT:

        for (int i = 0; i < 10; i++) //Get 10 sample value from the sensor for smooth the value
        {
          buf[i] = analogRead(pH_Probe);
          delay(10);
        }
        for (int i = 0; i < 9; i++) //sort the analog from small to large
        {
          for (int j = i + 1; j < 10; j++)
          {
            if (buf[i] > buf[j])
            {
              temp = buf[i];
              buf[i] = buf[j];
              buf[j] = temp;
            }
          }
        }
        avgValue = 0;
        for (int i = 2; i < 8; i++)               //take the average value of 6 center sample
          avgValue += buf[i];
        float pH_Val = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into millivolt
        pH_Val = 3.5 * pH_Val;                  //convert the millivolt into pH value

        lcd.setCursor(4, 1); // Set the cursor on the 5th column, 2nd row
        lcd.print(pH_Val);

        myFile.print("RT Min: ");
        unsigned long Now = millis() / 3600000;
        myFile.print(Now);
        myFile.print(". pH: ");
        myFile.println(pH_Val);
        Serial.print("pH: ");
        Serial.println(pH_Val);
        //myFile.close();

        if (pH_Val >= pH_HIGH_THRESHOLD)  {
          state = ST_ADD;
          timeCo2Add = timeNow;
          timeCo2Chk = timeNow;

          digitalWrite (CO2_GateValve, LOW);
        }
        else
          timepH = timeNow;
        break;

      case ST_ADD:
        if ((timeNow - timeCo2Add) >= PH_CO2_ADD_INTERVAL)  {
          state = ST_CHK;
          digitalWrite (CO2_GateValve, HIGH);
        }
        break;


      case ST_CHK:
      default:
        if ((timeNow - timeCo2Chk) >= PH_CO2_CHECK_INTERVAL)  {
          state  = ST_INIT;
          timepH = timeNow;
        }
        break;
    }
  }
}

Post the error message you get.

wildbill:
Post the error message you get.

My apologies, forum 101! "pulse" is what isn't declared in the scope.

Have a look at the guide to creating a program in the tutorials at the top of this section. If you look at your code you will see it is becoming increasingly difficult to read as the stuff in your loop gets more and more complicated. Generally it is easier if you create distinct functions with nice intuitive names and then call the functions in the loop. You are attempting to organise your code by putting in -------------------- etc which is a good start but when you start mixing in new stuff you will often fail to see how it upsets old stuff if it is all part of the one function (loop). It is often useful to create new sketches for new stuff to create the function you require, then you can save it and use it again in any other project and you also don't have the heartsinking feeling of realising you have messed up a code that doesn't have a save file.

AttachInterrupt needs a function to call when the interrupt fires. You told it to use a function called pulse, but you don't have one. To get rid of the error, copy the one from the example code.

attachInterrupt(0, pulse, RISING); //DIGITAL Pin 2: Interrupt 0

Needs this part added after the loop() {
}
section of code. Not inside of it.

void pulse() //measure the quantity of square wave
{
waterFlow += 1.0 / 450.0;
}