Smart Home Automation Forum
KC868-A4 DIGITAL I/O - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-A4 (https://www.kincony.com/forum/forumdisplay.php?fid=21)
+--- Thread: KC868-A4 DIGITAL I/O (/showthread.php?tid=5442)



KC868-A4 DIGITAL I/O - franco.demei@gmail.com - 04-02-2024

Hi all,
I'm trying to program the KC868-A4 to activate the relay, as long as the corresponding digital input is activated. When I release the button the corresponding relay should deactivates.
With the  code I wrote, when I close the digital input the relevant relay turns on. But when I open the  digital imput, The  relay remains on.  Can some one tell me where is a mistake ? Below the code in use. Thank You

#define Relay1 2
#define Relay2 15
#define Relay3 5
#define Relay4 4
#define Key1 36
#define Key2 39
#define Key3 27
#define Key4 14
int KEY_NUM1; // KEY_1 value
int KEY_NUM2; // KEY_2 value
int KEY_NUM3; // KEY_3 value
int KEY_NUM4; // KEY_4 value
void setup() {
  pinMode(Relay1, OUTPUT); // Relay1 IO2
  pinMode(Relay2, OUTPUT); // Relay2 IO15
  pinMode(Relay3, OUTPUT); // Relay1 IO5
  pinMode(Relay4, OUTPUT); // Relay1 IO4
 
  pinMode(Key1, INPUT); // Dig. Input1 IO36
  pinMode(Key2, INPUT); // Dig. Input2 IO39
  pinMode(Key3, INPUT); // Dig. Input3 IO27
  pinMode(Key4, INPUT); // Dig. Input4 IO14
}
void loop() {
  ScanKey1(); // Call function to scan Key1
  ScanKey2(); // Call function to scan Key2
  ScanKey3(); // Call function to scan Key3
  ScanKey4(); // Call function to scan Key4
  }
void ScanKey1() {
  KEY_NUM1 = 0;
 
  // Check if Key1 is pressed
  if (digitalRead(Key1) == LOW) { // Key1 Pressed
    delay(20); // Wait for button debounce
    if (digitalRead(Key1) == LOW) {
      KEY_NUM1 = 1;
     
      // Toggle relay1 state until Key1 is released
      while (digitalRead(Key1) == LOW) {
        digitalWrite(Relay1, !digitalRead(Relay1)); // Toggle relay1 state
        delay(100); // Adjust delay as needed to control toggle speed
      }
    }
  }
}
void ScanKey2() {
  KEY_NUM2 = 0;
  // Check if Key2 is pressed
  if (digitalRead(Key2) == LOW) { // Key2 Pressed
    delay(20);
    if (digitalRead(Key2) == LOW) {
      KEY_NUM2 = 1;
      // Toggle relay2 state until Key2 is released
      while (digitalRead(Key2) == LOW) {      
       digitalWrite(Relay2, !digitalRead(Relay2)); // Toggle relay2 state
        delay(100); // Adjust delay as needed to control toggle speed
      }
    }
  }
}
void ScanKey3() {
  KEY_NUM3 = 0;
  // Check if Key3 is pressed
  if (digitalRead(Key3) == LOW) { // Key3 Pressed
    delay(20);
    if (digitalRead(Key3) == LOW) {
      KEY_NUM3 = 1;
      // Toggle relay3 state until Key3 is released
      while (digitalRead(Key3) == LOW) {      
       digitalWrite(Relay3,HIGH); // Toggle relay3 state
        delay(100); // Adjust delay as needed to control toggle speed
      }
    }
  }
}
void ScanKey4() {
  KEY_NUM3 = 0;
  // Check if Key4 is pressed
  if (digitalRead(Key4) == LOW) { // Key4 Pressed
    delay(20);
    if (digitalRead(Key4) == LOW) {
      KEY_NUM3 = 1;
      // Toggle relay4 state until Key4 is released
      while (digitalRead(Key4) == LOW) {      
       digitalWrite(Relay4,HIGH); // Toggle relay4 state
        delay(100); // Adjust delay as needed to control toggle speed
      }
    }
  }
}



RE: KC868-A4 DIGITAL I/O - admin - 04-02-2024

your code function is when digital input trigger, Toggle relay.
you can replace this code:
digitalWrite(Relay1, !digitalRead(Relay1));

use if .... else ... for ON/OFF relay.


RE: KC868-A4 DIGITAL I/O - franco.demei@gmail.com - 04-03-2024

(04-02-2024, 09:52 PM)admin Wrote: your code function is when digital input trigger, Toggle relay.
you can replace this code:
digitalWrite(Relay1, !digitalRead(Relay1));

use if .... else  ...    for ON/OFF relay.

Thank you Sir, 
SOLVED  in this way :
void ScanKey1() {
  KEY_NUM1 = 1;                               // Key1 unpressed
  if (digitalRead(Key1) == LOW) {             // Key1 Pressed
    delay(20);
     if (digitalRead(Key1) == LOW) {
      KEY_NUM1 = 0;                           // if digitalRead(Key1 == low)
         digitalWrite(Relay1, HIGH);          // Relay1 is Turned ON
        } else {                              // if Key1 is turned OFF (Key4 open)
      digitalWrite(Relay1, LOW);              // Rela1 is turned OFF
      }
    }
 }



RE: KC868-A4 DIGITAL I/O - admin - 04-03-2024

ok, good.