Hi, I would like someone to help me with my 433 MHz RF module. I have 2 arduino uno's, one transmitter connected to D12 and one receiver connected to D12 (from the other arduino). On my arduino-transmitter, there is the buttonA on D13 and buttonB on D9 . The one on the receiver, the LEDA on D9 and LEDB on D7.
My problem is that my code should allow button B to turn on LEDB if pressed and turn off LEDB if button B is not pressed. Same principle as A. The code works well in B (even if you mustn't press too fast otherwise the LEDB doesn't light up sometimes but it's not important). On the other hand, in A, when I press button A, the LEDA lights up and goes out at intervals of about 1 second.
Here is the transmitter code:
#include <VirtualWire.h>
char controller;
const int buttonA = 13;
const int buttonB = 9;
const char CMD_BUTTON_A = "BPAxxxx" ;
const char* CMD_BUTTON_B = "BPB" ;
const char* CMD_BUTTON_NUL = "NUL" ;
int buttonState1 = 0;
int buttonState2 = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
//pinMode(13,OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(12);
vw_setup(4500);// speed of data transfer Kbps// smaller, better range.
// to change if needed to improve the transmitted signal
//delay(100);
}
void loop()
{
//byte message[VW_MAX_MESSAGE_LEN];
buttonState1 = digitalRead(buttonA);
buttonState2 = digitalRead(buttonB);
Serial.println(controller);
if (buttonState1 == HIGH)
{
controller="1" ;
vw_send((byte*) CMD_BUTTON_A, strlen(CMD_BUTTON_A) + 1);
vw_wait_tx(); // Wait until the whole message is gone
//digitalWrite(13,LOW);
}
if (buttonState2 == HIGH)
{
controller="2" ;
vw_send((byte*) CMD_BUTTON_B, strlen(CMD_BUTTON_B) + 1);
vw_wait_tx(); // Wait until the whole message is gone
//digitalWrite(13,LOW);
}
else
{
controller="0" ;
vw_send((byte*) CMD_BUTTON_NUL, strlen(CMD_BUTTON_NUL) + 1);
vw_wait_tx(); // Wait until the whole message is gone
//digitalWrite(13, HIGH);
}
}
Here is the receiver:
#include <VirtualWire.h>
const byte PIN_LED_A = 9;
const byte PIN_LED_B = 7;
const char* CMD_BUTTON_A = "BPAxxxx";
const char* CMD_BUTTON_B = "BPB" ;
const char* CMD_BUTTON_NUL = "NUL";
void setup()
{
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_rx_pin(12);
vw_setup(4500); // Bits per sec
Serial.begin(9600);
pinMode(PIN_LED_A, OUTPUT);
pinMode(PIN_LED_B, OUTPUT);
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
byte message[VW_MAX_MESSAGE_LEN];
byte message_size = VW_MAX_MESSAGE_LEN;
//is the maximum size in bytes of a message
//provided by the Virtualwire library
Serial.println();
if (vw_get_message(message, &message_size)) //Non-blocking//recover the message
{
if (strcmp((char*) message, CMD_BUTTON_A) == 0)
{
digitalWrite(9,HIGH);
//digitalWrite(7,);
}
if (strcmp((char*) message, CMD_BUTTON_B) == 0)
{
//digitalWrite(9,LOW);
digitalWrite(7,HIGH);
}
if (strcmp((char*) message, CMD_BUTTON_NUL) == 0)
{
digitalWrite(9,LOW);
digitalWrite(7,LOW);
}
}
}
For those that would have noticed it, that it is the A or the B, the operations are the same one thus I do not understand why A does not work whereas B perfectly. I put some "control" to see if arduino emitter send the message, in B it send "2" when I press but in A, it display all the time "0". I think that A is blinking because it feels that the button A is pressed but does not receive the message.
Here you have all the info and any help is welcome.
Translated with DeepL Translate: The world's most accurate translator (free version)