Hi rodmac,
I've used the VirtualWire library to replace a broken transmitter/receiver in a RC car which has two motors, one to control forwards/backwards, and the other to control the steering (left/right).
You should only call vw_set_tx_pin(uint8_t pin) if you want to use a different pin for transmitting, the library uses pin 12 by default. This means that 'pin' (normally 12) will output data from the arduino to the Sparkfun transmitter.
You should use vw_send(uint8_t* buf, uint8_t len) to actually send the data to the transmitter.
Here's part of the code I use in the remote:
#define pinLED 13
void setup() {
vw_setup(2000); // Bits per sec
}
void loop() {
sendCommand('L', 12); //Go left and set motor speed to 12
delay(100);
sendCommand('F',132); //Go forward and set motor speed to 132
delay(100);
sendCommand('S', 0); //Stop!
delay(400);
}
void sendCommand(char command[], byte value) {
char buffer[5]; //create buffer to hold data to be transmitted
//create buffer to hold value converted to a string e.g '123' - max value
//is 255 ('cos val is of type byte).
char num[4];
memset(buffer, '\0', sizeof(buffer));
memset(num, '\0', sizeof(num));
//My command var is a single letter e.g. 'S' stop, 'R' right etc
buffer[0] = command[0];
itoa(value, num, 10); //convert value to a string
//concatenate num to the buffer, result would be 'S0', 'F132', 'L12'
strcat(buffer, num);
digitalWrite(pinLED, true); // Flash a light to show transmitting
vw_send((uint8_t *)buffer, strlen(buffer)); //Send the data
vw_wait_tx(); // Wait until the whole message is gone
}
Then in the arduino actually in the car (which uses the Sparkfun receiver)
void setup(){
Serial.begin(9600);
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
//Receiver is connected to 11 (default pin in VirtualWire library)
delay(5);
}
void loop() {
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
//see if we have received a message from our TX
if (vw_get_message(buf, &buflen)) // Non-blocking
{
//invalid message length must be S/B/F/L/R + number (max 3 digits)
if (buflen < 3 || buflen > 5)
return;
char val[buflen]; //Same as buf, last char will be used for null terminator
memset(val, '\0', sizeof(val));
//Copy value from string i.e. 213 from R213 into separate string
strncpy(val, (char *)buf + 1, buflen - 1);
//convert string containing value to integer e.g. "213" to 213.
int VAL = atoi ( val );
switch (buf[0]) {
case 'B':
Serial.print("spin motor backwards at speed:\t")
Serial.println(VAL);
break;
case 'F':
Serial.print("spin motor forwards at speed:\t")
Serial.println(VAL);
break;
case 'L':
Serial.print("Steer car left at speed:\t")
Serial.println(VAL);
break;
case 'R':
Serial.print("Steer car right at speed:\t")
Serial.println(VAL);
break;
case 'S':
Serial.println("STOP THE CAR")
break;
default: //do nothing
break;
}
}
}
HTH