Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
KC868-A4 Bluetooth momentary Switch
#1
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");
    }
  }
}
Reply
#2
this is KC868-A4 relay control by bluetooth arduino demo code, you can test it: https://www.kincony.com/forum/showthread.php?tid=1647
Reply
#3
(04-28-2024, 12:50 AM)admin Wrote: this is KC868-A4 relay control by bluetooth arduino demo code, you can test it: https://www.kincony.com/forum/showthread.php?tid=1647

I solved in this way:
Code:
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

#define Key1 36
#define Key2 39
#define Key3 27
#define Key4 14

#define RELAY1 2
#define RELAY2 15
#define RELAY3 5
#define RELAY4 4

int incoming;

void setup() {

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);

  pinMode(Key1, INPUT);
  pinMode(Key2, INPUT);
  pinMode(Key3, INPUT);
  pinMode(Key4, INPUT);

Serial.begin(19200);   // Serial monitor
  SerialBT.begin(19200); // Bluetooth serial communication
 
  Serial.println("Bluetooth Control for KC868-A4 Relays");
 
}

void loop() {

  ScanKey1();
  ScanKey2();
  ScanKey3();
  ScanKey4();

if (SerialBT.available()) {
    char command = SerialBT.read();
   
    // Controlla il comando ricevuto e attiva o disattiva il relè corrispondente
    switch(command) {
      case '1':
        digitalWrite(RELAY1, HIGH);
        break;
      case '2':
        digitalWrite(RELAY1, LOW);
        break;
      case '3':
        digitalWrite(RELAY2, HIGH);
        break;
      case '4':
        digitalWrite(RELAY2, LOW);
        break;
      case '5':
        digitalWrite(RELAY3, HIGH);
        break;
      case '6':
        digitalWrite(RELAY3, LOW);
        break;
      case '7':
        digitalWrite(RELAY4, HIGH);
        break;
      case '8':
        digitalWrite(RELAY4, LOW);
        break;
      default:
        break;
    }
  }
}
int KEY_NUM1;
int KEY_NUM2;
int KEY_NUM3;
int KEY_NUM4;

void ScanKey1() {
  KEY_NUM1 = 1;
  if (digitalRead(Key1) == LOW) {
    delay(20);
    if (digitalRead(Key1) == LOW) {
      KEY_NUM1 = 0;
      digitalWrite(RELAY1, HIGH);
    } else {
      digitalWrite(RELAY1, LOW);
    }
  }
}

void ScanKey2() {
  KEY_NUM2 = 1;
  if (digitalRead(Key2) == LOW) {
    delay(20);
    if (digitalRead(Key2) == LOW) {
      KEY_NUM2 = 0;
      digitalWrite(RELAY2, HIGH);
    } else {
      digitalWrite(RELAY2, LOW);
    }
  }
}

void ScanKey3() {
  KEY_NUM3 = 1;
  if (digitalRead(Key3) == LOW) {
    delay(20);
    if (digitalRead(Key3) == LOW) {
      KEY_NUM3 = 0;
      digitalWrite(RELAY3, HIGH);
    } else {
      digitalWrite(RELAY3, LOW);
    }
  }
}

void ScanKey4() {
  KEY_NUM4 = 1;
  if (digitalRead(Key4) == LOW) {
    delay(20);
    if (digitalRead(Key4) == LOW) {
      KEY_NUM4 = 0;
      digitalWrite(RELAY4, HIGH);
    } else {
      digitalWrite(RELAY4, LOW);
    }
  }
}
In attachment   Mit App     Inv. Blocks
Reply
#4
ok, good.
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)