Using virtualwire.h problems using received data

Hi all hope someone can help me.
i have recently got these cheap RF set to make my arduino projects wireless.

My first project with these is a remote controlled boat that I have pulled the electronics out and replaced with arduino.
I have already created a sketch using a wii nunchuck and got it working. I now want to make it wireless.
I have successfully transmitted the values from the transmitter to the receiver and printed them using serial.print.
But when I then send the values to the two motors in the boat the I start having problems.
It will still print when the values are zero but as soon as they go higher it stopes printing and stopes receiving.
so I have narrowed it down to the analogWrite.

I have used the virtualwire library and have changed one of the examples to fit my project.

This is my transmit sketch with the nunchuck clk atatched to A5 and data A4.

eg without analogWrite,it works, with analogWrite, it freezes.

// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

//Adjusted by Toby Woodward boat project

#include <VirtualWire.h>

#include <Wire.h>
#include <WiiChuck.h> //#include "nunchuck_funcs.h"

WiiChuck chuck = WiiChuck();

const int led_pin = 13;
const int transmit_pin = 2;

int maxPWM = 60; // max pwm to motors

void setup()
{
    delay(1000);
    Serial.begin(115200);	// Debugging only
    Serial.println("setup");
    // Initialise the IO and ISR
    vw_set_tx_pin(transmit_pin);

    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);       // Bits per sec
    pinMode(led_pin, OUTPUT);
    
    //nunchuck
    chuck.begin();
    chuck.update();
    chuck.calibrateJoy();
    
}


void loop()
{
  chuck.update();
  
  int Mspeed,steerL,steerR,speedL,speedR;
  int y, x;
  
  //reads x, y nunchuck joystick values 
  y = (chuck.readJoyY());
  x = (chuck.readJoyX());
  
  
  //forwared- if joy pushed up
  if (y>0){
    Mspeed = map(y, 0, 125, 0,  maxPWM);
  }else{
    Mspeed = 0;
  }
  //right  -if joy pushed right
  if (x>1){
    steerR = map(x, 0, 123, 0,  maxPWM);
    steerL = 0;
  }else
  //left-if joy pushed left
  if(x<0){
    steerL = map(x, -130, 0, maxPWM, 0);
    steerR = 0;
  }else{// else go straight
    steerL = 0;
    steerR = 0;
  }
  
  // set speed for left motor and right motor with steer adjust
  speedL=Mspeed-steerL;
  speedR=Mspeed-steerR;
  
  //stopes values going negative
   if (speedL<0){
     speedL=0;
   }
   if (speedR<0){
     speedR=0;
   }
   
//   //prevents bad joystick calibration from suppling high volts to the motors
//   if (speedL > maxPWM){
//     speedL=maxPWM;
//   }
//   if (speedR > maxPWM){
//     speedR=maxPWM;
//   }
    
// create char array to send motor speeds as ascii goes from 0 to 255 
//and my values are limited to 60 not a problem.
  char msg[2] = {speedL,speedR};
  
  if (chuck.buttonZ == true) {
    digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
    
    //debuging only
    Serial.print("Left ");
    Serial.print(speedL);
    Serial.print(" Right ");
    Serial.println(speedR);
    
    //send char array
    vw_send((uint8_t *)msg, 2);
    
    vw_wait_tx(); // Wait until the whole message is gone
  }
  
  digitalWrite(led_pin, LOW);
  delay(20);
  
}

And my receive sketch

// receiver.pde
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

//Adjusted by Toby Woodward boat project

#include <VirtualWire.h>

const int led_pin = 13;

const int receive_pin = 3;

//motor pins go to mosfets
const int ML = 9;//left motor
const int MR = 10;//right motor

void setup()
{
    delay(1000);
    Serial.begin(115200);	// Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR

    vw_set_rx_pin(receive_pin);

    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec

    vw_rx_start();       // Start the receiver PLL running

    pinMode(led_pin, OUTPUT);
    
    //motor init
   pinMode(ML, OUTPUT);
   pinMode(MR, OUTPUT);
}

void loop()
{
  char tbs[30]; //holds serialprint sentance/line allows formating
  int speedL,speedR;
  
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;


  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    digitalWrite(led_pin, HIGH); // Flash a light to show received good message
    	// Message with a good checksum received, dump it.
    
    //convert char into int values and split	
    speedL = int(buf[0]);
    speedR = int(buf[1]);
    
    //print values 
    sprintf(tbs, "Left: %4d Right: %4d",speedL,speedR);
    Serial.println(tbs); 
    
  //this is what im having problems with      
//send motors their speeds
    //analogWrite(ML,speedL);
    //analogWrite(MR,speedR);
    
   digitalWrite(led_pin, LOW);
  }
  else{// stop motors if no transmition
    analogWrite(ML, 0);
    analogWrite(MR, 0);
 }
    
}

Would love some help if you see somthing else that could be more efficient or better please reply.
Thanks Toby :wink:

On the transmitter, why do you declare msg as char, when the sending function expects uint8_t? Why are you storing ints in the char array?

Nothing on the sending end needs to be int, that I can see. Using the appropriate types is far better than assuming that the compiler will fit variables in the specified type correctly.

The virtual wire library may be using a timer. If it does, it will interfere with PWM on some pins. Can you try other PWM pins on the receiver, for the motors?

Hi thanks for your reply.
It Works thanks.
Still grasping the ideas behind rf transmission.
You are right I had not understood the use of unit8_t cleaned my code got rid of char
I looked up uint8_t and now understand it a lot better.
Thanks for the tip about the timer I changed the PWM pin 9 and it fixed it thanks.
Here is my revised code...

transmitter

// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

//Adjusted by Toby Woodward boat project

#include <VirtualWire.h>

#include <Wire.h>
#include <WiiChuck.h> //#include "nunchuck_funcs.h"

WiiChuck chuck = WiiChuck();

const int led_pin = 13;
const int transmit_pin = 2;

int maxPWM = 60; // max pwm to motors

void setup()
{
    delay(1000);
    Serial.begin(115200);	// Debugging only
    Serial.println("setup");
    
// Initialise RF
    vw_set_tx_pin(transmit_pin);
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);       // Bits per sec
    pinMode(led_pin, OUTPUT);
    
    //Initialise  nunchuck
    chuck.begin();
    chuck.update();
    chuck.calibrateJoy();
}

void loop()
{
 int Mspeed,steerL,steerR;
  uint8_t speedL,speedR;
  int y, x;
  
 chuck.update();
 
 //reads x, y nunchuck joystick values 
  y = chuck.readJoyY();
  x = chuck.readJoyX();
  
  
  //forwared- if joy pushed up
  if (y>1){
    Mspeed = map(y, 0, 117, 0,  maxPWM); 
  
  }else{
    Mspeed = 0;
  }
  
//right  -if joy pushed right
      if (x>1){
        steerR = map(x, 0, 115, 0,  Mspeed); 
        steerL = 0;
      }else if(x<0){//left-if joy pushed left
        steerL = map(x, -140, 0, Mspeed, 0);
        steerR = 0;
      }else{
        steerL = 0;
        steerR = 0;
      }
   //stopes values going negative
   if (speedL<0){
     speedL=0;
   }
   if (speedR<0){
     speedR=0;
   }
   
  // set speed for left motor and right motor with steer adjust
  //and convert to uint8_t
  speedL = uint8_t(Mspeed-steerL);
  speedR = uint8_t(Mspeed-steerR);

    
//create array to send motor speeds 8 bit goes from 0 to 255 
//and my values are limited to 60 not a problem.
  uint8_t msg[2] = {speedL,speedR};
  
  if (chuck.buttonZ == true) {
    digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
    
    //debuging only
    Serial.print("speedL ");
    Serial.print(speedL);
    Serial.print(" speedR ");
    Serial.print(speedR);
    Serial.print(" x ");
    Serial.println(x);
    
    //send char array
    vw_send((uint8_t *)msg, 2);
    
    vw_wait_tx(); // Wait until the whole message is gone
  }
  
  digitalWrite(led_pin, LOW);
delay(5);
  
}

receiver

// receiver.pde
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

//Adjusted by Toby Woodward boat project

#include <VirtualWire.h>

const int led_pin = 13;

const int receive_pin = 3;

//motor pins go to mosfets
const int ML = 9;//left motor
const int MR = 10;//right motor

void setup()
{
    delay(1000);
    Serial.begin(115200);	// Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR

    vw_set_rx_pin(receive_pin);

    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec

    vw_rx_start();       // Start the receiver PLL running

    pinMode(led_pin, OUTPUT);
}

void loop()
{
  char tbs[30]; //holds serialprint sentance/line allows formating
  int speedL,speedR;
  
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  
  
  
  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    digitalWrite(led_pin, HIGH); // Flash a light to show received good message
     
    // Message with a good checksum received, dump it.
    speedL = buf[0];
    speedR = buf[1];
    
    //debuging only
    sprintf(tbs, "Left: %4d Right: %4d",speedL,speedR);
    Serial.println(tbs); 
    
    //this is what im having problems with      
    //send motors their speeds
    analogWrite(10,speedL);
    analogWrite(11,speedR);
    digitalWrite(led_pin, LOW);
  }

}

Thanks Toby