Let's Make Robots!

Rf links sending and interpreting

I have made a controller and trying to start programming. This is my first time playing around with rf links. I have went through gareth's walkthrough and get messages to print on my screen, thats cool but what about sending other stuff. My question is must i always send messages or can i just send integers? Also i need a bit of help on how to get the receiving code to interpret the message or integer. 

Comment viewing options

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

Again, haven't tested, but for writing a value to say, an LED depending on if it receives an A or B first. (I forgot to do pinMode  but I heard analogWrite does this automatically)

 


#include <VirtualWire.h>
char myArray[32];// array needed to store data for string
byte myCounter; // counter for storing data


int myVal;  // value to store incoming data
int myVal2; // 2nd data to store


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;
    myCounter = 0; // need to reset the counter

    for (i = 0; i < buflen; i++)  // buflen = length of message received
    {
      //we need to add 1 to counter before filling up, so we don't include the A or B in the array:
      //so we add it as we read, rather than after:
      myArray[myCounter++] = buf[i];  // fill up the array with what you recieved
      //myCounter++;   // add 1 each loop
     
      if(buf[0] == 'A')  // buf[0] is skipped above, so read it
      {
        myVal = atoi(myArray);
        analogWrite(9, myVal);  //write first value
      }
      if(buf[0] == 'B')
      {
        myVal2 = atoi(myArray);
        analogWrite(10, myVal2);//write second value
      }
     
          
    }
   
   
  }
}
 

I think the easiest way is using sprintf on the transmitting side to send the values, then on the receiving side, you're going to need to use the atoi function.

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

 You need to make an array you can use to fill up with incoming data for a string. And then a counter to count for each time it stores a value.

I just threw this together, haven't tested it yet, too lazy to grab my RF kits. lol

#include <VirtualWire.h>
char myArray[32];// array needed to store data for string
byte myCounter; // counter for storing data
int myValue;  // value to store incoming data
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;
    myCounter = 0; // need to reset the counter

    for (i = 0; i < buflen; i++)  // buflen = length of message received
    {
      myArray[myCounter] = buf[i];  // fill up the array with what you recieved
      myCounter++;   // add 1 each loop
    }
   
   
    myValue = atoi(myArray);   // this should give you a value you can use
    //##DO SOMETHING WITH myValue## forgot that part :)


myServo.write(myValue);  //maybe? :)   
  }
}

Here's the transmitting example:

#include <VirtualWire.h>

char buffer[32];
long myVal;
void setup()
{

  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);     // Bits per sec

}

void loop()
{
    myVal = analogRead(0);  // reads analog port 0
    sprintf(buffer,"%u", myVal); // puts the value into buffer
    vw_send((uint8_t *)buffer, strlen(buffer)); // sends buffer
    vw_wait_tx(); //wait until done sending
    delay(500);   // half-second delay, can do faster

}


very good stuff. Works perfectly.

Check this  out and see if it works for you.

http://arduino.cc/en/Tutorial/SerialCallResponse