#include <VirtualWire.h> // you must download and install the VirtualWire.h to your hardware/libraries folder
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RF Link module
vw_setup(2000); // Bits per sec
vw_set_tx_pin(3); // pin 3 is used as the transmit data out into the TX Link module, change this to suit your needs.
}
void loop()
{
const char *msg = "LMR-II Rocks"; // this is your message to send
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait for message to finish
delay(200);
}
and this is the code for the receiver
#include <VirtualWire.h> // you must download and install the VirtualWire.h to your hardware/libraries folder
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
Serial.begin(9600);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
vw_set_rx_pin(23); // We will be receiving on pin 23 (Mega) ie the RX pin from the module connects to this pin.
vw_rx_start(); // Start the receiver
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // check to see if anything has been received
{
int i;
// Message with a good checksum received.
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]); // the received data is stored in buffer
}
Serial.println("");
}
}
I have the transmitter code running on an arduino mega and the receiver running on an arduino uno. I can upload the codes and they work perfectly displaying the message and I had the mega about 15 feet away for testing and the message displayed on the serial monitor. But when I tried to modify the code to just make the arduino uno turn on an led I used this code for the receiver
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
int ledPin = 12;
void setup()
{
Serial.begin(2400);
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
vw_set_rx_pin(11);
vw_rx_start(); // Start the receiver
pinMode(ledPin , OUTPUT);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
Serial.print(buf[0], HEX);
if(buf[0] == 'led on') {
digitalWrite(ledPin ,HIGH);
}
}
}
and this code for the transmitter
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RF Link module
vw_setup(2000); // Bits per sec
vw_set_tx_pin(8); // pin 3 is used as the transmit data out into the TX Link module, change this to suit your needs.
}
void loop()
{
const char *open_msg = "led on";
vw_send((uint8_t *)open_msg, strlen(open_msg));
vw_wait_tx(); // Wait for message to finish
delay(15000);
}
nothing happens and the Tx light on the arduino uno doesn't light up rapidly like it did with the other code and so I was hoping someone could help me and figure out this problem please because I was hoping to see if I could use these codes with a touch screen lcd and have each button control the LED's and motion sensors hopefully later on.I am new to coding so I know I have made mistakes and I am on here trying to get some help and guidance. So please forgive the errors and I will appreciate any advice and help.
Do you have an antenna for the RX and for the TX module ?
Just a wire of about 17cm will do, but even any wire of 5cm to 50cm is better than no wire at all.
Please remove all the 'undef' lines. I don't know why the are used. I even think it is bad programming.
You can remove the lines for 'inverted' signals. Your transmitter and received doesn't have that.
The transmitter transmits only once in 15 seconds with: delay(15000);
Can you make that every second ?
As soon as this is valid: if (vw_get_message(buf, &buflen))
there is a valid reception with data, with valid checsum and everything.
You have a Serial.print after that, so you only have to see what is on the serial monitor.
Please update your sketches according these notes and show them again.
I changed it to this on the receiving end before I saw your post and it worked but I know it can benefit from your help you just gave.
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
int ledPin = 12; // output pin for valve one to sprayer**
void setup()
{
Serial.begin(9600);
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
vw_set_rx_pin(11);
vw_rx_start(); // Start the receiver
pinMode(ledPin, OUTPUT); // makes valve one pin an output**
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // check to see if anything has been received
{
Serial.print((char)buf[0], HEX);
if((char)buf[0] == 'o') {
digitalWrite(ledPin,HIGH);
}
else if((char)buf[0] == 'c') {
digitalWrite(ledPin,LOW);
}
}
}
and this is what I did for the transmitter
#include <VirtualWire.h> // you must download and install the VirtualWire.h to your hardware/libraries folder
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
vw_set_ptt_inverted(true); // Required for RF Link module
vw_setup(2000); // Bits per sec
vw_set_tx_pin(8); // pin 3 is used as the transmit data out into the TX Link module, change this to suit your needs.
}
void loop()
{ const char *open_msg = "on";
const char *close_msg= "cut off";
vw_send((uint8_t *)open_msg, strlen(open_msg));
vw_wait_tx(); // Wait for message to finish
delay(5000);
vw_send((uint8_t *)close_msg, strlen(close_msg));
vw_wait_tx();
delay(5000);
}
it worked just to see if it's possible to control it but now I want to connect it to a touchscreen LCD and when the button is pressed the arduino uno is to perform if statements but I have already tried messing with my code for the touch screen LCD and nothing has happened when I press the buttons. I thank you for your help and I will try to write my code better but I am still learning this stuff and don't know the proper way to write still. I need help now in getting the touch screen to work with the RF now, I will post my code for the tough screen soon and before I do I need to say I'm sorry for my poor coding before I post it.
I got it to work with the touch screen, I had grounding issues with the transmitter. Now to see if I can make a PIR motion detector turn on and make the LED turn on when motion is detected.
Good, progress.
You can omit this line: vw_set_ptt_inverted(true);
And you send text messages. It is easier to send just one byte. For example value 100 for 'on' and value 101 for 'off' or something like that.
When you want to send more, you can add an integer for temperature and so on.
Thanks I tried to put if statements in the code with a PIR motion sensor and it doesn't respond the way I want. I used the same code for the transmitter and just added this code
for the receiver and nothing happens. Instead sometimes when I press the button on and off real fast a bunch of times the LED lights up and doesn't react to the motion just turns on and off. I wondered if the recent code is still running and not this. lol
Please remove all the #undef lines. They hurt my eyes.
You use the integer 'val', but the value for that is set outside the 'setup()' and outside the 'loop()' function.
I checked the power coming out of the pin 12 when I press the button and it responds and gives me 4.17 volts so that's enough to run the sensor and I added the int val = digitalRead(sensor); into the loop right before the if (val == HIGH) and still I get nothing from the motion sensor, I don't know if it's getting values from it or not.
I ran a code just to test to see if the PIR sensor was working sand it worked properly turning on and off the LED when motion was detected but I noticed that the TX light flashes on and off when motion is detected. Is this the problem with it working with the RF because I noticed that the TX blinks when the button on the LCD with the transmitter is pressed. is it interfering?
No its not being affected directly by the RF transmission, but more than likely its in a floating state so you need to send led low or high when lcd button is pressed this way you are dictating its condition.