HC-12 for controlling switches

Hi every one. Can i used HC-12 module to remotely controlled 16 switches?

Thanks

I tried this sample from the net and it works fine for up to switches only. But when i extend it from 3 up to 16 switches, it gives me an intermittent problem… sometimes it turn on the switch and sometimes not… So i just wander if there is an alternative to use other module other than HC-12.

thanks

// transmitt side

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

int buttonPin = 8;
boolean onOff = 0;
void setup() {
  pinMode(buttonPin, INPUT);
  mySerial.begin(9600);
}

void loop() {
 
  int buttonState = digitalRead(buttonPin);
  
  if(buttonState == 1){//if button is down
    mySerial.println(1111);//send unique code to the receiver to turn on. In this case 1111
    onOff = 1;//set boolean to 1
  }
  if(buttonState == 0 && onOff == 1){//Verifier to send off signal once
    mySerial.println(0000);//send unique code to the receiver to turn off. In this case 0000
  }
  delay(20);//delay little for better serial communication
}
// receiver's side


#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int ledPin = 13;
unsigned long last = millis();//set timer

void setup() {
  mySerial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {

  boolean ledState = digitalRead(ledPin);//check if the LED is turned on or off. Returns 1 or 0
  
  if(mySerial.available() > 1){    
    int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)    

    if(millis() - last > 250){//if time now is 250 milliseconds greater than last time
      if(ledState == 0 && input == 1234){//if LED is off and button code is ok
          digitalWrite(ledPin, HIGH);
      }else if(ledState == 1 && input == 1234){//if LED is on and button code is ok
          digitalWrite(ledPin, LOW);
      }
      
    }
    
    mySerial.flush();//clear the serial buffer for unwanted inputs   
    last = millis();//reset timer   
  }
  delay(20);//delay little for better serial communication
 
}

How do you know ? What's the code you use to send messages to all the clients?

If we were to see your test code, we could may be suggest that flush() is possibly not a great idea.

Thanks. now i realized why im having problems sending data.

:slight_smile: :slight_smile:

By the way:

delay(20);//delay little for better serial communication

I don't think that is such a good idea. In 20ms your serial buffer will receive roughly 20 bytes at 9600 bauds. If you just read 1, deal with it and then wait again for 20ms, you add possibly another 20 bytes in the buffer... If there are lots of things going on, very quickly you will overflow the software serial buffer. I would just not slow down reading (It's OK to slow down sending if you want)

oh i see. purposely the reason why i wanted to control 16 switches was to control my remote control scoreboard using HC-12. However, when i tried to control lit 16 leds as a substitute for my switches it seems im having problem, such as, one led does not lit off when i released the button switch. It should lit off everytime i released the switch but it did not. Any idea as to how to do it. thanks Mr. JML

No idea without code and a diagram on how things are wired...

I used the same code i posted above but only extend up to 4 ledss. For 2 leds , im having no problem at all, but for 4 leds to 16 leds, i tsrated receiving unwanted output like it does led not lit off

// receiver


#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int ledPin4 = 10;


void setup() {
  mySerial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
}

void loop() {
   
  if(mySerial.available() > 1){    
    int input = mySerial.parseInt(); 
    
    if(input == 1111){//if on code is received
      digitalWrite(ledPin1, HIGH);//turn LED on
    }
    
    if(input == 0000){//if off code is received
      digitalWrite(ledPin1, LOW);//turn LED off
    }
    if(input == 2222){//if on code is received
      digitalWrite(ledPin2, HIGH);//turn LED on
   
  }
 if(input == 3333){//if off code is received
      digitalWrite(ledPin2, LOW);//turn LED off
    }

   if(input == 4444){//if on code is received
      digitalWrite(ledPin3, HIGH);//turn LED on
   
  }
 if(input == 5555){//if off code is received
      digitalWrite(ledPin3, LOW);//turn LED off
    } 
  if(input == 6666){//if on code is received
      digitalWrite(ledPin4, HIGH);//turn LED on
   
  }
 if(input == 7777){//if off code is received
      digitalWrite(ledPin4, LOW);//turn LED off
    } 
  mySerial.flush();//clear the serial buffer for unwanted inputs     
  
  delay(20);//delay little for better serial communication
 
  }
}

transmit side

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

int buttonPin1 = 6;
int buttonPin2 = 7;
int buttonPin3 = 8;
int buttonPin4 = 9;
boolean onOff1 = 0;
boolean onOff2 = 0;
boolean onOff3 = 0;
boolean onOff4 = 0;
void setup() {
  pinMode(buttonPin1, INPUT);
   pinMode(buttonPin2, INPUT);
    pinMode(buttonPin3, INPUT);
   pinMode(buttonPin4, INPUT);
  mySerial.begin(9600);
}

void loop() {
 
  int buttonState1 = digitalRead(buttonPin1);//read button state
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);//read button state
  int buttonState4 = digitalRead(buttonPin4);
  if(buttonState1 == 1){//if button is down
    mySerial.println(1111);
    onOff1 = 1;//set boolean to 1
  }
  if(buttonState1 == 0 && onOff1 == 1){
    mySerial.println(0000);
  }

  if(buttonState2 == 1){//if button is down
    mySerial.println(2222);
    onOff2 = 1;//set boolean to 1
  }
  if(buttonState2 == 0 && onOff2 == 1){
    mySerial.println(3333);
  }

  if(buttonState3 == 1 ){
    mySerial.println(4444);
  onOff3 = 1;
  }
  if(buttonState3 == 0 && onOff3 == 1){
    mySerial.println(5555);
  }
if(buttonState4 == 1 ){
    mySerial.println(6666);
  onOff4 = 1;
  }
  if(buttonState4 == 0 && onOff4 == 1){
    mySerial.println(7777);
  }
  
  delay(20);//delay little for better serial communication
}

Where do you reset your onOffx variables ? (Although not a massive issue the way your code work those booleans are kinda useless anyway just not turning off something which is already off (if you were to reset them))

Your delay at the end is OK for debouncing, Can you check with basic LEDs attached to the buttons that they are wired correctly ? (I.e. Don't connect to arduino just put a led and see if it turns on)