I am trying to create a circuit with one tactile pushbutton, and LCD and two sensors: 1) temperature, and 2) pulse sensor. When the circuit is on, the LCD initially shows the reading from the temp sensor, when the button is pressed the LCD shows the reading from the pulse sensor.
The code compiles, the LCD shows both of the temperature function and pulse reading work.
I attached a picture of the schematic of the circuit.
The problem is that when the button is pressed, it takes many pushing of the button for the circuit to go from temperature reading to pulse reading. I thought maybe debouncing the button was unnecessary so I tried to write a code without the code, but it made no difference. My coding skills are still in the beginner side but I am trying my best for my coding to make sense. I just need some guidance and advice on what the problem could be and how I can tackle the problem. My code is attached here:
#include <LiquidCrystal.h>
#define BUTTON 2
const int rs = 12, en = 11, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
//TEMPERATURE
const int tempPin = A0; // LM35 in A0
int T1=60;
//PULSE
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
int PulseWire = A1; // Pulse Sensor purple wire connected to analog pin A0
int blinkPin = 13; // pin to blink led at each beat
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore.
// Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
// Otherwise leave the default "550" value.
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
//=========BUTTON DEBOUNCE============
boolean lastButton = LOW; // last button state
boolean currentButton = LOW; // current button state
int switchMode = 1; //cycle between function
//====================================
void setup()
{
lcd.begin(16, 2);
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
//pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
}//end if
}//end void setup()
//Debouncing Function
boolean debounce(boolean last) {
boolean current = digitalRead(BUTTON);
if (last != current)
{
delay(T1); //wait #ms
current = digitalRead(BUTTON); //read it again
}//end if
return current; //return the current value
}//end boolean debounce
void setMode(int mode) {
//TEMP ON
if (mode == 1)
{
Serial.begin(9600);
int value = analogRead(tempPin); // read the value from the sensor
lcd.setCursor(0, 1);
float millivolts = (value / 1024.0) * 5000;
float celsius = millivolts / 10;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp = "); lcd.print((celsius * 9) / 5 + 32); lcd.print((char) 223); //turning the celsius into fahrehait
lcd.print("F");
lcd.setCursor(7, 2);
lcd.print(celsius); lcd.print((char) 223);
lcd.print("C");
delay(1600);
lcd.clear();
delay(T1);
}//end if TEMP ON
//RED ON
else if (mode == 2) {
{
Serial.begin(9600);
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
lcd.setCursor(0, 0);
lcd.print("HeartBeat Found!");
lcd.setCursor(4, 1);
lcd.print("BPM: "); lcd.print(myBPM);
delay(1600);
lcd.clear();
delay(T1);
}//end if
}//end setMode void function
}
}
void loop()
{
currentButton = debounce(lastButton); //read debounced state
if (lastButton == LOW && currentButton == HIGH) /// if it was pressed
{
switchMode++;
}
//if cycled through the different options,
//reset the counter to 0
if (switchMode == 3) switchMode = 1;
setMode(switchMode);
}//end loop
I also considered doing a switch case statement, but so far it hasn't worked either. Please let me know some ideas!
Thank you!!