Jitter in radio signal when there is "motor.attach(2)" in the code

Hi all,

I have got a radio transmitter and receiver and I want to control a motor with it. I can read the radio signal perfectly and I can also control the motor with a potentiometer. I use an Arduino MEGA 2560. The motors are actually brushless motors with ESC (Electronic Speed Controller)

The problem is if I put the line

motor.attach(2);

There is automatically jitter in the radio signal. The overall code is (it reads the radio signal and maps it to [0 - 100])

#include <Servo.h>

#define MAX_1 2669
#define MIN_1 1257

#define MAP_RADIO_HIGH 100
#define MAP_RADIO_LOW 0



unsigned long ch1=0;  
Servo motor;


void setup()
{
motor.attach(2);

pinMode (10, INPUT);

Serial.begin(9600);
Serial.println("Done with setup");
}



void loop()
{
// get the values  
ch1  = pulseIn (10, HIGH, 100000); 
ch1 = MAP_RADIO_HIGH - map(ch1, MIN_1, MAX_1, MAP_RADIO_LOW, MAP_RADIO_HIGH);

//print the values
Serial.print ("ch1 ");
Serial.println (ch1);

delay(20);
}

If I comment the line

motor.attach(2);

there will be no jitter. And the battery for the motors is not even plugged so it's not an interference with power problem.

Any advice would be greatly appreciated, :wink:
Thanks!

If by jitter you mean pluseIn() reporting odd values then yes with servo running that may happen as servo is interrupt and timer driven.

Mark

If by jitter you mean pluseIn() reporting odd values then yes with servo running that may happen as servo is interrupt and timer driven.

Yes this is what I mean. Is there any way to deal with this problem?

One solution is to use interrupts and therefore the pulseIn() is not needed.

Exemple with lots of documentation here => RCArduino: How To Read Multiple RC Channels

If you still need help - I've written a library for RC-Receiving which works quite well (I'm steering a quadcopter with it).
If you're still interested, write me a pm or so.

You don't say which transmitter/receiver you are using. ESCs put out loads of RF
interference, if you have a cheap receiver then you'll pick up lots of noise.

You need a correctly designed antenna, as far from the ESCs and motors as poss,
and a selective receiver with a decent front-end.

Hi,

The problem was resolved some time ago:

One solution is to use interrupts and therefore the pulseIn() is not needed.
Exemple with lots of documentation here => RCArduino: How To Read Multiple RC Channels

Works flawlessly. My transmitter/receiver is a Turnigy 6X FHSS 2.4ghz (30$ on hobbyking).