PWM - control of code

Hi,

I have a quick (and proberly stupid) question. I have written some code to read the PWM signal from an rc receiver. See below. As expected the data ranges are from 900 to 2000, or 0.9ms to 2.0ms. I would like to run a loop in the arduino (which controls 3 servos from the IMU 6DOF) if, or when the input value changes by say +- 50 (using the range 900-2000). Is this possible?? I have looked on this great site, but have got confused and bogged down by masses of code and info.

Or maybe it would be possible to just run the code I have previously written when a channel from the  transmitter is on i.e. landing gear switch. This would therefore produce an output of either 900 or 2000. 0.9/2ms pwm, and would be a simple switch?

Thanks

Robert

 

int pin = 8;  // set this to the pin connected to your receiver ch1
unsigned long duration;
int pin2 = 9;  // set this to the pin connected to your receiver ch2
unsigned long duration2;
int pin3 = 10;  // set this to the pin connected to your receiver ch3
unsigned long duration3;

void setup()
{
  Serial.begin(9600);
  pinMode(pin, INPUT);
    Serial.begin(9600);
  pinMode(pin2, INPUT);
   Serial.begin(9600);
  pinMode(pin3, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
 
  Serial.println(duration);
  delay(100);
 
  duration2 = pulseIn(pin2, HIGH);
  Serial.println(duration2);
  delay(100);
 
  duration3 = pulseIn(pin3, HIGH);
  Serial.println(duration3);
  delay(100);
}

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Developer
    Hi Robert,

    Sure it is possible.

    RC servos need a continuous PWM pulse string to hold a position. You cannot just send a single pulse.... You might look a little deeper into the ArduPilot code to see how we do this using the timers and interrupts. Or, I believe there is an Arduino RC library, if you want to go the simple route, although I don't know how well it performs.
This reply was deleted.

Activity

Neville Rodrigues liked Neville Rodrigues's profile
Jun 30
Santiago Perez liked Santiago Perez's profile
Jun 21
More…