Flashing LED via HC-12 transmitter

Hi! This is my first time posting, thank you in advance for your help and patience. Big picture, I have 2 arduinos, one a "sender", and one a "receiver". They both have HC-12s. The "sender" has a push button, and when this is pressed, it sends a unique code, "1234", to the "receiver", which tells it to turn on it's LED. This LED is "Latched", meaning you push the button, the LED stays on, until it receives the same code, and then it turns off. Up to this point I have had success. The next thing I'm trying to do is have that latched LED flash, and remain flashing, until it again receives that unique code, at which point it turns off and stays off. With the sketch below, it currently flashes once then remains on. If I make the last entry a LOW after the delay, it flashes once, then remains off. Here's the sketch:

//HC-12 Toggle button Receive
//Autor Tom Heylen tomtomheylen.com


#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);
          delay(500);
          digitalWrite(ledPin,LOW);
          delay(500);
          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
 
}

// BLUE CORD IS PORt 7

I know there is a simple solution here, I just can't seem to figure it out on my own (due to inexperience and buffonery). The sketch below is what the "sender" has uploaded, I don't think it'll be a factor, but I'll include it anways.

//HC-12 Toggle button Send
//Autor Tom Heylen tomtomheylen.com


#include <SoftwareSerial.h>

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

int buttonPinGreen = 8;
int pinForLedGreen = 9;
int buttonPinBlue = 12;
int pinForLedBlue = 13;

unsigned long last = millis();//set timer


void setup() {
  pinMode(buttonPinGreen, INPUT);
  pinMode(pinForLedGreen, OUTPUT);
  pinMode(buttonPinBlue, INPUT);
  pinMode(pinForLedBlue, OUTPUT);

  //define pinMode for LEDs as outputs
  //define pinMode for LEDs as outputs

  mySerial.begin(9600);
}

void loop() {

  int buttonStateGreen = digitalRead(buttonPinGreen);
  int buttonStateBlue = digitalRead(buttonPinBlue);


  if (buttonStateGreen == 1) { //if button is down
    mySerial.println(1234);//send unique code to the receiver in this case 1234
  }

  if (buttonStateBlue == 1) { //if button is down
    mySerial.println(5678);//send unique code to the receiver in this case 5678
  }

    mySerial.flush();//clear the serial buffer for unwanted inputs
    last = millis();//reset timer
  
  delay(20);//delay little for better serial communication

}
// THE BLACK CABLE IS PORT 8

Again thank you for your help.

  • Rich

I know there is a simple solution here

There is. And, you know what it is.

Is the blinking in any way related to the reception of serial data? NO, it is not. The NEED to blink, or not, IS related to the reception of serial data.

So, create two functions. One will handle reading serial data, and setting a boolean variable to true or false, to define whether there is a need to blink.

The other will handle the blinking, IF there is a need to blink.

All that loop() should do is call those two functions - one unconditionally, the other conditionally.

I have developed the following program for the receiver, which now keeps blinking LED-L (at DPin-13) after receiving the code 1234 from the sender. The blinking goes OFF when the same code is again received. The program codes are prepared before @PaulS Post#1, and I am amazed to see that my program logic has followed what he has said in his Post#1. You can study my program very much in the light of Post#1.

Few Remarks:
1. In the sender program, the bouncing problem of the button at DPin-8 has to be resolved using hardware/software debouncer.

2. In the receiver section, you want to keep blinking the LED-L, which is a loop. You need to insert delay for the ON/OFF period of LED-L. This delay can not be offered using Arduino's delay() function as it is a blocking subroutine, and it will most likely prevent the ISR of the UART interrupt from being updated. I have added time delay using millis() function which works on polling the overflow condition of TCX timer.

//HC-12 Toggle button Receive
//Autor Tom Heylen tomtomheylen.com

unsigned long presentmillis;
#include <SoftwareSerial.h>

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

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

volatile byte flag1 = 0x00;

void setup() {
  mySerial.begin(9600);
  Serial.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) 
    Serial.println(input);   

   // if(millis() - last > 250)
    //{//if time now is 250 milliseconds greater than last time
      if(ledState == 0 && input == 1234)
      {
        flag1 = 0x01;
        ledBlink();
      }
      /*
      {//if LED is off and button code is ok
          digitalWrite(ledPin, HIGH);
          delay(500);
          digitalWrite(ledPin,LOW);
          delay(500);
          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
 
}

void ledBlink()
{
 // presentmillis = millis();
  while (flag1 == 0x01)
  {
    
     if(mySerial.available()> 1)
      {    
        int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767) 
         Serial.println(input);  
          if(flag1 == 0x01 && input == 1234)
          {
            flag1 = 0x00;
            digitalWrite(13, LOW);
            return;
          }
      } 
     
    
    
     while(millis() - presentmillis < 500)
    {
      digitalWrite(13, HIGH);
    }
    presentmillis = millis();

     while(millis() - presentmillis < 500)
    {
      digitalWrite(13, LOW);
    }
    presentmillis = millis();
    
  }
  
}

// BLUE CORD IS PORt 7