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)