Servo control using arduino via rc receiver

Hi,

I tried to control servo using an arduino via rc receiver, well it is moving well just like this tutorial taught....
https://www.instructables.com/Control-Servos-with-Arduino-and-RC-receiver/

I tried to change it a little bit so that every time i move the gimbal towards same direction the servo goes +10 each time, but it only goes +10 once and doesnt respond thereafter...what can i change?


// ***------ RC Servo control -----***
// ***---- K. Michalsky / 2015 ----***


//Arduino Pins
// Pin7 to channel2 of receiver
// Pin9 to Signal of Servo1

//Include Servo library.The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. 
#include <Servo.h>

//Create servo objects to control the servo motors
Servo servoMotor1;

//Define variables for the position of the servos
int posServo1 = 0;

//Define a variable for the channel of the Receiver with which you want to control the servos, in this case channel2 (ailerons channel from my receiver)
int channel2;

//Attach each servo variable to a pin
void setup() { 
  servoMotor1.attach(9);   //Servo1 attached to pin 9

    //Define the input Pin for the Receiver
  pinMode(7, INPUT);
  Serial.begin(9600);     //Serial comunication for later monitoring of channel signal value
}

void loop() {
  //Read the pulse from Pin7 either HIGH or LOW
  channel2 = pulseIn(7, HIGH);

  //Monitor channel value
  Serial.print("Channel 2:");
  Serial.println(channel2);

  //Define a range of the channel value to center the servo when the stick of the transmiter is centered. This range can variate depending on which servos and Transmiter you are using. Servos are centered at 90 degrees
  if((channel2 >= 1450) && (channel2 <= 1550)){
    //posServo1 = 0;
    servoMotor1.write(posServo1);
  }
  //If the channel value is bigger than the highest value of the range do following
  else if(channel2>1550){
    for(int i = 0; posServo1 < 10; posServo1 ++){
      servoMotor1.write(posServo1);
      i = posServo1;
      delay(1);
              }        
          }
                  
                 
  delay(100);               //Time for Monitoring values
}

What do you expect there? (Mismatch in loop index and position testing)

I think you over-complicated things. If you want to move 10 degrees:

  if(channel2 > 1550)
  {
    posServo1 = constrain(posServo1 + 10, 0, 180);
    servoMotor1.write(posServo1);
    delay(10);
  }

[/quote]

Thank it works, i also tried it on AC servo PUL and DIR and it works, but i cannot make it go faster it rotates very slowly, what can i do?


// ***------ RC Servo control -----***
// ***---- K. Michalsky / 2015 ----***


//Arduino Pins
// Pin7 to channel2 of receiver
// Pin9 to Signal of Servo1

//Include Servo library.The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. 
#include <Servo.h>

#define dirPin 2
#define stepPin 3

//Create servo objects to control the servo motors
//Servo servoMotor1;

//Define variables for the position of the servos
//int posServo1 = 0;

//Define a variable for the channel of the Receiver with which you want to control the servos, in this case channel2 (ailerons channel from my receiver)
int channel2;

int mSpeed = 0;

//Attach each servo variable to a pin
void setup() { 

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // Set the spinning direction CW/CCW:
  digitalWrite(dirPin, HIGH);
  
  //servoMotor1.attach(9);   //Servo1 attached to pin 9

    //Define the input Pin for the Receiver
  pinMode(7, INPUT);
  Serial.begin(9600);     //Serial comunication for later monitoring of channel signal value
}

void loop() {
  //Read the pulse from Pin7 either HIGH or LOW
  channel2 = pulseIn(7, HIGH);

  //Monitor channel value
  Serial.print("Channel 2:");
  Serial.println(channel2);

  //Define a range of the channel value to center the servo when the stick of the transmiter is centered. This range can variate depending on which servos and Transmiter you are using. Servos are centered at 90 degrees
  //if((channel2 >= 1450) && (channel2 <= 1550)){
    //posServo1 = 0;
    //servoMotor1.write(posServo1);
  //}
  //If the channel value is bigger than the highest value of the range do following
  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
  }   
  if(channel2 < 1450){
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    
  }

        
          }                  
                 
   
  //Time for Monitoring values
[type or paste code here](https://forum.arduino.cc/t/ac-servo-rc-radio-speed-fix/976720)

now here pls

You are moving one step for each pulse from the radio (pulseIn()). There are usually about 50 pulses per second so 50 steps per second.

To go any faster you will have to send multiple step pulses for each pulse from the radio.

is it possible to multiply pulses using arduino code :slight_smile:

thanks, if i do this it works much faster but is there a way to make it a short cut

  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
  }

Yes. Function, or loop, or both.

Function:

void StepPulse()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
}


  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
    StepPulse();
  }

Or loop:

  if(channel2 > 1550){
    digitalWrite(dirPin, HIGH);
    for (int i=0; i< 8; i++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1);
    }
  }

Or both:

  if(channel2 > 1550) {
    digitalWrite(dirPin, HIGH);
    for (int i=0; i< 8; i++) {
      StepPulse();
    }
  }

o thank you :blush:

i tried to add channel3, when the left (other) gimbal is above 1550 it increases variable speed and vice versa, but i didnt get it right, what do you think?


// ***------ RC Servo control -----***
// ***---- K. Michalsky / 2015 ----***


//Arduino Pins
// Pin7 to channel2 of receiver
// Pin9 to Signal of Servo1

//Include Servo library.The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. 
//#include <Servo.h>

#define dirPin 2
#define stepPin 3

//Create servo objects to control the servo motors
//Servo servoMotor1;

//Define variables for the position of the servos
//int posServo1 = 0;

//Define a variable for the channel of the Receiver with which you want to control the servos, in this case channel2 (ailerons channel from my receiver)
int channel2;
int channel3;

int mSpeed = 0;


//Attach each servo variable to a pin

void StepPulse()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
}

void StepPulse2()
{
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1);
}

void setup() { 

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // Set the spinning direction CW/CCW:
  //digitalWrite(dirPin, HIGH);
  
  //servoMotor1.attach(9);   //Servo1 attached to pin 9

    //Define the input Pin for the Receiver
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  Serial.begin(9600);     //Serial comunication for later monitoring of channel signal value
  
}

void loop() {
  //Read the pulse from Pin7 either HIGH or LOW
  channel2 = pulseIn(7, HIGH);
  channel3 = pulseIn(8, HIGH);

  //Monitor channel value
  Serial.print("Channel 2:");
  Serial.println(channel2);

  //Define a range of the channel value to center the servo when the stick of the transmiter is centered. This range can variate depending on which servos and Transmiter you are using. Servos are centered at 90 degrees
  //if((channel2 >= 1450) && (channel2 <= 1550)){
    //posServo1 = 0;
    //servoMotor1.write(posServo1);
  //}
  //If the channel value is bigger than the highest value of the range do following
if(channel3 > 1550) {
      for (int s=50; s < ((mSpeed * 2) + 1); s++) {
      
  }
}

if(channel3 < 1450) {
      for (int s=50; s < ((mSpeed * 2) + 1); s--) {
      
  }
}

if(channel2 > 1550) {
    digitalWrite(dirPin, HIGH);
    for (int i=0; i < mSpeed; i++) {
      StepPulse();
  }
}

if(channel2 < 1450) {
    digitalWrite(dirPin, LOW);
    for (int i=0; i < mSpeed; i++) {
      StepPulse2();
  }
}
        
}    
              
                 
   
  //Time for Monitoring values

When you add another pulseIn() you add another 20 millisecond delay. Everything will go at half speed.

I recommend you find a library to read the RC inputs using interrupts instead of pulseIn().

i dont mind the extra delay, only problem is the variable speed for channel3 is not functioning.

You can't use the same 'stepPin' for two different stepper drivers. Are you trying to have two RC channels control the same stepper?!?

only using one motor, i fixed it stepPulse..... channel2 is the one that moves the motor which works great. channel3 changes variable speed, this part isnt working.

These empty 'for' loops don't change mSpeed at all.

yay this worked :slight_smile:

if(channel3 > 1550 && channel3 < 1700) {
        mSpeed = 10;
              
  }
  
if(channel3 > 1701 && channel3 < 1850) {
        mSpeed = 100;
        //delay(5);
  }

if(channel3 > 1850 && channel3 < 2000) {
        mSpeed = 300;
        //delay(5);
  }



if(channel2 > 1550) {
    digitalWrite(dirPin, HIGH);
    for (int i=0; i< mSpeed; i++) {
      StepPulse();
    }