DC motor PWM variable speed

Hello,

I am working on a project to control a car driven by 2 DC motors with my hand. I attached 2 flex sensors on two fingers of a glove, and depending on each position ( There are four positions: Both unbent, left bent only, right bent only, and both bent) I will send the data to the car using a 315 MHz transmitter and receiver.

On the car, I have a motor shield (L298N) connected to the arduino along with the receiver. Depending on which data is received , the car can go in four directions ( Forward, Right, Left and backwards). Now to make things straight, the receiver is working just fine, my problem is to change the speed of the motors. If I set the speed of both motors to be either 0 or the max of 255, everything works perfect. The problem is that I want to decrease the speed of the left motor when turning right and the right motor when turning left. But whenever I try to do that, the program just stops and the motors lock up on the last value they read.

I double checked all the connections, the PWM pins needed are for enabling the 2 motors ( I am using pins 9 and 10 of the arduino). One more note, I changed the speed of the motors alone, separate from the project and they worked fine. So in short, there seems some problem going on when trying to change the speed inside the receive program.
I have attached a picture of the motor shield and the code for the transmitter and receiver.

Any help!
Thanks in advance

transmit.ino (673 Bytes)

receive.ino (1.25 KB)

motor_shield.jpg

Probably interference from the motors / drivers is either blocking the RF signals or
putting too much noise on the power rails.

How is everything powered? Motor supply independent of Arduino / receiver? (it should be)
Motors well away from the receiver? Motor has suppression capacitor across its contacts?

Yes MarkT,

The motor supply is independent from arduino. A 12V battery( 8x1.5V) is connected to the motor shield which drives the motors. Also the motor shield has a 5V output pin which I use to power the arduino and receiver.

The receiver is actually right on the car so its not well away from the motors, but I don't know if the problem is from interference from motors because I can see from the Serial Monitor that the receiver is acquiring the signals perfectly. Also when I don't set the speed below 255 in the receive program, everything works fine. My problem is just using PWM to vary the speed of the motors.

Have you tried just experimenting with PWM by itself? Narrow the issue down as much as you
can.

Yes I tried and it worked well. I am not able to find any reason why the PWM would work in a normal program and not in this one. I used the same code for both except that in the receive program it is inside an if statement. Any ideas?

Supply rail noise then? PWM is going to switch motor currents at a high rate, much noisier than
just having the motors on full.