Multiple button inputs with state detection (MQTT Openhab project)

Hello,

I'm relatively new to Arduino and (almost) everything I manage to accomplish w.r.t. my project is done through research and reverse engineering.

Right now I'm trying to create a home automatisation project using an Arduino, MQTT (Mosquitto), and Openhab. I'm proud to say that my project is about to be finished, but I have one last problem that I can't manage to solve on my own.

The code to my project is attached because it's too long.

The problem I'm having is in this part of the code:

//Drukknop flank detectie voor Living Drukknop 1
//
//r1 = reading 2
//s1 = state 2
//p1 = previous 2
//t1 = time 2
//d1 = debounce 2

  r1 = digitalRead(P_LIV_D1); 

 // if the input just went from LOW and HIGH and we've waited long enough
 // to ignore any noise on the circuit, toggle the output pin and remember
 // the time
 if (r1 == HIGH && p1 == LOW && millis() - t1 > d1) {
   if (s1 == HIGH){
     s1 = LOW;
        client.publish(MQTT_TOPIC_LIV_L1, "LIV_L1_ON");
}
   else
   {
     s1 = HIGH;
     client.publish(MQTT_TOPIC_LIV_L1, "LIV_L1_OFF");
   }
   t1 = millis(); 
 }
 p1 = r1; 
   



//Drukknop flank detectie voor Slaapkamer 1 Drukknop 1
//
//r2 = reading 2
//s2 = state 2
//p2 = previous 2
//t2 = time 2
//d2 = debounce 2

  r2 = digitalRead(P_SK1_D1);

 // if the input just went from LOW and HIGH and we've waited long enough
 // to ignore any noise on the circuit, toggle the output pin and remember
 // the time
 if (r2 == HIGH && p2 == LOW && millis() - t2 > d2) {
   if (s2 == HIGH){
     s2 = LOW;
        client.publish(MQTT_TOPIC_SK1_L1, "SK1_L1_ON");
}
   else
   {
     s2 = HIGH;
     client.publish(MQTT_TOPIC_SK1_L1, "SK1_L1_OFF");
   }
   t2 = millis () ; 
 }
 p2 = r2;

It works fine using only 1 push button where it detects which state it is in, i.e. press once for HIGH state and this will publish the ON command in MQTT and press again for LOW state and publish OFF command in MQTT -> repeat cycle.

But when I add a second button as input weird things start to happen:
Every press publishes the last state it was in to MQTT without switching states and often even starts to flood the terminal with said state.

How do I manage to get it to work with more than 1 push button?

Note: Unless you have an alternative, it has to stay in this kind of format so I can publish ON or OFF to MQTT upon state change.

test.ino (7.4 KB)

Your inconsistent placement of curly braces does nothing to make your code readable. Pick ONE style and stick with it.

How ARE the switches wired? Are you CERTAIN that you do not have a floating pin condition?

PaulS:
Your inconsistent placement of curly braces does nothing to make your code readable. Pick ONE style and stick with it.

How ARE the switches wired? Are you CERTAIN that you do not have a floating pin condition?

The switches are currently only 1 switch connected to pin 2 (pin 3 will be added later and is currently empty) and it's wired like shown here: https://www.arduino.cc/en/Tutorial/Button

I do apologize for the messiness of the code.

The switches are currently only 1 switch connected to pin 2 (pin 3 will be added later and is currently empty) and it's wired like shown here: https://www.arduino.cc/en/Tutorial/Button

It is not a good idea to read from pins that having nothing connected to them. You get unpredictable results, as you are seeing.

Add hardware. Write code to support that hardware. Not the other way around.

PaulS:
It is not a good idea to read from pins that having nothing connected to them. You get unpredictable results, as you are seeing.

Add hardware. Write code to support that hardware. Not the other way around.

That solved the flooding bit at least :slight_smile:

But I'm still not getting a state change upon pressing the button, just the same stated being published over again upon each press.

Edit: Also, they are no longer publishing both states at the same time when pressing 1 button.

But I'm still not getting a state change upon pressing the button, just the same stated being published over again upon each press.

Do you now have two switches wired?

Add Serial.print() statements in the two outer if bodies. Are the statements showing up in the Serial Monitor app when, and only when, the switches BECOME pressed?

PaulS:
Do you now have two switches wired?

Add Serial.print() statements in the two outer if bodies. Are the statements showing up in the Serial Monitor app when, and only when, the switches BECOME pressed?

When I press nothing, nothing happens.
When I press button 1 (LIV_D1) 3 times I get 3x the same:

LIV_D1_LOWMessage arrived: topic: openhab/UNO1/LIV_L1
Length: 10
Payload: LIV_L1_OFF

When I press button 2 (SK1_D1) 3 times I also get 3x the same

SK1_D1_HIGHMessage arrived: topic: openhab/UNO1/SK1_L1
Length: 9
Payload: SK1_L1_ON

This tells me no state change is happening.

One thing to note: I have the 2 10k Ohm resistors connected to the same GND.

This tells me no state change is happening.

If no state change was happening, you would not be getting any messages, so that tells me that the state changes ARE happening.

The switch state changes, anyway. I do not understand what s1 and s2 are states of.

After the switch state change detection/message publishing code, you have a bunch of other stuff that diddles with s1 and s2 but does not publish messages. What is that about?