I'm new to arduino and I dont know why this error is coming up. Any help would be greatly appreciated. The above error is in the set up function. I am not too experienced with functions yet as I have only began working on them a couple of weeks ago.
#include <Adafruit_BMP280.h> // For the purple BMP280 sensor board
#include <SoftwareSerial.h>
// Purple BMP280 Sensor Board
Adafruit_BMP280 bmp; //use I2C interface
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
float temp;
float pressure;
float calculate_altitude(float temp, float pressure);
float altitude;
float altitude1;
float altitude2;
float altitude3;
float x;
float y;
float z;
// Initialising variables
void setup() {
//Setup for purple BMP280
Serial.begin(300);
Serial.println(F("BMP280 Sensor event test"));
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1) delay(10);
}
//Default settings from datasheet
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, //Opertaing Mode
Adafruit_BMP280::SAMPLING_X2, //Temp. oversampling
Adafruit_BMP280::SAMPLING_X16, //Pressure oversampling
Adafruit_BMP280::FILTER_X16, //Filtering
Adafruit_BMP280::STANDBY_MS_500); //Standby time
bmp_temp->printSensorDetails();
//Calculation for altitude using temperature and pressure values
float calculate_altitude(float temp, float pressure);
{
float altitude;
altitude1 = 1013.25/(pressure);
altitude2 = pow(altitude1,0.190222560);
altitude3 = altitude2-1;
altitude = (altitude3*(temp 273.15))/0.0065;
return altitude;
}
}
void loop() {
//Obtaining temperature and pressure events off BMP280
sensors_event_t temp_event, pres_event;
bmp_temp->getEvent(&temp_event);
bmp_pressure->getEvent(&pres_event);
//Taking temperature and pressure values from the events
temp = temp_event.temperature;
pressure = pres_event.pressure;
altitude = calculate_altitude(temp, pressure);
//Reading acceleration values in X, Y and Z axis directions from accelerometer
x = analogRead(A1);
y = analogRead(A2);
z = analogRead(A3);
Serial.print(" temperature = ");
Serial.print(temp); //Print temperature value
Serial.print(",");
Serial.print(" pressure = ");
Serial.print(pressure); //Print pressure value
Serial.print(",");
Serial.print(" altitude = ");
Serial.print(altitude); //Read altitude value
Serial.print(",");
Serial.print(" x axis acceleration = ");
Serial.print(x); //Read acceleration in X direction
Serial.print(",");
Serial.print(" y axis acceleration = ");
Serial.print(y); //Read acceleration in Y direction
Serial.print(",");
Serial.print(" z axis acceleration = ");
Serial.println(z); //Read acceleration in Z direction
delay(100);
}
I am also getting this error in the loop function: undefined reference to `calculate_altitude(float, float)'. If this could be explained aswell I dont understand what it means and what to do to solve it.
CodePudding user response:
According to the C Standard (6.8.6.4 The return statement)
1 A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.
Your function setup
declared with the return type void has a return statement with an expression.
void setup() {
//...
//Calculation for altitude using temperature and pressure values
float calculate_altitude(float temp, float pressure);
{
float altitude;
altitude1 = 1013.25/(pressure);
altitude2 = pow(altitude1,0.190222560);
altitude3 = altitude2-1;
altitude = (altitude3*(temp 273.15))/0.0065;
return altitude;
}
}
It seems it is a result of a typo. It seems you are trying to define the function calculate_altitude
and by mistake placed it within the function setup
. But due to semicolons
float calculate_altitude(float temp, float pressure);
^^^
the compiler considers it as a block scope function declaration and as a result the return statement belongs to the function setup
instead of to the function calculate_altitude
.
The reason of such a typo is a bad code formatting.
CodePudding user response:
With proper indenting and the corrections I described in my comment above, this is how your code should look:
#include <Adafruit_BMP280.h> // For the purple BMP280 sensor board
#include <SoftwareSerial.h>
// Purple BMP280 Sensor Board
Adafruit_BMP280 bmp; //use I2C interface
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
float temp;
float pressure;
float calculate_altitude(float temp, float pressure);
float altitude;
float altitude1;
float altitude2;
float altitude3;
float x;
float y;
float z;
// Initialising variables
void setup()
{
//Setup for purple BMP280
Serial.begin(300);
Serial.println(F("BMP280 Sensor event test"));
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1) delay(10);
}
} // this close-brace was missing
//Default settings from datasheet
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, //Opertaing Mode
Adafruit_BMP280::SAMPLING_X2, //Temp. oversampling
Adafruit_BMP280::SAMPLING_X16, //Pressure oversampling
Adafruit_BMP280::FILTER_X16, //Filtering
Adafruit_BMP280::STANDBY_MS_500); //Standby time
bmp_temp->printSensorDetails();
//Calculation for altitude using temperature and pressure values
float calculate_altitude(float temp, float pressure) //; - Extra semicolon
{
float altitude;
altitude1 = 1013.25/(pressure);
altitude2 = pow(altitude1,0.190222560);
altitude3 = altitude2-1;
altitude = (altitude3*(temp 273.15))/0.0065;
return altitude;
}
// } Extra close-brace here
void loop() {
//Obtaining temperature and pressure events off BMP280
sensors_event_t temp_event, pres_event;
bmp_temp->getEvent(&temp_event);
bmp_pressure->getEvent(&pres_event);
//Taking temperature and pressure values from the events
temp = temp_event.temperature;
pressure = pres_event.pressure;
altitude = calculate_altitude(temp, pressure);
//Reading acceleration values in X, Y and Z axis directions from accelerometer
x = analogRead(A1);
y = analogRead(A2);
z = analogRead(A3);
Serial.print(" temperature = ");
Serial.print(temp); //Print temperature value
Serial.print(",");
Serial.print(" pressure = ");
Serial.print(pressure); //Print pressure value
Serial.print(",");
Serial.print(" altitude = ");
Serial.print(altitude); //Read altitude value
Serial.print(",");
Serial.print(" x axis acceleration = ");
Serial.print(x); //Read acceleration in X direction
Serial.print(",");
Serial.print(" y axis acceleration = ");
Serial.print(y); //Read acceleration in Y direction
Serial.print(",");
Serial.print(" z axis acceleration = ");
Serial.println(z); //Read acceleration in Z direction
delay(100);
}
(Note that I do not have an Arduino so was unable to test compile this code for you.) But I corrected the obvious errors.