Verifying Bluetooth Connection On Button Click

Hi!

I'm experiencing an issue with my code that involves using Bluetooth client blocks to verify the connection when certain buttons are clicked. It doesn’t seem to be working, and I'm having trouble identifying the problem. As a beginner, I would also appreciate any suggestions to improve the code and make it simpler and more efficient.

Thanks in advance!

Arduino Code:

#include "SoftwareSerial.h"

//Keyword Denfinition:
#define Rel1 9
#define Rel2 8
#define LED1 6
#define LED2 5
#define LED3 11
#define LED4 10


SoftwareSerial hc05(2, 3);

//Variables:
int chanVar = 0;
bool DEBUG = 0;
int h05;
int interval = 54;
int ledEffect = 0;
int relEffect = 0;
int brightness = 105;

//                           s e t u p ( )
//**********************************************************************
void setup() {
  Serial.begin(9600);
  hc05.begin(9600);

  pinMode(Rel1, OUTPUT);
  pinMode(Rel2, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
} //                    E N D  O F  s e t u p ( )




//                            l o o p ( )
//**********************************************************************
void loop() {
    h05 = hc05.read();

  if(Serial.available()) {
    if (DEBUG) {
      Serial.print("interval: ");
      Serial.println(interval);
    }
  }
  
  effectDet();
  ledEffectApply();
  relEffectApply();
} //                   E N D  O F  l o o p ( )



//Functions:
void ledChanInc() {
  if(chanVar == 0)
  {
    digitalWrite(LED1, 0);
    analogWrite(LED2, brightness);
    digitalWrite(LED3, 0);
    digitalWrite(LED4, 0);
  }
  else if(chanVar == 1)
  {
    digitalWrite(LED2, 0);
    analogWrite(LED3, brightness);
  }
  else if(chanVar == 2)
  {
    digitalWrite(LED3, 0);
    analogWrite(LED4, brightness);
  }
}

void ledChanDec() {
  if(chanVar == 3)
  {
    digitalWrite(LED4, 0);
    analogWrite(LED3, brightness);
  }
  else if(chanVar == 2)
  {
    digitalWrite(LED3, 0);
    analogWrite(LED2, brightness);
  }
  else if(chanVar == 1)
  {
    digitalWrite(LED2, 0);
    analogWrite(LED1, brightness);
  }
}

void effectDet() {
  switch(h05) {

    case 0:
      if(DEBUG)
      {
        Serial.println(h05);
      }

      else 
      {
        ledEffect = 0;
      }
    break;

    case 1:
      if(DEBUG) {
        Serial.println(h05);
      }

      else
      {
          ledEffect = 1;
      }
    break;

    case 2:
      if(DEBUG)
      {
        Serial.println(h05);
      }

      else
      {
        ledEffect = 2;
      }
    break;

    case 3:
      if(DEBUG)
      {
        Serial.println(h05);
      }

      else
      {
        ledEffect = 3;
      }
    break;

    case 4:
      relEffect = 0;
    break;
    
    case 5:
      relEffect = 1;
    break;

    case 6:
      relEffect = 2;
    break;

    case 7:
      relEffect = 3;
    break;

    case 8 ... 108:
      setSpeed();
    break;

    case 109 ... 209:
      setBrightness();
    break;
  }
}

void setSpeed()
{
  int speed = h05;
  speed = map(speed, 8, 108, 1000, 0);
  interval = speed;

  if(DEBUG)
  {
    Serial.println(speed);
  }
  return interval;
}

int setBrightness()
{
  brightness = h05;
  brightness = map(brightness, 109, 209, 0, 255);

  if(DEBUG)
  {
    Serial.println(brightness);
  }
  return brightness;
}

void ledEffectApply()
{
  if(ledEffect == 0)
  {
    digitalWrite(LED1, 0);
    digitalWrite(LED2, 0);
    digitalWrite(LED3, 0);
    digitalWrite(LED4, 0);
  }

  else if(ledEffect == 1)
  {
    analogWrite(LED1, brightness);
    analogWrite(LED2, brightness);
    analogWrite(LED3, brightness);
    analogWrite(LED4, brightness);
  }

  else if(ledEffect == 2)
  {
    analogWrite(LED1, brightness);
    analogWrite(LED2, brightness);
    analogWrite(LED3, brightness);
    analogWrite(LED4, brightness);
    delay(interval);
    digitalWrite(LED1, 0);
    digitalWrite(LED2, 0);
    digitalWrite(LED3, 0);
    digitalWrite(LED4, 0);
    delay(interval);
  }

  else if(ledEffect == 3)
  {
    for(chanVar; chanVar < 3; chanVar++)
    {
      ledChanInc();
      if(DEBUG) {
        Serial.print("chanVar: ");
        Serial.println(chanVar);
      }
      delay(interval);
    }

    for(chanVar; chanVar > 0; chanVar--)
    {
      ledChanDec();
      if(DEBUG) {
        Serial.print("chanVar: ");
        Serial.println(chanVar);
      }
      delay(interval);
    }
  }
}

void relEffectApply()
{
  if(relEffect == 0)
  {
    digitalWrite(Rel1, 0);
  }

  else if(relEffect == 1)
  {
    digitalWrite(Rel1, 1);
  }

  else if(relEffect == 2)
  {
    digitalWrite(Rel2, 0);
  }

  else if(relEffect == 3)
  {
    digitalWrite(Rel2, 1);
  }
}

Blocks:

The issue happens when the buttons LED_toggle, Start, and Relay_"x"_toggle are pressed. It is supposed to verify if bluetooth is connected, but it still will proceed to showing next screen and I continue getting broken pipe errors whenever bluetooth isn't connected and I try to turn the LED's or the relay's on.

Disable Clock1 in Screen1.Iniitialize, to prevent premature transmission attempts by its Timer before BlueTooth has connected.

Also, shouldn't your sketch loop be testing if hc05.available () before doing a hc05.read()?

Hello ABG!
Thanks for answering my post and for the suggestion!

Implementing:

if(hc05.available())
  h05 = hc05.read();

Seems to have given me more stable inputs and prints only the useful data instead of the "-1"s when debugging.

About the blocks, I'm assuming this is what you suggested:

Still, though, the conditions I'm testing in the button click I mentioned don't seem to work.

The condition seems to work just fine when "Blutooth_Devices_List .AfterPicking" doesn't get a connection or if my device isn't paired to the module. I detected a problem, which was that whenever I tried disconnecting the module from power.

I'm unable to send anything but the condition doesn't seem to fail and cause the intended result.

Also, I've recently found out that I can just check for the broken pipe errors. Still, I'd like to know why did my solution not work.

Flashing button click

And

Sequence button click

Lack connectedness tests before trying to send Bluetooth data.

You're correct! But even if I added that, it didn't work in the other tests and I'd like to know why that is!?

Upload your new blocks and aia?

Hello ABG! So sorry for the response delay. (I've been quite busy)

The following aia file and blocks correspond to new ones, ever since I decided to opt for a new solution!
For obtaining the results I had before just remove the broken pipe error verification.

BT_control (1).aia (204.9 KB)

Thanks in advance for any feedback!

Aditionally, I'd like to ask about the current method I'm using for changing the interface. (Hide/Show Buttons/Elements)
Do you think maybe using tinyDB for module address and having different screens is a better approach to it? Is there a better solution than both of these?

You know that switching screens breaks Bluetooth connections, right?

You guarded a few Button Click events with connection checks before transmitting to BlueTooth, but you did not guard the Clock Timer.

Yes I do acknowledge that! That's exactly why I'm hiding/showing icons instead of switching screens.

Thanks for the suggestion!

What I've been wandering is if tinyDB could carry the MAC address to another screen so that I could separate interfaces and if that's a better solution than what I currently have.

Yes you can
But as .

you have to reconnect on the other screen

Taifun