Go Down

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

DrDiettrich

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.

RakshithNarun

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

vw_wait_tx();

exit the wait

and in what condition does it stay inside the instruction

RakshithNarun

I am a facing a problem with the following code

Code: [Select]

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

Carlmikael

Plz show the code from transmitter.

DrDiettrich

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.

RakshithNarun

This is the transmitter code

Code: [Select]

//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);
}

cattledog

#36
Mar 21, 2017, 03:39 pm Last Edit: Mar 21, 2017, 03:52 pm by cattledog
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
Code: [Select]
if ((char)buf[i] == '1'))

RakshithNarun

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

jremington

#38
Mar 22, 2017, 05:35 pm Last Edit: Mar 22, 2017, 05:38 pm by jremington
Code: [Select]
   int value=1019;  //some number
   vw_send((uint8_t *)&value, 2);  //don't forget the "&"

Pat-The-Pirate

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!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy