Hobby Servo control via RC Receiver (with a twist)

Hello guys,

I would like to build a code that does the following on Arduino Pro Mini:

  1. gets PWM input from standard RC Receiver.
  2. reads analog voltage from A0.
  3. commands a standard RC servo on D9.

The servo commands should be as follows:

  • if A0 voltage > 50% (2.5V) then servo PWM == Rx read PWM.
  • if Ao voltage <= 50% (2.5V) then servo PWM == const, lets say 10 degrees as an example.

I have followed the Arduino site example with a small change and can run the servo from a pot, and when the pot is at less than 50%, lock the servo in a predefined position, using this simple code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
if (val<90)
myservo.write(50); // sets the servo position according to the scaled value
else
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

my question is how do i proceed from here in order to read PWM value from my RC receiver, and store it in a variable.
i believe that once i know that, i will be able to figure out everything else.

Can anyone offer assistance please?
thanks for your help.

I think i did it :slight_smile:

Here is my code... any comments?

#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int ch1; // Here's where we'll keep our channel values

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(8, INPUT); // Set our input pins as such
Serial.begin(9600); // Pour a bowl of Serial

}

void loop() {

ch1 = pulseIn(8, HIGH, 25000); // Read the pulse width of each channel
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
if (val<90)
myservo.write(50); // sets the servo position according to the scaled value
else
myservo.write(ch1); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there

//myservo.write(ch1);

}

i3dm:
I think i did it :slight_smile:

Here is my code... any comments?

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R