Changing PWM value stop my rf signals

Hi all

I will attach my code below, when i change my PWM value from 255 to anything below that it stops my rf from receiving a signal so i think anyway.

But if I have it on say 100pwm I get readings from my ultrasonic sensors but as soon as the ultrasonic sensors distance goes over 100cm it just freezes there ?

Oh and everything works fine if I leave the PWM values at 255.

Please I need help .

Thanks
M......

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
#define usTRIG1 3
#define usTRIG2 5
#define usECHO1 4
#define usECHO2 11
char data;
unsigned long t1,t2;//timers

int M1INA=6;
int M1INB=12;
int PWM1=10;//Motor 1
int M2INA=7;
int M2INB=8;
int PWM2=9;//Motor 2

void setup()
{
  Serial.begin(9600);
  pinMode(usTRIG1,OUTPUT);pinMode(usTRIG2,OUTPUT);
  digitalWrite(usTRIG1,LOW);digitalWrite(usTRIG2,LOW);
  pinMode(usECHO1,INPUT);pinMode(usECHO2,INPUT);
  vw_set_ptt_inverted(true);
  vw_setup(2000);
  vw_set_rx_pin(2);
  vw_rx_start();
  pinMode(13,OUTPUT);
  
  pinMode(M1INA,OUTPUT);
  pinMode(M1INB,OUTPUT);
  pinMode(PWM1,OUTPUT);
  pinMode(M2INA,OUTPUT);
  pinMode(M2INB,OUTPUT);
  pinMode(PWM2,OUTPUT);
 
}

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;
        digitalWrite(13, true); // Flash a light to show received good message
 for (i = 0; i < buflen; i++)
 {
       if (buf[i] == 'E'){
          digitalWrite(usTRIG1,LOW);delayMicroseconds(10);digitalWrite(usTRIG1,HIGH);delayMicroseconds(10);digitalWrite(usTRIG1,LOW);
          t1=pulseIn(usECHO1,HIGH);//return pulse length in uS
        }
       if (buf[i] == 'C'){
          digitalWrite(usTRIG2,LOW);delayMicroseconds(10);digitalWrite(usTRIG2,HIGH);delayMicroseconds(10);digitalWrite(usTRIG2,LOW);
          t2=pulseIn(usECHO2,HIGH);
          Serial.print(t1/29);Serial.print("\t");Serial.println(t2/29);//distance in cm, 29 not 58 because ultrasound is 1 way only, no reflection
        }
        }
     digitalWrite(13, false);
     }
     
    if(t1 > 3000 && t1 < 6000 && t2 > 3000 && t2 < 6000) //Go forward if distance is greater than 100cm.
  {
    digitalWrite(M1INA,LOW);
    digitalWrite(M1INB,HIGH);
    digitalWrite(M2INA,LOW);
    digitalWrite(M2INB,HIGH);
    analogWrite(PWM1,255);
    analogWrite(PWM2,255);
  }
  
   else if(t1 > 300 && t1 < 2999 )  //Stop both motors if too close.
  {
    analogWrite(PWM1,0);
    analogWrite(PWM2,0);
  }
  
 

    
    else 
 {
    analogWrite(PWM1,0);
    analogWrite(PWM2,0);
 }   
     
 

}

You set the rx pin, but the VirtualWire sets also the tx and ptt pin, even if you don't use them.
Could you set vw_set_tx_pin and vw_set_ptt_pin to unused pins ?

Perhaps there is noise from pwm signals (in the wires) into the receiver ?
Do you use usb power ? perhaps you can try an external power supply.

Im not really sure how RF works its my first time using them just copied and pasted some examples to get my stuff working.

Ok I put an external power source on the receiver .
Yes I am using usb to power my arduino and a 12v battery to power the motors.

Now even with the PWM at 255, the motors kick in but then the serial print just freezes and the motors keep turning.

Freezes ? The whole sketch freezes ?
Perhaps the motors have a large influence on the Arduino board. Perhaps bad wiring, missing ground wires.

The most common error is when a servo motor is powered by the Arduino 5V pin. That 5V is not strong enough for a servo motor. But you don't seem to have that. Could you make a photo of it, or tell which driver board you use, or how big the motors are ?

Yes well the serial print doesnt print out any new readings, when i close it and open it again it starts printing the readings when the motors kick in it freezes again or no new readings get printed

Ive attached a picture but you cant really tell where all the wiring goes but the grounds are connected to the arduino .

This is my motor drive im not using it as a shield because as a shield it uses to many pinshttp://www.pololu.com/product/2502

I have 2 12v motors they pull about 9 amps free running

So i have a 12v battery running the motors a 5v battery running the receiver now and my usb running the arduino.

Thanks

Virtualwire:
“Caution: VirtualWire takes over Arduino Timer1, and this will affect the PWM capabilities
of the digital pins 9 and 10.”

Your code:
int PWM1=10;//Motor 1
int PWM2=9;//Motor 2

So that could be a problem.

Hi Cross thanks for the advice will try it out shortly, but which PWM pins are available to use now?

Thanks.

Solved changed my PWM pins and everything works, such a small thing can cause so many problems haha.

Thanks to all for helping

You got it solved, but 3-5-6-9-10-11 are the PWM pins on 328 based boards.
So 3,5,6,11.
11,12 are the default pins for Virtualwire IO, but can be moved.