IR Remote control multiple servo

Hi all experts, I have a board Mega 2560 and I need to control 4 3-wire hobby servos.
1 button on the remote control - servo 1 to one side, button 2 - 1 servo back, button 3 - 2 servos to one side, press 4 - 2 servos back.

There are codes from an IR remote - I would use the button 1, 2 and up and down arrows for my servo control.

Up Arrow FF629D
Left Arrow FF22DD
OK FF02FD
Right Arrow FFC23D
Down Arrow FFA857
1 FF6897
2 FF9867
3 FFB04F
4 FF30CF
5 FF18E7
6 FF7A85
7 FF10EF
8 FF38C7
9 FF5AA5

  • FF42BD
    0 FF4AB5

FF52AD

I can not find anywhere how to program multiple servos, always works for me only one servo - can you advise someone more experienced, I will be very happy.
I thank everyone beforehand for any help.

Just make multiple Servo objects... Tip, place them in an array

Servo servos[4];

Rest isn't that hard but because you didn't post any code this is all for now :wink:

I understand that I have the code for one servo, which works, more to me many can not add.
I am a beginner :slight_smile:

#include <IRremote.h>      //must copy IRremote library to arduino libraries
#include <Servo.h>
#define plus 0xFF6897   //clockwise rotation button
#define minus 0xFF9867  //counter clockwise rotation button

int RECV_PIN = 19;       //IR receiver pin
Servo servo;
int val;                //rotation angle
bool cwRotation, ccwRotation;  //the states of rotation

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  servo.attach(9);     //servo pin
}

void loop() 
{
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value

    if (results.value == plus)
    {
      cwRotation = !cwRotation;      //toggle the rotation value
      ccwRotation = false;         //no rotation in this direction
    }

    if (results.value == minus)
    {
      ccwRotation = !ccwRotation;   //toggle the rotation value
      cwRotation = false;            //no rotation in this direction
    }
  }
  if (cwRotation && (val != 180))  {
    val++;                         //for colockwise button
  }
  if (ccwRotation && (val != 0))  {
    val--;                         //for counter colockwise button
  }
  servo.write(val);
  delay(5);          //General speed

I see you also skipped the How to use this form thread :wink: Please edit your post and add code tags.

And I see you want to fade frome one side to the other. That makes everything a bit harder indeed. Let’s start by removing the delay (and never use it again) :wink: See, Blink without delay.

And btw, having a ccw and a cw variable is a but weird, especcilly because you set both different. Because now you can have that cw and ccw are both true at the same time, then what? I would say a cw (or ccw) variable and a run variable make more sens.

But like I said, arrays! Make an array of all the variables and objects you now have for one servo and make an array for them :slight_smile: Now you can loop over all the servo’s :wink:

for(byte i = 0; i < sizeof(servos)/sizeof(servos[0]); i++){
  //do the stuff for each servo
  servos[i].write(angle[i]);
}

Or making it a bit more readable by adding this macro on top

#define elements(x) sizeof(x)/sizeof(x[0])
for(byte i = 0; i < elements(servos); i++){
  //do the stuff for each servo
  servos[i].write(angle[i]);
}

Somehow do not understand, sorry -

I:\Dokumenty\Arduino\sketch_sep07b\sketch_sep07b.ino: In function 'void loop()':

sketch_sep07b:27: error: 'servos' was not declared in this scope

   for(byte i = 0xFF6897; i < elements(servos); i++){

                                       ^

I:\Dokumenty\Arduino\sketch_sep07b\sketch_sep07b.ino:3:28: note: in definition of macro 'elements'

 #define elements(x) sizeof(x)/sizeof(x[0])

                            ^

sketch_sep07b:29: error: 'angle' was not declared in this scope

   servos[1].write(angle[0]);

                   ^

sketch_sep07b:31: error: 'servos' was not declared in this scope

     for(byte i = 0xFF9867; i < elements(servos); i++){

                                         ^

I:\Dokumenty\Arduino\sketch_sep07b\sketch_sep07b.ino:3:28: note: in definition of macro 'elements'

 #define elements(x) sizeof(x)/sizeof(x[0])

                            ^

sketch_sep07b:33: error: 'angle' was not declared in this scope

   servos[1].write(angle[90]);

                   ^

sketch_sep07b:33: error: expected '}' at end of input

   servos[1].write(angle[90]);

                             ^

sketch_sep07b:33: error: expected '}' at end of input
#include <IRremote.h>      //must copy IRremote library to arduino libraries
#include <Servo.h> //must copy IRremote library to arduino libraries
#define elements(x) sizeof(x)/sizeof(x[0])

int RECV_PIN = 19;       //IR receiver pin
 
Servo servo1;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 


IRrecv irrecv(RECV_PIN);

decode_results results;
 
void setup() 
{ 
Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  servo1.attach(9);     //servo1 pin
} 
 
 
void loop() 
{ 
  for(byte i = 0xFF6897; i < elements(servos); i++){
  //do the stuff for each servo
  servos[1].write(angle[0]);
}        
    for(byte i = 0xFF9867; i < elements(servos); i++){
  //do the stuff for each servo
  servos[1].write(angle[90]);

Of course you have to then create multiple servo's!

So not

Servo servo1;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

But

Servo servos[4]; //let's make 4 servo objects!

But now you also want 4 position variables.

Thanks for your help, but I still did not work, jump up errors - can you tell me a little more advice?

So I tried a different programming, meantime on two engines, but sometimes I error - advice please.

thank you very much.

#include <IRremote.h>      //must copy IRremote library to arduino libraries
#include <Servo.h>

//constants
int RECV_PIN = 19;       //IR receiver pin
int PAS= 10;
const long MOTOR_1_KEY_LEFT = 0xFF6897;
const long MOTOR_1_KEY_RIGHT = 0xFF9867;
const long MOTOR_2_KEY_LEFT = 0xFF30CF;
const long MOTOR_2_KEY_RIGHT = 0xFF18E7;

Servo myservo_1;    // Servo 1
Servo myservo_2;    // Servo 2
IRrecv irrecv(RECV_PIN);
decode_results results;

int speed_motor_1 = 90; // initialize on stop
int speed_motor_2 = 90; // initialize on stop

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  myservo_1.attach(9);     //servo pin 9
  myservo_2.attach(10);    //servo pin 10
}

void loop() 
{
 if (irrecv.decode(&results)) {
  
    Serial.println(results.value);
    
    if(results.value == MOTOR_1_KEY_LEFT && speed_motor_1 >= PAS)
    {
     speed_motor_1=speed_motor_1-PAS;
     myservo_1.write(speed_motor_1);
   )
  if(results.value == MOTOR_1_KEY_RIGHT && speed_motor_1 <= 180-PAS)
 {
     speed_motor_1=speed_motor_1+PAS;
     myservo_1.write(speed_motor_1);
  )
   if(results.value == MOTOR_2_KEY_LEFT && speed_motor_2 >= PAS)
  {
     speed_motor_2=speed_motor_2-PAS;
     myservo_2.write(speed_motor_2);
  )
  if(results.value == MOTOR_2_KEY_RIGHT && speed_motor_2 <= 180-PAS)
   {
     speed_motor_2=speed_motor_2+PAS;
     myservo_2.write(speed_motor_2);
   )
   irrecv.resume(); // receive the next value
   }
   }