04-27-2024, 08:39 AM
I'm trying to program four momentary switch via bluetooth commands. I don't understand why I can't enable "Relay3".
To compile KC868-A4 code I'm using "Arduino IDE". To program Android code "MIT App Inventory" .
Here is the codes I Used. Can some one help me to understand where is a mistake ? Tank You.
AI Code
Arduino IDE Code:
#include "BluetoothSerial.h"
BluetoothSerial SerialBT; // RX | TX
int Relay1 = 2;
int Relay2 = 5;
int Relay3 = 15;
int Relay4 = 4;
void setup() {
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
Serial.begin(19200); // Serial monitor
SerialBT.begin(19200); // Bluetooth serial communication
Serial.println("Bluetooth Control for KC868-A4 Relays");
}
void loop() {
if (SerialBT.available()) {
char command = SerialBT.read();
// Command format: '1' to '4' for relays 1 to 4
if (command >= '1' && command <= '4') {
int relay = command - '1' + 2; // Calculate the relay control pin
digitalWrite(relay, !digitalRead(relay));
Serial.print("Relay ");
Serial.print(command);
Serial.print(" is ");
Serial.println(digitalRead(relay) ? "ON" : "OFF");
}
}
}
To compile KC868-A4 code I'm using "Arduino IDE". To program Android code "MIT App Inventory" .
Here is the codes I Used. Can some one help me to understand where is a mistake ? Tank You.
AI Code
Arduino IDE Code:
#include "BluetoothSerial.h"
BluetoothSerial SerialBT; // RX | TX
int Relay1 = 2;
int Relay2 = 5;
int Relay3 = 15;
int Relay4 = 4;
void setup() {
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
Serial.begin(19200); // Serial monitor
SerialBT.begin(19200); // Bluetooth serial communication
Serial.println("Bluetooth Control for KC868-A4 Relays");
}
void loop() {
if (SerialBT.available()) {
char command = SerialBT.read();
// Command format: '1' to '4' for relays 1 to 4
if (command >= '1' && command <= '4') {
int relay = command - '1' + 2; // Calculate the relay control pin
digitalWrite(relay, !digitalRead(relay));
Serial.print("Relay ");
Serial.print(command);
Serial.print(" is ");
Serial.println(digitalRead(relay) ? "ON" : "OFF");
}
}
}