VirtalWire Example question

I used the VW library this weekend with an RF unit and it was really cool once I finally got the two modules to speak to each other. I used the examples in the library and noticed that the transmitter example sends the word “hello” and the receiver code receives the “hello” and displays it in HEX. Well I didn’t care for the hex so I omitted it and got “h e l l o” which was cool. Can anyone tell me where the spaces between the letters came from?

Transmitter Code:

// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@open.com.au)
// Copyright (C) 2008 Mike McCauley
// Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp

#include <VirtualWire.h>

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

Receiver Code:

// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@open.com.au)
// Copyright (C) 2008 Mike McCauley
// Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp

#include <VirtualWire.h>

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

  • Serial.print(" ");*
  • }*
  • Serial.println("");*
  • digitalWrite(13, false);*
  • }*
    }
    [/quote]

Serial.print("Got: ");

for (i = 0; i < buflen; i++)
{
Serial.print(buf*, HEX);*

  • Serial.print(" ");*
  • }*
  • Serial.println("");*
    [/quote]
    You printed it…

/facepalm

Lets say I had a green and red led hooked up to the receiver arduino. Could I simply use an if statement to turn the led of my choosing on? For example, if I have two buttons on the transmitter arduino that tell the arduino to send “GREEN” or “RED” out to the reciever would this modifactin of the receiver code turn the corresponding led on?

Receiver:

for (i = 0; i < buflen; i++)
{
val=buf

  • }*
  • if (val==“GREEN”) {*
  • digitalWrite(GreenLED, HIGH);*
  • }*
  • if (val==“RED”) {*
  • digitalWrite(RedLED, HIGH);*
  • }*
    [/quote]
    I just started using the arduino a few weeks ago so my coding is week.

val==“GREEN”)

This you can’t do.
Look at “strcmp”, and make sure your incoming string is terminated with a ‘\0’

How about this?

if( strcmp(val, “GREEN” ) == 0 ) {
digitalWrite(GreenLED, HIGH);
}

I think “val” should be “buf” (which will have to have a ‘\0’ terminating the string), but yes, tthat’s the idea.

So you think I can use the buf variable that’s already been initialized and populated? Would you happen to know the C command to append “\0” to buf?

How about this?

No, because of of this:

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

How is val defined? Why copy the contents of the buf array to val? Where is the ; on the end of the statement.

If you get rid of this block of code, and do this:

if(strcmp(buf, "GREEN"))
{
    // Turn the green light on
}

This should work.

I figured I would have to copy the contents of the buf array to a variable because to me it looks like I need the “for” loop to populate it character by character so it can be compared. I think I would define val like so:

char val

Or is the buf array already populated with what was sent over and the for loop in the receiver code is just segmenting the buf array out char by char?

I know this is basic stuff and hope I’m not annoying the community lol.

But your “val” had no subscript, so that wouldn’t work.
Don’t worry about the noob questions - we’ve all had to ask them at some time.

What you may need to do is “buf [buflen] = ‘\0’;” before your “strcmp” (I’m not sure if the library already does this for you, but it won’t hurt).

A normal C way of doing that would be

(assume buffer has what’s received, with the first char at buf [0])

if (strncmp (buf, “Green”, 5) == 0)
/* Turn on green LED /
else if (strncmp (buf, “Red”, 3) == 0)
/
Turn on red LED /
else
/
do something else */

You could also use the strncasecmp function to do a case-insensitive compare, so “Red”, “RED”, and “rEd” would all match. This avoids the problem of either figuring out how to append a ‘\0’ to the proper place at the end of input (which might not be the end of buf), or hoping you’ll hit a difference before it overruns.

Here’s an example… hopefully it’s easy enough to read, it’s an old sketch.

But basically, you put the incoming bytes into a string, then compare that string to another string. inString is the name of the string I used.

You can also just compare bytes, but I found it’s hit and miss with having an extra byte at the beginning being “random” or not what’s being sent.

/*
Nokia LCD Demo - A demo using the NokiaLCD library for a Nokia LCD with the epson driver.
Created by Thomas Jespersen, July 2009 (Originally Arduino Sketch by Gravitech.us)
Released into the public domain.
/
/
/
/
Interface LCD6610 to Arduino Nano /
/
Nano → LCD6610 /
/
D2 → BL /
/
D3 → #CS /
/
D4 → SCLK /
/
D5 → SDATA /
/
D6 → #RESEET /
/
+5V → VCC,VBL /
/
GND → GND /
/
*/

#include <NokiaLCD.h>
#include <VirtualWire.h>

#undef int
#undef abs
#undef double
#undef float
#undef round

// Nokia LCD Setup settings
#define RED 0xE0
#define GREEN 0x1C
#define BLUE 0x03
#define YELLOW 0xFC
#define MAGENTA 0xE3
#define CYAN 0x1F
#define BLACK 0x00
#define WHITE 0xFF
NokiaLCD nokiaLcd;

char inString[32];
int inCount;

void setup()
{
DDRD |= B01111100; // Set SPI pins as output
PORTD |= B01111100; // Set SPI pins HIGH

vw_set_rx_pin(7);
vw_set_ptt_inverted(true);
vw_setup(2000);
vw_rx_start();
pinMode(13, OUTPUT);

nokiaLcd.lcd_init();
delay(500);
nokiaLcd.lcd_clear(BLACK, 0, 0, 131, 131);
nokiaLcd.lcd_clear(WHITE, 0, 65, 131, 131);
Serial.begin(9600);
}

void loop()
{
const char *msg = “hello”;
const char *msg2 = “I’m here!”;

char text [50];
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
inCount = 0;

///////////////////////////////////
if (vw_get_message(buf, &buflen))
{
int i;
digitalWrite(13, true); // Turn LED on while receiving
Serial.print("Got: ");

for (i = 0; i < buflen; i++) // Begin reading?
{
Serial.print(buf*);*

  • //Serial.print(" ");*

  • // Here’s my problem, I think*
    _ //itoa(buf*, inString, 16); // puts info into an array?[/color]_
    _
    //using this, I only print out 6F (o in hex) which is the last of the message I’m sending*_

_ inString[inCount] = buf*; // this fixed it !
inCount++; //and this :D*
* }
Serial.println("");*_

* // inString[inCount] = ‘\0’;*

* if (strcmp(inString, “hello”) == 0){*
* strcpy(text, “Welcome!”);//buffer is being printed as text*
* nokiaLcd.lcd_draw_text(WHITE, BLACK, 10, 25, text); //
_ }*_

* if (strcmp(inString, “goodbye”) == 0){*
* strcpy(text, “Welcome!”);*
* nokiaLcd.lcd_draw_text(RED, BLACK, 10, 25, text);
_ }*_

* digitalWrite(13, false);*
* }*
*////////////////////////////////////////////// *
}
[/quote]