Go Down

Topic: VirtualWire confusions (Read 679 times) previous topic - next topic

2MuchNFO

Hi all,
bit of an epic but I've tried to include all relevant info.

I bought a 4800bps 315Mhz RF link to have a play with..
http://www.coolcomponents.co.uk/catalog/product_info.php?cPath=25&products_id=186

Connecting one side to an Arduino Duemilanove and the other to an Arduino Nano - both 328

Assumed it was broken and was about to give up when I tried the VirtualWire Library http://www.open.com.au/mikem/arduino/ copied over the example code and got the things talking.  :D
My pleasure was somewhat diminished by my inability to understand the code or the helpfile.

This is the example sending code loop...
Code: [Select]
void loop()
{
const char *msg = "hello";
vw_send((uint8_t *)msg, strlen(msg));
delay(400);
}


Which from what I can piece together from forum and doc surfing sets the multi character constant called msg to have the string value hello and then uses the vw_send command from the library to send that text with a length of 5?
What does the (uint8_t *) part mean? Is it just standard for the send command? Multi-byte?

The receive loop code is...

Code: [Select]
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;
// Message with a good checksum received, dump HEX
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(" ");
}
Serial.println("");
}
}


uint8_t is new to me (as is most of this I'm afraid) Again, reading into it, it seems to be saying define a byte array with the length being the length of the string sent.
Then define another for the length itself.
Then checks to see if there is a message....
if there is, it runs through the array and displays it as a series of HEX characters.
(Actually it has helped to type this out so hopefully I have some of it correct!) :)

Would appreciate if someone could check my understanding and comment please.

To take it a step further, I wanted to replace the HELLO message with the output from a pot connected to A0 (+5V and GND)
I mapped the reading to between 0 and 255 ...
Code: [Select]
analogValue = analogRead(0);  
analogValue = map(analogValue, 0, 1023, 0, 255);

...but then got completely confused when trying to amend the code to send this data.
It did give me some readings but these tended to be 0, 8 a few other randoms or 255.  Also when the pot was turned all the way the output stopped completely so I guess that tx wasn't sending.

I'm using the default send and receive port for VirtualWire (12,11).
Should I have had a resistor connected to the pot?

Tried this as a send command...
Code: [Select]
vw_send((uint8_t *)analogValue, 1);
Where analogValue was the mapped int reading from the pot.

Then left most of the original code in the receive end but replaced the print line with.
Code: [Select]
Serial.println(buf[0], INT);

and took out the 'for' loop. Clearly wrong as it isn't giving me correct results.

Could someone assist please?

Many thanks in advance

2MuchNFO

Moved forwards slightly so thought it worth an update..

Code: [Select]
msg[0] = analogValue;

seems to put the INT value into char* msg for me (lying if I said I understood what was happening though. Stole it from some other code I found)

I can then vw_send this and pick it up at the far end.

If the value of analogValue is 0, nothing gets sent. Annoying but I can map to 1-255 in this case to prevent it as I don't need the accuracy.

Interesting I added the test code..

Code: [Select]
Serial.println(analogValue);
msg[0] = analogValue;

Serial.println(msg[0],DEC);

..to the sketch and analogValue counted from 0 - 255 and msg counted to 127 and then counted up from -128 to -1. Sure there is a good reason for this?

The number comes through correctly with the
Code: [Select]
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], DEC);
}

...receiving code however.

I guess I'm just getting in a knot with all the various data types  :-[

2MuchNFO

I have managed to get this working. I blame my confusion over char* as I'd never used it before.

Go Up