433 MHz TX RX questions

Hi All

I am fairly new to Arduino programming. I am trying to get my head wrapped around communicating between two Arduinos over RF connection. I have a 433MHz TX and RX. I have the transmitter plugged into an Arduino Mega with the data line attached to Digital 7. The Receiver is attached to an Arduino Uno and the data line is attached to Digital 7 as well.

The TX code has INT variable called test_count which increments every 1 second. The value of this variable is then transmitted.

Here is the transmitter code:

//Transmitter (Mega)
#include <VirtualWire.h>

int test_count = 0;

void setup()
{
  Serial.begin(9600);
  
  vw_setup(2000);
  vw_set_tx_pin(7);
}

void loop()
{
  vw_send((uint8_t *)home_score, 1);
  delay(1000);
}

The RX code also has INT variable called test_count. What I am trying to achieve is to make the test_count equal to the data received from the transmitter and then display it using the serial monitor.

Here is the code for the receiver so far:

//Receiver (Uno)
#include <VirtualWire.h>

int home_score;

void setup()
{
  Serial.begin(9600);	// Debugging only
  
  vw_setup(2000);
  vw_set_rx_pin(7);
  vw_rx_start();
}

void loop()
{
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  
  if(vw_get_message (buf, &buflen))
  {
    for(int i=0;i< buflen;i++)
    {
      home_score = buf[i];
    }
  }

  delay(1000);
  Serial.println(home_score);
}

The problem I am having is that the serial monitor only prints a 0 after every second. I would really appreciate if someone can help me fix this code. I would also really appreciate if someone can help me understand how I can send values of multiple variable and receive and store them correctly.

for(int i=0;i< buflen;i++)
    {
      home_score = buf[i];
    }
  }

Because "home_score" only ever gets set to the last byte in the buffer?

Would the buffer not update when new data arrives?

Why don't you print all the contents of the buffer, and find out?

(please don't modify code after it has been quoted - it makes the thread hard to follow)

Sorry for changing the code. I will change it back to what you have quoted.

I have just tried printing the contents of the buffer but it is still printing 0.

Here is what the receiver code looks like:

//Receiver (Uno)
#include <VirtualWire.h>

int home_score;

void setup()
{
  Serial.begin(9600);	// Debugging only
  
  vw_setup(2000);
  vw_set_rx_pin(7);
  vw_rx_start();
}

void loop()
{
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  
  if(vw_get_message (buf, &buflen))
  {
    for(int i=0;i< buflen;i++)
    {
      Serial.println(buf[i]);
    }
  }

  delay(1000);
//  Serial.println(home_score);
}

it is still printing 0.

Maybe because that's what you're sending? (even allowing for the fact this doesn't compile)

//Transmitter (Mega)
#include <VirtualWire.h>

int test_count = 0;

void setup()
{
  Serial.begin(9600);
  
  vw_setup(2000);
  vw_set_tx_pin(7);
}

void loop()
{
  vw_send((uint8_t *)home_score, 1);
  delay(1000);
}

Try:

#include <VirtualWire.h>

byte home_score = 0;

void setup()
{
  Serial.begin(9600);
  
  vw_setup(2000);
  vw_set_tx_pin(7);
}

void loop()
{
  vw_send((uint8_t *)&home_score, 1);
  delay(1000);
  home_score++;
}

My bad and thanks for you reply.

I have fixed the code now. However the result of printing the buffer through serial monitor are quite strange. The numbers being printed are quite random with zeros, 6, 248, etc. I was expecting the value of variable received from the transmitter to be printed or am I missing something basic again.

I'm making a tutorial that will help with your problem and will you let to trasmit valuables using virtualwire.Also you can trasmit more than one valuable.

The tutorial will be finished before of the end of the month, I hope, in my blog controlrobotics.rodrigomompo.com

If you distribute it please use the link of the blog, thanks

very beautiful your presentation :), I'll try it too, you can set and some videos with you set there blog

I will try to upload some videos when y received my new camera and if the studies let me time