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