Code Conundrum: Merged loops not working

Dear wise Arduino overlords,

I come for your wisdom on the following code. I'm in the process of merging two setups and two loops. One setup and loop is for a debounce (setupDebounce and loopDebounce) and another is for a pressure sensor reading (setupPressure and loopPressure). Separately, the two work fine and perform flawlessly. However for some reason unbeknownst to me, when I merge the two sketches together, the debounce loop does not work properly. In particular, the debounce loop results in a continuous HIGH to Pin13 despite pushing the button (it should switch to my other LED HIGH on pin9). The pressure loop works as it should displaying the proper data. Any insight or code modifications would be much appreciated!

In return for your assistance, I shall slay and tribute not one, but two forum trolls in your honor.

Thanks for your help gents!

Also props to the debounce code author. I leveraged his debounce code here https://www.youtube.com/watch?v=4NcfEwm9WBI

Best,
Dan

#include <SFE_BMP180.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#define ALTITUDE 1655.0  

SFE_BMP180 pressure;                            
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int Button = 10;
int Debounce = 0;
int led = 13;
int led2 = 9;
int Pthresh = 1030;
boolean ButtonPressed = false;
boolean Toggle = false;
const int relayPin = 8;

void setup(){
  setupPressure();
  setupDebounce();
  
 }
 
 void loop(){
   loopPressure();
   loopDebounce(); 
 }
 
void setupPressure()
{
  Serial.begin(9600);
  Serial.println("REBOOT");

  pinMode(relayPin, OUTPUT);              
  
  lcd.begin(16, 2);                        
  lcd.print("PROJECT - ALPHA");            
  
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
}

void setupDebounce() {
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(Button, INPUT);
  digitalWrite(led, HIGH);
  digitalWrite(led2, LOW);

}

void loopDebounce() {
  if(digitalRead(Button)==HIGH)
    {
      Debounce++;
    } else
      {
        Debounce = 0;
        ButtonPressed = false;
        Toggle = false;
      }
  if (Debounce >= 5000)
  {
   ButtonPressed = true; 
  }
  if(ButtonPressed == true && Toggle == false)
    {
      Toggle = true;
        digitalWrite(led, !digitalRead(led));
        digitalWrite(led2, !digitalRead(led2));
    }

}

void loopPressure()
{
   
  char status;
  double T,P;

  status = pressure.startTemperature();
  if (status != 0)
  {

    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)
        {
          lcd.setCursor (0, 1);
          lcd.print("Pab_m:");
          lcd.setCursor (6, 1);
          lcd.print(P*0.0295333727,2);
          lcd.println(" inHg");
         
          delay(5000); 
          
          lcd.setCursor (0, 0);
          lcd.print("P_thr:");
          lcd.print(Pthresh*0.0295333727,2);
          lcd.println(" inHg");
          
        }
      }
     }
  }
  delay(1000);  // Pause for 5 seconds.
}

sketch_aug22d.ino (2.23 KB)

NOT an Arduino overlord in any sense but you got zero replies so I'll take a shot.

The pressure code takes WAY too long to run for the debounce to work.
With such pressure code you don't really need debounce and forget adding anything responsive.

How long do the pressure. commands take? Do they block until a value is returned?

    status = pressure.getTemperature(T);

Open up the example BlinkWithoutDelay in your copy of Arduino. Look at how it works. Try to use that same technique in loopPressure() to take and display a reading once per 5 seconds. You should be able to eliminate all use of the delay() function and then the debounce will work.

Consider making the debounce threshold a #define or const int so that you can easily change it and use it in other parts of your program as needed.

GoForSmoke

Here is the script from the.h file. Not sure if that answers your question.

MorganS - I'll give it a try and see what happens.

/* char begin();
 // call pressure.begin() to initialize BMP180 before use
 // returns 1 if success, 0 if failure (bad component or I2C bus shorted?)
 
 char startTemperature(void);
 // command BMP180 to start a temperature measurement
 // returns (number of ms to wait) for success, 0 for fail

 char getTemperature(double &T);
 // return temperature measurement from previous startTemperature command
 // places returned value in T variable (deg C)
 // returns 1 for success, 0 for fail

 char startPressure(char oversampling);
 // command BMP180 to start a pressure measurement
 // oversampling: 0 - 3 for oversampling value
 // returns (number of ms to wait) for success, 0 for fail

 char getPressure(double &P, double &T);
 // return absolute pressure measurement from previous startPressure command
 // note: requires previous temperature measurement in variable T
 // places returned value in P variable (mbar)
 // returns 1 for success, 0 for fail