1

I want to change or declare a constant in the setup() and then, I want to access it in the loop().

I have searched a lot, but the only questions I found could be solved by declaring the constant in the beginning of the program. That doesn't work for me because I am using the Adafruit BMP388 and I can't use the pressure before setup().

I also tried declaring the variable before setup() and changing it inside setup(), but it didn't change when I used it in loop().

How can I solve this?

ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Nov 10, 2020 at 18:28
8
  • how are we to know that you actually changed it in loop()? Commented Nov 10, 2020 at 18:41
  • I didn't change it in loop(), I have to access it there. I want to change it in setup() Commented Nov 10, 2020 at 18:43
  • look at the fade sketch in arduino IDE example sketches Commented Nov 10, 2020 at 18:44
  • how are we to know that you actually changed it in setup()? Commented Nov 10, 2020 at 18:45
  • I want to use it in loop(): I need the startpressure (declared in setup()) and compare it to the current pressure Commented Nov 10, 2020 at 18:46

1 Answer 1

0

You are not going to get good answers without showing your code, but maybe this is a good guess of what you want:

why not try something roughly like this (untested and incomplete)?

Adafruit_BMP3XX bmp;
float startPressure;
float difference;
void setup() {
 bmp.begin_I2C();
 startPressure = bmp.readPressure();
} 
void loop() {
 difference = startPressure - bmp.readPressure();
}

Or, better:

Adafruit_BMP3XX bmp;
void setup() {
 bmp.begin_I2C();
} 
void loop() {
 static const float startPressure = bmp.readPressure();
 static float difference;
 difference = startPressure - bmp.readPressure();
}
answered Nov 10, 2020 at 19:45
5
  • 1
    I'm not entirely sure this is the direction he's going in. But if he is, you could move float startPressure; into the loop(), perform startPressure = bmp.readPressure(); as part of its initialization, and make it static and const. As in: static const float startPressure = bmp.readPressure(); This would behave as what you have but would limit startPressure to the only scope it's used in. Commented Nov 10, 2020 at 19:54
  • @timemage: True, and better, but it would require an explanation of "static", and I don't feel up to it 8-). I will add it though. Commented Nov 10, 2020 at 20:07
  • 1
    difference does not need to be static. Commented Nov 10, 2020 at 20:21
  • Yeah, and it technically isn't "constant" in C++ terms. But it seems to be in the spirit of the question so far as I understand the question. Commented Nov 10, 2020 at 20:23
  • @Edgar-Bonet: No, doesn't have to be in the example, but who knows if he wants it to remain the same over several loops in his code. Commented Nov 10, 2020 at 20:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.