Uno (large code): "getPressure" not declared in scope, but ... it is?

Any help would be massively appreciated! Below is my code. I just incorporated a barometric pressure sensor, BMP180, and wanted to include this code from one of the downloadable examples. I put everything in its proper place.

But getPressure isn't declared in that scope for some reason... and the error is in the void setup. "getPressure" isn't a void function (which I still haven't learned what that is, but I use them in my code) and it's actually a double function.

My understanding is Arduino Uno hates/doesn't accept doubles, but the code by itself works great and the sensor works fine. Does combining all these doubles with my voids mess things up?

Things I tried: changing all "doubles" to float. Moving declaration from above setup into the setup. Moving the "double getPressure() { ... } from below the loop to above the setup.... this worked.

Except now my other void functions weren't recognized. If I moved those up above setup, the code just kept breaking.

What do you think? Thank you!

I removed one massive sensor to lighten up the code a bit, the problem still persists, and I didn't want to lose all the void functions down below.


//Serial Comment Out
#define DEBUG 1  //switch to 1 to print to serial
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif

//Libraries
#include <SPI.h>
#include <SdFat.h>
#include <OneWire.h>            //DS18 temp
#include <DallasTemperature.h>  //DS18 temp
#include <SFE_BMP180.h>         //BMP180 pressure

//Time
uint32_t timeStamp = 0;
int loopDelay = 3000;

//Document Info
String header0;
String header1;  //update below
String header2;

//LED
const int ledPower = 3;
const int ledWrite = 2;
const int ledLight = 9; //must be PWM
// PIN 10 for SD card ONLY

//SD Card
SdFat SD; //SD instance (needs SdFat.h library)
File myFile; //name of file object as .txt
const int cS = 10; //SD card chip select, change to 8 if ya want DO NOT USE 10 for anything else
int fileNameNumber = 0; //incremented when a measurement is stopped
String SDFileName = "bmptest" + (String)fileNameNumber;  //1st file is Log0...

//Photoresistor
const int lightPin = 0; //Analog 0, voltage divided wire
int lightLevel, high = 0, low = 1023; //light=no r, dark=high r, will be constrained to 0–255
float voltBytes = 4.01176471; //lightlevel (0–255) * this = 0–1023 resolution
float milliVolt = 0.00488759; //5v÷1023 resolution

//Temp DS18
#define ONE_WIRE_BUS 4          // Data wire is plugged into port 4 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);  // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature. 
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

//BMP180 Definitions
SFE_BMP180 pressure;
double baseline;        // baseline pressure

void setup() {
  Serial.begin(115200);
//LED
  pinMode(ledPower, OUTPUT);  //Exterior Arduino On Power Up
  digitalWrite(ledPower, HIGH); //Arduino On
  pinMode(ledWrite, OUTPUT);     //Exterior loop light
  pinMode(10, OUTPUT); //Required for SD card module, do not use
  
  //SD CARD
  debug(F("Initializing SD card..."));
  if (!SD.begin(cS)) {  // see if the card is present and can be initialized:
    debugln(F("Card not present"));
    while (1)  // don't do anything more:
      ;
  }
  debugln(F("card initialized."));
  while (SD.exists(SDFileName)) {
    fileNameNumber++;
    SDFileName = "bmptest" + (String)fileNameNumber;
  }
//Header
  header0 = (F("Setup End"));
//Print initial startup
  myFile = SD.open(SDFileName, FILE_WRITE);
  if (myFile) {
//Temp DS18 Setup
  sensors.begin();                            // Start up the library
  numberOfDevices = sensors.getDeviceCount(); // Grab a count of devices on the wire
  myFile.print(F("# of DS18 Temp Sensors Found: "));
  myFile.println(numberOfDevices, DEC);
  debug(F("Found: "));
  Serial.println(numberOfDevices, DEC); //leave as-is, buggy line with serial.print
  
  //Headers
    myFile.println(header0);
    debugln(header0);
  header1 = ("Payload Data for " + SDFileName);  
  header2 = (F("Time,°C1,°F1,°C2,°F2,relaltm,relaltft,[update:Altm(1013.25mb),Altft(1013.25mb),rAltm(1022.6mb),rAltft(1022.6mb)],LightLevel,Bytes,Volts"));                      //update as needed
    myFile.println(header1);
    debugln(header1);
  if (pressure.begin())
    debugln("BMP180 init success");
  else{
    debugln(F("BMP180 init fail (disconnected?)\n\n"));
  }  
  baseline = getPressure();             // Get the baseline pressure:
  myFile.print(F("Baseline pressure (mb): "));
  myFile.print(baseline);
  
  debug(F("Baseline pressure (mb): "));
  debug(baseline);
}
    myFile.println(header2);
    debugln(header2);
//Close SD
    myFile.close();
//LED  
  ledWriteFlash();
            }
} //END setup

void loop() {
  timeStamp = millis(); // this declaration must be in void loop
 
 //PHOTORESISTOR
  lightLevel = analogRead(lightPin);
  autoTune();  // have the Arduino do the work for us!
  analogWrite(ledLight, lightLevel); //nightlight: add 255 - before lightlevel

 // SD CARD WRITE
  myFile = SD.open(SDFileName, FILE_WRITE);
  if (myFile) {
//Time
    myFile.print(timeStamp);
    debug(timeStamp);
    printComma();
//DS18 Temp – should be inside SD write
 sensors.requestTemperatures(); // Send the command to get temperatures
  for(int i=0;i<numberOfDevices; i++) { // Loop through each device, print out temperature data
    if(sensors.getAddress(tempDeviceAddress, i)){ // Search the wire for address
    float tempC = sensors.getTempC(tempDeviceAddress);
    myFile.print(tempC);                //Celsius for first device
    debug(tempC);
    printComma();

    myFile.print(tempC*(9.0/5.0)+32);   //Fahrenheit, then loops back for next device
    debug(tempC*(9.0/5.0)+32);
    printComma();
   } 	
  }
//BMP Temp & Pressure
  double a,P;
  P = getPressure();    // Get a new pressure reading:
  a = pressure.altitude(P,baseline);    // Show the relative altitude difference between the new reading and the baseline reading:
    //for header2: relaltm,relaltft
        //relative altitude (in m and ft):
  if (a >= 0.0) myFile.print(" "); // add a space for positive numbers
  myFile.print(a,1);
  if (a >= 0.0) debug(" "); // add a space for positive numbers
  Serial.print(a,1); //leave as-is
  printComma();
  
  if (a >= 0.0) myFile.print(" "); // add a space for positive numbers  
  myFile.print(a*3.28084,0);
  if (a >= 0.0) debug(" "); // add a space for positive numbers
  Serial.print(a*3.28084,0);
  printComma();
  
//Photoresistor
  char bytesStr[7]; //99999.9 and null
  float Bytes = lightLevel*voltBytes;
  dtostrf(Bytes,1,2,bytesStr); //convert to text

  char voltsStr[5];
  float Voltage = lightLevel*voltBytes*milliVolt;
  dtostrf(Voltage,1,2,voltsStr);

  char photo1[50];
  sprintf(photo1,"%d,%s,%s",lightLevel,bytesStr,voltsStr); //photoresistor data string

  myFile.print(photo1);  // send to card
  debug(photo1);    //send to screen
  printComma();

//New Line
    myFile.println();
    debugln();

  myFile.close();
        } else {
    debugln("error opening bmptest");
  }  //END SD card write

//LED flash
  ledWriteFlash();

delay(loopDelay); 
} //END loop

double getPressure() { 
  char status;
  double T,P,p0,a;
  status = pressure.startTemperature();
  if (status != 0) {    // Wait for the measurement to complete:
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0) {
      status = pressure.startPressure(3);
      if (status != 0)   {        // Wait for the measurement to complete:
        delay(status);
       status = pressure.getPressure(P,T);
        if (status != 0)  {
          return(P);
        }  else debugln("error retrieving pressure measurement\n");
      }   else debugln("error starting pressure measurement\n");
    }    else debugln("error retrieving temperature measurement\n");
  }     else debugln("error starting temperature measurement\n");
}

void ledWriteFlash(){
  digitalWrite(ledWrite, HIGH);
  delay(50);
  digitalWrite(ledWrite, LOW);
  delay(100);
  digitalWrite(ledWrite, HIGH);
  delay(50);
  digitalWrite(ledWrite, LOW);
  delay(50);
}

void printComma() //from arduino help
  {
    myFile.print(F(",")); 
    debug(F(","));
  }

  void manualTune(){  //necessary, though not used
  lightLevel = map(lightLevel, 0, 1023, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
} 

void autoTune(){
  if (lightLevel < low)  {
    low = lightLevel;
  }
  if (lightLevel > high)  {
    high = lightLevel;
  }
  lightLevel = map(lightLevel, low+30, high-30, 0, 255); //can always mess with +numbers
  lightLevel = constrain(lightLevel, 0, 255);
} //END autotune


Probably a good idea to show us the exact error message

Change the name of the function, it's conflicting with SFE_BMP180.h..

good luck.. ~q

Absolutely. I thought of taking a screenshot but let's see... brb with it

/Users/xxx/Desktop/bmp180_online_help/bmp180_online_help.ino: In function 'void setup()':
/Users/xxx/Desktop/bmp180_online_help/bmp180_online_help.ino:103:14: error: 'getPressure' was not declared in this scope
   baseline = getPressure();             // Get the baseline pressure:
              ^~~~~~~~~~~
/Users/xxx/Desktop/bmp180_online_help/bmp180_online_help.ino:103:14: note: suggested alternative: 'pressure'
   baseline = getPressure();             // Get the baseline pressure:
              ^~~~~~~~~~~
              pressure
/Users/xxx/Desktop/bmp180_online_help/bmp180_online_help.ino:115:3: error: 'ledWriteFlash' was not declared in this scope
   ledWriteFlash();
   ^~~~~~~~~~~~~
/Users/xxx/Desktop/bmp180_online_help/bmp180_online_help.ino:115:3: note: suggested alternative: 'ledWrite'
   ledWriteFlash();
   ^~~~~~~~~~~~~
   ledWrite
/Users/xxx/Desktop/bmp180_online_help/bmp180_online_help.ino: At global scope:
/Users/xxx/Desktop/bmp180_online_help/bmp180_online_help.ino:117:1: error: expected declaration before '}' token
 } //END setup
 ^

exit status 1

Compilation error: 'getPressure' was not declared in this scope

I posted the error message(s) as code below

I tried changing it but as a noob I get easily confused since I thought it was also referencing something deep in the library.

I think you're saying here that this wouldn't work? (It didn't.)

Well, it compiles on Wokwi, but didn't have the SFE lib, so I commented it out and the associated objects and it compiled..
Looking inside the SFE lib i do see a public method of a class with the same name, while I don't think this should be an issue, I'm thinking it is??

simmed here..

~q

For reference, here is the original code (minus the paragraphs of comments at the top):


#include <SFE_BMP180.h>
#include <Wire.h>

// You will need to create an SFE_BMP180 object, here called "pressure":

SFE_BMP180 pressure;

double baseline; // baseline pressure

void setup()
{
  Serial.begin(115200);
  Serial.println("REBOOT");

  // Initialize the sensor (it is important to get calibration values stored on the device).

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while(1); // Pause forever.
  }

  // Get the baseline pressure:
  
  baseline = getPressure();
  
  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" mb");  
}

void loop()
{
  double a,P;
  
  // Get a new pressure reading:

  P = getPressure();

  // Show the relative altitude difference between
  // the new reading and the baseline reading:

  a = pressure.altitude(P,baseline);
  
  Serial.print("relative altitude: ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
  Serial.print(a,1);
  Serial.print(" meters, ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
  Serial.print(a*3.28084,0);
  Serial.println(" feet");
  
  delay(500);
}


double getPressure()
{
  char status;
  double T,P,p0,a;

  // You must first get a temperature measurement to perform a pressure reading.
  
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:

    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Use '&T' to provide the address of T to the function.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Use '&P' to provide the address of P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          return(P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}




Wow so it should work!!! That's very exciting.

I wonder if doing a last minute update in Arduino messed stuff up. There was an update when I first opened it up this afternoon.

Saw that too, was thinking might of been me commenting things out..
maybe just needs a forward declaration..
~q

YOU DID IT!!!! omg thank you all!!!!

Thank you for your help qubits, sincerely appreciate it!!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.