-1

am having trouble understanding what i'm getting in an Arduino setup.

Scenario is: I'm sending the character A (could be any character) from the TX board to the RX board using a chipset running with virtualwire. The RX is receiving the message and the buffer length is 1. When I print out the buf[i] on the serial monitor, I get 255 (which is the 8 bit maximum integer I assume) instead of the character A. Here is the relevant code:

TX file [code] void setup() {

c = 'A'; //for testing only

// Wireless code...Data pin on TX connected to port 7...vw stands for 'virtualwire'

vw_setup(2000); // Initialize tx transmission rate vw_set_tx_pin(7); // Declare arduino pin for data to transmitter ...

void loop() {

... vw_send((uint8_t *)c, 1); // Turn buzzer on...Transmit the character

RX file

// RX data pin is 8

void loop() {

Serial.println("Looping");
delay(2000);

uint8_t buflen = VW_MAX_MESSAGE_LEN;// Defines maximum length of message
uint8_t buf[buflen]; // Create array to hold the data; defines buffer that holds the message

if(vw_have_message() == 1) // Satement added by virtualwire library recommendation as one of three statements to use before the get_message statement below// not in original HumanHardDrive code
{
  Serial.println("Message has been received");
}

if(vw_get_message(buf, &buflen)) // &buflen is the actual length of the received message; the message is placed in the buffer 'buf'

{
  Serial.print("buflen from &buflen = ");
  Serial.println(buflen); // Print out the buffer length derived from the &buflen above

  for(int i = 0;i < buflen;i++)
  {
    Serial.print("i = ");
    Serial.println(i);             <--prints 0
    Serial.print(" buf[0] = ");    
    Serial.print(buf[0]);          <--prints 255  
    Serial.print("   buf[i] = ");
    Serial.println(buf[i]);        <--prints 255


    if(buf[i] == 'A')  <-- Does not recognize A since buf[i] comes out as 255

[/code]

Thanks for any suggestions!

Jaime
  • 1
  • 1

1 Answers1

0

The problem is probably this:

vw_send((uint8_t *)c, 1);

c is not a pointer, you are passing the value of 'A' as a pointer location which is then read by vw_send. You'll need the address-of operator:

vw_send((uint8_t *) &c, 1);


There is also a redundant if: if(vw_get_message(buf, &buflen)) is not nested inside if(vw_have_message() == 1) so it runs regardless of the fact vw_have_message() may not always return 1.

Chris A
  • 1,475
  • 3
  • 18
  • 22