Go Down

Topic: How to use Virtual Wire Library (Read 2006 times) previous topic - next topic

DrDiettrich

You lost part of the source code, by copying from one post to another.

RakshithNarun

So what is the mistake i have  done in my code

Should i change my code from

Code: [Select]
vw_send((uint8_t *)msg,1);

to

Code: [Select]
vw_send((uint16_t *)msg,1);

Groove

No.
You need to send both bytes of the int variable.
Per Arduino ad Astra

RakshithNarun

pls some help to write the code for the above said example

Groove

Per Arduino ad Astra

RakshithNarun

So this is the correction what u are suggesting

Code: [Select]

vw_((uint8_t *)i,2);

Groove

#21
Mar 14, 2017, 03:20 pm Last Edit: Mar 14, 2017, 03:23 pm by Groove
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?
Per Arduino ad Astra

RakshithNarun

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

Groove

So, how did the mod in reply #20 go?
Per Arduino ad Astra

jremington

#24
Mar 14, 2017, 05:21 pm Last Edit: Mar 14, 2017, 05:22 pm by jremington
Quote
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.

RakshithNarun

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
Code: [Select]

#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

Code: [Select]

#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

Groove

Hint: a 16 bit variable consists of two bytes,
Per Arduino ad Astra

jremington

#27
Mar 15, 2017, 03:58 pm Last Edit: Mar 15, 2017, 04:00 pm by jremington
Quote
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 already sends as many bytes as there are in the character array "controller". Can you figure out why?

Code: [Select]
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"?

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

RakshithNarun

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

RakshithNarun

I am finding a small problem after using the following code

Code: [Select]

//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

Go Up