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