Let's Make Robots!

Sending a int using VirtualWire

I've got the basic examples reciever and transmitter working sending the default "hello" message.

But I really want to send a int, for example 45 or another value over RF links. Can't understand how to rewrite it to do so!

 

Anyone care to help me out?

 

Reciever code:

 

#include <VirtualWire.h>

#undef int

#undef abs

#undef double

#undef float

#undef round

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_rx_start();       // Start the receiver PLL running

}

 

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], HEX);

   Serial.print(" ");

}

Serial.println("");

        digitalWrite(13, false);

    }

}

 

 

 

Transmitter code:

 

#include <VirtualWire.h>

#undef int

#undef abs

#undef double

#undef float

#undef round

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

}

 

void loop()

{

    const char *msg = "hello";

 

    digitalWrite(13, true); // Flash a light to show transmitting

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

    vw_wait_tx(); // Wait until the whole message is gone

    digitalWrite(13, false);

    delay(200);

}

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

im trying to send the time via the same method and im at the same point

Ah, could you show me a example code? for just sending one number.

 

I am not at the computer with the Arduino IDE, so this is somewhat approximate...

int iVal = 45; // or whatever value you want to send

char acInt[6];

itoa( iVal, acInt, 10);

// acInt is now a string representation of the number 45 - pass it instead of the Hello World string

the other way:

int iVal = atoi( acStr); // acStr is your input buffer

 

that just makes ints into string and visa versa. In practice, you will need something to indicate that it is a number, so maybe you would put an X in front of it like this:

char acInt[7];

acInt[0] = 'X';

itoa( iVal, &acInt[1], 10);

and then send it and on the receive side do something like:

if( acStr[0] == 'X')

{

int iVal = atoi( &acStr[1]); // acStr is your input buffer

}
Strings are just arrays of characters. Parsing through them is tedious but not difficult if you get the basic concepts.

look up the functions atoi() and itoa() - they convert between strings and ints

I am curious about VirtualWire -  are there benefits over using NewSoftSerial ? I have been using nss mostly because I kept seeing it recommended on the Arduino board.