I am working on a simple project. Press a button on unit A and unit B lights up a LED for the duration of the button press (momentary), and vise versa. I have only been able to get them to work in one direction. I have figured out the last unit to power up is the one able to receive the signal and light up i.e. power up unit A then unit B, only B will light up. I am new to Arduino and can't for the life of me figure this out.
Each unit is an Arduino Nano with and HC-12, LEDs /w resistors, and a switch with a pulldown resistor and running the same sketch.
Any help would be amazing.
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int button = 2;
int led = 6;
boolean onOff = 0;
void setup() {
pinMode (button, INPUT);
pinMode (led, OUTPUT);
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
}
void loop() {
int buttonState = digitalRead(button);//read button state
if(buttonState == 1){//if button is down
HC12.println(1111);//send unique code to the receiver to turn on.
onOff = 1;//set boolean to 1
}
if(buttonState == 0 && onOff == 1){//Verifier to send off signal once
HC12.println(2222);//send unique code to the receiver to turn off. In this case 2222
}
if(HC12.available() > 1){
int input = HC12.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
if(input == 1111){//if on code is received
digitalWrite(led, HIGH);//turn LED on
}
if(input == 2222){//if off code is received
digitalWrite(led, LOW);//turn LED off
}
}
delay(20);
}
You have taken steps to ensure that the "off signal" is sent only once - at the moment the button is released - this is correct. It would be even more correct to send the "on signal" only once too - only at the moment the button pressed. You can use the same onOff flag for it.
Sending codes as 4-chars strings and reading them with parseInt() function is definitely a bad idea. It would be much faster to send only one byte code and read them simply with the read() command.
you are using the SoftwareSerial library which has the restriction It cannot transmit and receive data at the same time. - this may be part of your problem
you could try AltSoftSerial
Thank you horace. I tried AltSoftSerial and I get nothing at all now. I am at a loss
#include <AltSoftSerial.h>
AltSoftSerial HC12(2, 3); // HC-12 RX=2, TX=3
int button = 8;
int led = 6;
boolean onOff = 0;
void setup() {
pinMode (button, INPUT);
pinMode (led, OUTPUT);
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
}
void loop() {
int buttonState = digitalRead(button);//read button state
if(buttonState == 1){//if button is down
HC12.println(1);//send unique code to the receiver to turn on.
onOff = 1;//set boolean to 1
}
if(buttonState == 0 && onOff == 1){//Verifier to send off signal once
HC12.println(2);//send unique code to the receiver to turn off. In this case 2222
}
if(HC12.available() > 1){
int input = HC12.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
if(input == 1){//if on code is received
digitalWrite(led, HIGH);//turn LED on
}
if(input == 2){//if off code is received
digitalWrite(led, LOW);//turn LED off
}
}
delay(20);
}