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.