Sending data from setup to loop

Hi guys!

I think im in the right thread. I am relatively new to arduino and programming in general although i have had a class in c++.

I am trying to write a code that sets the altitude of a bmp2805 sensor to zero and then will give the altitude based on how high the sensor is.

Here is my code:
#include <Wire.h> //adding libraries that are needed
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp; //Initialized BMP280 Sensor

void setup() {

Serial.begin(9600); //Checking the BMP280 Sensor to see if it is connect and operational

Serial.println(F("*****************"));
Serial.println(F("Checking BMP280 Sensor..."));

if (!bmp.begin()) { //Sensor cannot befound or not operating, check the wiring
Serial.println(F("SENSOR NOT FOUND"));
while(1);
}

if (bmp.begin()) { //Sensor is found and sending signal
Serial.println(F("SENSOR FOUND"));
Serial.println(F("*****************"));
Serial.println();
}

}

void loop() {

float ground;
float locAlt;

for(ground=0; ground=0 ; ) { //setting ground to the initial Altitude Above Sea-Level
ground = bmp.readAltitude(1025.0);
}

locAlt=bmp.readAltitude(1025.0)-ground;

Serial.print(F("Temperature= ")); //displays temp in C
Serial.print(bmp.readTemperature());
Serial.println(F("C"));

Serial.print(F("Pressure= ")); //displays pressure in Pa
Serial.print(bmp.readPressure());
Serial.println(F("Pa"));

Serial.print(F("Approx. Altitude ASL= ")); //displays Altitude ASL in m
Serial.print(bmp.readAltitude(1025.0));
Serial.println(F("m"));

Serial.print(F("Approx. local Altitude= ")); //displays the altitude of the sensor to the ground and not sea-level
Serial.print(bmp.readAltitude(1025.0)-ground);
Serial.println("m");

Serial.println();
delay(1000);

}

it complies but in the serial monitor it does not the correct altitude. Im not sure what im am doing wrong. I would like for the program to take the initial altitude (ASL) and set it to the ground variable and then every time it takes a measurement it will subtract ground from it. this in theory should give me the altitude of the sensor from the ground and not sea level. I am thinking that i am running into problems in my for loop.

I was thinking if there is a way to only take the ground measurement once from setup and then set it to ground and send that data to the void loop.

Use global variables -> declare them before setup() and you can use them everywhere.

I agree with Reply #1

...R

for(ground=0; ground=0 ; ) {

First time i met with this.
Is it an arduino code?

GrOnThOs:

for(ground=0; ground=0 ; ) {

First time i met with this.
Is it an arduino code?

It's C.

GrOnThOs:

for(ground=0; ground=0 ; ) {

First time i met with this.
Is it an arduino code?

Yeah, I think GrOnThOs has spotted a problem. This compiles, but I don't think it does what you think it does.

The second expression, ground=0, is a conditional. However, you have put in it an assignment statement. Furthermore, the assignment evaluates to 0, which is the same as false. The code in the brackets will never be executed.

Maybe you meant to use the equality operator, ==, in which case it would theoretically run until you got a non-zero result from the sensor. Is this correct? Do you really expect to get a 0 result from the sensor sometimes?

Jimmus:
Yeah, I think GrOnThOs has spotted a problem. This compiles, but I don't think it does what you think it does.

It's actually hard to imagine what someone would think it does.

Robin2:
I agree with Reply #1

...R

I agree with reply#2's concurrence.

Am I just doubly redundant or is it exponential?

BulldogLowell:
Am I just doubly redundant or is it exponential?

Probably.

...R

aarg:
It's actually hard to imagine what someone would think it does.

Well, there is that.

But since the code is essentially useless, it's even harder for me to imagine someone putting it in there fully knowing what it actually does. So whatever they think it does is probably not what it does. Unless, of course, it is.

Sounds right to me. :grin: