How to use Virtual Wire Library

So this is the correction what u are suggesting

vw_((uint8_t *)i,2);

Yes.

As a noob , you have to wait five minutes between forum posts. Why don't you use those minutes to try things out, or doing more reading?

I am trying to learn from different examples, but i am not able find enough examples or i am not able understand the existing code

So, how did the mod in reply #20 go?

i am not able understand the existing code

You can ask questions on this forum. Post a line of code that you don't understand and ask for an explanation.

We generally recommend that people start with the very simple examples provided with the Arduino IDE, to learn the language and the special features of the Arduino, before starting a project which uses advanced techniques.

Thanks for all your help guys
As you said i tried an example and came up with this code

This is for the transmitter side

#include<VirtualWire.h>

int xi=A0;
int xa,xo;
char *controller;

void setup()
{
  Serial.begin(9600);
  pinMode(xi,INPUT);
  vw_set_ptt_inverted(true);
  vw_set_tx_pin(4);
  vw_setup(4000);
}

void loop()
{
  Serial.println("Start");
  xa=analogRead(xi);
  Serial.println(xa);
  if (xa<=400)
  {
    controller="2";
    Serial.println("clock");
  }
  else if(xa>=400 & xa<=800)
  {
    controller="1";
    Serial.println("stop");
  }
  else if(xa>=800)
  {
    controller="0";
    Serial.println("anticlock");
  }
  Serial.println("");
  vw_send((uint8_t *)controller,strlen(controller));
  vw_wait_tx();
  delay(500);
}

The A0 is connected to a potentiometer

This is the code for the Receiver side

#include<VirtualWire.h>
int en1=9;
int i1=7;
int i2=6;

void setup()
{
  pinMode(en1,OUTPUT);
  pinMode(i1,OUTPUT);
  pinMode(i2,OUTPUT);
  digitalWrite(en1,1);
  Serial.begin(9600);
  Serial.println("setup");
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(12);
  vw_setup(4000);
  vw_rx_start();
}

void loop()
{
  Serial.println("Start");
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen=VW_MAX_MESSAGE_LEN;
  if(vw_get_message(buf,&buflen));
  {
    if(buf[0]=='2')
    {
      digitalWrite(i1,1);
      digitalWrite(i2,0);
      Serial.println("clock");
    }
    else if(buf[0]=='1')
    {
      digitalWrite(i1,0);
      digitalWrite(i2,0);
      Serial.println("stop");
    }
    else if(buf[0]=='0')
    {
      digitalWrite(i1,0);
      digitalWrite(i2,1);
      Serial.println("anticlock");
    }
    delay(500);
  }
}

On the reciever side I am using an Hbridge module

This code is working fine for me
In this code I am able to send 8bit data using single variable

Now what i want to learn is that how to transmit and receive 2 variables
And also transmit and receive data of 16bit length
How to edit the existing code

Thanks for your help

Hint: a 16 bit variable consists of two bytes,

This code is working fine for me

I'm sure it does, but it is clear that you don't understand it. You need to understand your own code before you can move on. If we write the code for you, you won't understand it.

For example, the following line [u]already[/u] sends as many bytes as there are in the character array "controller". Can you figure out why?

vw_send((uint8_t *)controller,strlen(controller));

However, in the receiver code, you ignore the received length of the buffer and just examine the first character. Why are you ignoring "buflen"?

if(vw_get_message(buf,&buflen));
  {
    if(buf[0]=='2')

Thanks for telling me to learn the
That is the main reason I didnt explain my entire project
I only explained a part of my project
Thanks again

U asked me how does the length of controller works
controller is a char variable, hence the end of the string is mensioned using a null character(/0)

And for your next question, y i am ignoring buflen
i thought i was sending only 1 char(0,1,2), hence i left that part

Are u specifying there is a significance for buflen
Please teach me

Thank you

I am finding a small problem after using the following code

//tx code with analog stick test 4
#include<VirtualWire.h>
char *controller;
int xi=A0;
int yi=A1;
int xa,ya,xo=1,yo=1;
void setup()
{
  Serial.begin(9600);
  Serial.println("setup");
  vw_set_ptt_inverted(true);
  vw_set_tx_pin(12);
  vw_setup(4000);
  pinMode(xi,INPUT);
  pinMode(yi,INPUT);
}

void loop()
{
  //code for converting  
  //analog stick values to 0,1,2 
  xa=analogRead(xi);
  ya=analogRead(yi);
  if (xa<=600)
  {
    xo=2;
  }
  else if (xa>=600 & xa<=900)
  {
    xo=1;
  }
  else if (xa>=900)
  {
    xo=0;
  }
  //
  if (ya<=400)
  {
    yo=2;
  }
  else if (ya>=400 & ya<=600)
  {
    yo=1;
  }
  else if (ya>=600)
  {
    yo=0;
  }
  //code for transmitting the analog stick control input
  if (yo==2)
  {
    //forward
    controller="0";
  }
  else if(yo==0)
  {
    //backward
    controller="1";
  }
  else if(yo==1 && xo==2)
  {
    //right
    controller="2";
  }
  else if(yo==1 && xo==0)
  {
    //left
    controller="3";
  }
  else 
  {
    //stop
    controller="4";
  }
  digitalWrite(13,HIGH);
  vw_send((uint8_t *)controller,1);
  vw_wait_tx();
  Serial.print("controller:");
  Serial.println(controller);
  digitalWrite(13,LOW);
  delay(200);
}

the led at pin 13 should blink every 200 ms, which is 5 times a minute
this is to check if the transmission has completed successfully or not
But my led just staying on
It worked once
Can u pls help me with this

200ms is 5 times a second.

Also if-else-if sequences should end with only an else, covering all cases not handled before. Else no branch may be taken at all, if something is wrong with the conditions or values.

What u said is right it is 5 times a second

I also changed the last else if to else but no effect

When does this instruction

vw_wait_tx();

exit the wait

and in what condition does it stay inside the instruction

I am a facing a problem with the following code

// rx with motor test 5

#include<VirtualWire.h>
const int en1=9;
const int en2=8;
const int i1=7;
const int i2=6;
const int i3=5;
const int i4=4;

void setup()
{
    Serial.begin(9600);	// Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec
    vw_set_rx_pin(11);
    vw_rx_start();       // Start the receiver PLL running
    
    pinMode(en1,OUTPUT);
    pinMode(en2,OUTPUT);
    pinMode(i1,OUTPUT);
    pinMode(i2,OUTPUT);
    pinMode(i3,OUTPUT);
    pinMode(i4,OUTPUT);
    digitalWrite(en1,HIGH);
    digitalWrite(en2,HIGH);
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;

        digitalWrite(13, true); // Flash a light to show received good message
	// Message with a good checksum received, dump it.
	Serial.print("Got: ");
	
	for (i = 0; i < buflen; i++)
	{
	    Serial.print(buf[i]);
	    Serial.print(" ");
            
            if (buf[i]==30)
            {
              Serial.println("FORWARD");
            }
            else if (buf[i]==31)
            {
              Serial.println("BACKWARD");
            }
            else if (buf[i]==32)
            {
              Serial.println("RIGHT");
            }
            else if (buf[i]==33)
            {
              Serial.println("LEFT");
            }
            else if (buf[i]==34)
            {
              Serial.println("STOP");
            }
            else 
            {
              Serial.println("INVALID");
            }
        }
        

	Serial.print("");
        
        
        digitalWrite(13, false);
        
        Serial.println("");
        delay(200);
    }
}

using this code i am getting 30, 31, 32, 33, 34 at buf on serial monitor
but only one string is being printed for all the conditions “INVALID”
according to my knowledge i verified the code, and i could not find any bugs
Please help me with this problem
Thank you

Plz show the code from transmitter.

Since buf is a local array, I only can imagine stack corruption for that strange behaviour. Output the received code once more, in the "INVALID" case.

This is the transmitter code

//tx with analog stick test 5
#include <VirtualWire.h>
char *controller;
int xi=A0;//x axis of analog stick
int yi=A1;//y axis of analog stick
int xa,ya,xo=1,yo=1;

void setup()
{
    Serial.begin(9600);  // Debugging only
    Serial.println("setup");
    
    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000); // Bits per sec
    vw_set_tx_pin(12);   //set pin 12 as transmitter pin
    
    pinMode(xi,INPUT);
    pinMode(yi,INPUT);
}

void loop()
{
    //code for converting  
    //analog stick values to 0,1,2 
    xa=analogRead(xi);
    ya=analogRead(yi);
    if (xa<=600)
    {
      xo=2;
    }
    else if (xa>=600 & xa<=900)
    {
      xo=1;
    }
    else 
    {
      xo=0;
    }
    //
    if (ya<=400)
    {
      yo=2;
    }
    else if (ya>=400 & ya<=600)
    {
      yo=1;
    }
    else
    {
      yo=0;
    }
    //code for transmitting the analog stick control input
    if (yo==2)
    {
      //forward
      controller="0";
    }
    else if(yo==0)
    {
      //backward
      controller="1";
    }
    else if(yo==1 && xo==2)
    {
      //right
      controller="2";
    }
    else if(yo==1 && xo==0)
    {
      //left
      controller="3";
    }
    else 
    {
      //stop
      controller="4";
    }
    
    digitalWrite(13, true); // Flash a light to show transmitting
    vw_send((uint8_t *)controller, strlen(controller));
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite(13, false);
    delay(200);
}

Review an ascii table and consider the difference between the hex and decimal representation of the character.

You need to specify hex 0x30 … 0x34. for ascii 1…5.

Your code is expecting decimal 48…53. not 30-34

EDIT: buf is a byte array, so you could cast to char and use an expression like if ((char)buf[i] == '1'))

i understand what u implied and fixed the code

what instruction should i use to transmit a integer data read from an analog pin or a variable data

   int value=1019;  //some number
   vw_send((uint8_t *)&value, 2);  //don't forget the "&"

I did a Forum search for 433MHz and came up with loads of results such as:

https://forum.arduino.cc/index.php?topic=345159.0

Why not save yourself a bundle of posts and ask one of these people if they would share their code?

It's all been done before, if one could but find it!