Qwiic button issues

Hello, new here. I’m trying to activate a relay with a button press, and I can only get it to activate WHILE the button is pressed. I’ve tried button.isPressed and button.hasBeenClicked, I’ve read all the documentation there is on the qwiic button, and I’m still at a loss. I saw that the registers must be cleared by the user- but that tells me nothing about how to do that…

Any ideas? Code below:

#include <Wire.h>

#include <SparkFunTMP102.h>

#include <SparkFun_Qwiic_Relay.h>

#include <SerLCD.h>

#include <SparkFun_Qwiic_Button.h>

#define DISPLAY_ADDR 0x72

#define RELAY_ADDR 0x18

#define BUTTON_ADDR 0x5B

unsigned long previousMillis = 0;

const unsigned long tempReadInterval = 1000;

const unsigned long updateInterval = 500;

float temperature;

bool alertState;

bool manualFan;

Qwiic_Relay fan(RELAY_ADDR);

QwiicButton button;

uint8_t brightness = 100;

SerLCD lcd;

TMP102 tSense;

int fanSet = 86;

byte degF[8] = {0b11000, 0b11000, 0b00000, 0b00111, 0b00100, 0b00111, 0b00100, 0b00100};

byte fan1[8] = {0b00000, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b00000, 0b00000};

byte fan2[8] = {0b00000, 0b00100, 0b00100, 0b11111, 0b00100, 0b00100, 0b00000, 0b00000};

void setup() {

Wire.begin();

Wire.setClock(400000);

lcd.begin(Wire);

lcd.setBacklight(0,0,0);

button.begin(BUTTON_ADDR);

lcd.createChar(0, degF);

lcd.createChar(1, fan1);

lcd.createChar(2, fan2);

bool systemReady = false;

if (!tSense.begin()) {

systemReady = false;

lcd.print(“Cannot connect to TMP102.”);

while (1);

} else

systemReady = true;

lcd.print(“Connected to TMP102”);

delay(1000);

Wire.beginTransmission(DISPLAY_ADDR);

Wire.write(‘|’);

Wire.write(‘-’);

Wire.endTransmission();

if (!fan.begin()) {

systemReady = false;

lcd.print(“Cannot connect to Fan.”);

while (1);

} else

systemReady = true;

lcd.print(“Connected to Fan”);

delay(1000);

Wire.beginTransmission(DISPLAY_ADDR);

Wire.write(‘|’);

Wire.write(‘-’);

Wire.endTransmission();

fan.turnRelayOff();

button.LEDoff();

if (systemReady == true) {

lcd.print(“SystemReady”);

lcd.setBacklight(0, 255, 0);

delay(1000);

Wire.beginTransmission(DISPLAY_ADDR);

Wire.write(‘|’);

Wire.write(‘-’);

Wire.endTransmission();

}

tSense.setFault(2);// 0-3: 0:1 fault, 1:2 faults, 2:4 faults, 3:6 faults.

tSense.setAlertPolarity(1);// set the polarity of the Alarm. (0:Active LOW, 1:Active HIGH).

tSense.setAlertMode(1);// set the sensor in Comparator Mode (0) or Thermostat Mode (1).

tSense.setConversionRate(1);// set the Conversion Rate (how quickly the sensor gets a new reading)0-3: 0:0.25Hz, 1:1Hz, 2:4Hz, 3:8Hz

tSense.setExtendedMode(1);//0:12-bit Temperature(-55C to +128C) 1:13-bit Temperature(-55C to +150C)

tSense.setHighTempF(90.01);//set T_HIGH, the upper limit to trigger the alert on

tSense.setLowTempF(87.0);//set T_LOW, the lower limit to shut turn off the alert

}

void loop()

{

unsigned long currentMillis = millis();

bool isFanOn = fan.getState();

const float turnOnThreshold = fanSet + 1.0; // Adjust the hysteresis value as needed

const float turnOffThreshold = fanSet;

if (currentMillis - previousMillis >= tempReadInterval) {

previousMillis = currentMillis;

tSense.wakeup();

temperature = tSense.readTempF();

alertState = tSense.alert();

alertMode(alertState);

tSense.sleep();

}

if (!manualFan) {

if (!isFanOn && temperature > turnOnThreshold) {

startFan();

}

if (isFanOn && temperature < turnOffThreshold) {

stopFan();

}

}

if (currentMillis - previousMillis >= updateInterval){

//sendTemp(temperature);

updateDisplay(temperature, isFanOn);

}

if (button.hasBeenClicked() ) {

startFan();

manualFan = true;

}

}

void startFan() {

fan.turnRelayOn();

}

void stopFan() {

fan.turnRelayOff();

}

void alertMode(bool alertState) {

if (alertState == true) {

startFan();

lcd.setBacklight(0, 0, 0);

delay(200);

lcd.setBacklight(255, 0, 0);

delay(500);

}else{

lcd.setBacklight(0,255,0);

//stopFan();

}

}

void updateDisplay(float value, bool isFanOn) {

Wire.write(‘|’);

Wire.write(‘-’);

lcd.setCursor(0, 0);

lcd.print("Temp: ");

lcd.print(value, 1);

lcd.writeChar(0);

lcd.print(" ");

lcd.setCursor(0, 1);

if (isFanOn) {

lcd.print("Fan is ON ");

}

if (!isFanOn) {

lcd.print("Fan is OFF ");

}

}

Line 170 here https://github.com/sparkfun/SparkFun_Qw … Button.cpp has the function to clear the registers

Thanks for that- still unsure how to use that, or I’m formatting the function call incorrectly

EDIT:

I have the following, which seems to work but sometimes doesn’t respond to button presses…

if (button.hasBeenClicked() && !isFanOn) {

startFan();

manualFan = true;

button.clearEventBits();

}

if (button.hasBeenClicked() && isFanOn && manualFan == true){

stopFan();

manualFan = false;

button.clearEventBits();

}

To activate the relay with a button press, you can use the button.isPressed() function instead of button.hasBeenClicked(). This function checks if the button is currently being pressed. You can modify your code by replacing button.hasBeenClicked() with button.isPressed() in the if (button.hasBeenClicked()) statement. This should allow the relay to activate while the button is pressed.

Thank you- I was using ‘button.isPressed()’ but wasn’t getting latching behavior, so it led me to button.hasBeenClicked()'. After wanting the button to light up it led me back to ‘isPressed’, and is latching fine. It may have been elsewhere in the code that was making me chase my tail. Currently working as follows:

if (button.isPressed() && !isFanOn && !manualFan) {

while (button.isPressed()){

button.LEDon(100);

}

button.LEDoff();

startFan();

manualFan = true;

}

if (button.isPressed() && isFanOn && manualFan) {

while (button.isPressed()) {

button.LEDon(100);

}

button.LEDoff();

stopFan();

manualFan = false;

}