Programming Question(bmp280)

I want to do an altitude meter, it’s supposed to take my altitude and compare it to the new altitude. For this I have taken the example that comes in the library of the barometer, but you have to press a button for a few seconds to set the reference altitude, the question is: How can I do so that the arduino press the button automatically and take the altitude reference? Here is the code:

#include "SparkFunBME280.h"
#include "CircularBuffer.h"


BME280 mySensor;
float reference = 0;
int button;

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

Serial.print("Program Started\n");
  Serial.print("Starting BME280... result of .begin(): 0x");
  //Calling .begin() causes the settings to be loaded
  delay(10);//Needs > 2 ms to start up
  Serial.println(mySensor.begin(), HEX);

  Serial.println();
  delay(1000);
  
}

void loop() {
  mySensor.readTempC();
for(int timer, timer<=500,timer++){ //I was thinking of making a counter for the button, but I do not                                                                           
  if (timer<500){                            //know how to leave the for.
    button=0; //The button needs to be pressed to ground for a few seconds to set the reference.
  }else{
    button=1;
  }
}
  
  //Check button.  No debounce, so hold for operation
  if( button == 0 )//This is the condition, The button needs to be pressed to ground for 4     seconds. This should only work once and it is at the start.               
  {
    //Set reference altitude.
    reference = mySensor.readFloatAltitudeMeters();
  }
  
  float lastAlt = mySensor.readFloatAltitudeMeters() - reference;
  myBuffer.pushElement(lastAlt);
  float tempAlt = myBuffer.averageLast(8); //Do an average of the latest samples
  

  
  
  myBuffer.pushElement(lastAlt);
  

  if(tempAlt >= 0)
  {
    Serial.print(" ");
  }
  Serial.print(tempAlt);
  Serial.println(" m:       "); //the altitud 
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

irwin432:
How can I do so that the arduino press the button automatically and take the altitude reference?

I assume you don’t want the Arduino to constantly set the altitude reference. When do you want this to happen, what conditions should trigger taking the altitude reference?

Sorry for that, I corrected the post and now has everything you say, "button" is the condition, if "button" is equal to 0, the code takes the reference, this has to be at startup, and for about 4 seconds.

I'm sorry, but button is a stupid name. There is NO hint, in the name, what the variable is supposed to mean/hold.

Setting the value in button in a for loop makes no sense. At the end of the for loop, the value in button will always be the same. You might as well ditch the for loop, and just set button (whatever the hell that means) to the value that it will have at the end of the for loop.

  if( button == 0 )//This is the condition, The

Kind of pointless to test the value of a variable that hold a KNOWN value.