loading

help with arduino code?

I am making a project for my 6 DOF arm and I want to control the arm with a remote. I found a code for just that, but with only one servo and I am hoping somebody can help me out with changing the code to control 6 servos. please help me!!!
the code is shown below.


#include <IRLib.h>
#include <Servo.h> 
// You will have to set these values depending on the protocol
// and remote codes that you are using. These are from my Sony DVD/VCR
#define MY_PROTOCOL NEC
#define RIGHT_ARROW   0xffc23d //Move several clockwise
#define LEFT_ARROW    0xff22dd //Move servo counterclockwise
#define OK_BUTTON     0xff02fd //Center the servo
#define UP_ARROW      0xff629d //Increased number of degrees servo moves
#define DOWN_ARROW    0xffa857 //Decrease number of degrees servo moves
#define BUTTON_0      0xff4ab5  //Pushing buttons 0-9 moves to fix positions
#define BUTTON_1      0xff6897  // each 20 degrees greater
#define BUTTON_2      0xff9867
#define BUTTON_3      0xffb04f
#define BUTTON_4      0xff30cf
#define BUTTON_5      0xff18e7
#define BUTTON_6      0xff7a85
#define BUTTON_7      0xff10ef
#define BUTTON_8      0xff38c7
#define BUTTON_9      0xff5aa5
 
IRrecv My_Receiver(11);//Receive on pin 11
IRdecode My_Decoder; 
Servo My_Servo;  // create servo object to control a servo 
int pos;         // variable to store the servo position 
int Speed;       // Number of degrees to move each time a left/right button is pressed

void setup() 
{

  My_Servo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pos = 90;            // start at midpoint 90 degrees
  Speed = 3;           //servo moves 3 degrees each time left/right is pushed
  My_Servo.write(pos); // Set initial position
  My_Receiver.enableIRIn(); // Start the receiver

 
void loop() 

    if (My_Receiver.GetResults(&My_Decoder)) {
       My_Decoder.decode();
       if(My_Decoder.decode_type==MY_PROTOCOL) {
          switch(My_Decoder.value) {
            case LEFT_ARROW:    pos=min(180,pos+Speed); break;
            case RIGHT_ARROW:   pos=max(0,pos-Speed); break;
            case OK_BUTTON: pos=90; break;
            case UP_ARROW:      Speed=min(10, Speed+1); break;
            case DOWN_ARROW:    Speed=max(1, Speed-1); break;
            case BUTTON_0:      pos=0*20; break;
            case BUTTON_1:      pos=1*20; break;
            case BUTTON_2:      pos=2*20; break;
            case BUTTON_3:      pos=3*20; break;
            case BUTTON_4:      pos=4*20; break;
            case BUTTON_5:      pos=5*20; break;
            case BUTTON_6:      pos=6*20; break;
            case BUTTON_7:      pos=7*20; break;
            case BUTTON_8:      pos=8*20; break;
            case BUTTON_9:      pos=9*20; break;
          }
        My_Servo.write(pos); // tell servo to go to position in variable 'pos'
       }
     My_Receiver.resume();
    }
}

sort by: active | newest | oldest

Create more servo objects.

Where you have

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

Do

Servo My_other_servo;

Servo My_other_other_servo;

void setup()
{

My_Servo.attach(9); // attaches the servo on pin 9 to the servo object
My_other_servo.attach(pin number whatever)

My_other_other_servo.attach(pin number whatever again)

Don't forget, you will need a lot of power to run 6 servos successfully.

ibenkos (author)  steveastrouk1 year ago

I understand how to do it in the void setup but the void loop is complicated. can you help me there?

ibenkos (author) 1 year ago

I finally got it working, I just tested some different stuff and found out what I had to do, and the code works!

#include <IRLib.h>

#include <Servo.h>

// You will have to set these values depending on the protocol

// and remote codes that you are using. These are from my Sony DVD/VCR

#define MY_PROTOCOL NEC

#define RIGHT_ARROW 0xffc23d //Move several clockwise

#define LEFT_ARROW 0xff22dd //Move servo counterclockwise

#define OK_BUTTON 0xff02fd //Center the servo

#define UP_ARROW 0xff629d //Increased number of degrees servo moves

#define DOWN_ARROW 0xffa857 //Decrease number of degrees servo moves

#define BUTTON_0 0xff4ab5 //Pushing buttons 0-9 moves to fix positions

#define BUTTON_1 0xff6897 // each 20 degrees greater

#define BUTTON_2 0xff9867

#define BUTTON_3 0xffb04f

#define BUTTON_4 0xff30cf

#define BUTTON_5 0xff18e7

#define BUTTON_6 0xff7a85

#define BUTTON_7 0xff10ef

#define BUTTON_8 0xff38c7

#define BUTTON_9 0xff5aa5

IRrecv My_Receiver(11);//Receive on pin 11

IRdecode My_Decoder;

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

int pos1; // variable to store the servo position

int Speed1; // Number of degrees to move each time a left/right button is pressed

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

int pos2; // variable to store the servo position

int Speed2; // Number of degrees to move each time a left/right button is pressedServo My_Servo; // create servo object to control a servo

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

int pos3; // variable to store the servo position

int Speed3; // Number of degrees to move each time a left/right button is pressedServo My_Servo; // create servo object to control a servo

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

int pos4; // variable to store the servo position

int Speed4; // Number of degrees to move each time a left/right button is pressedServo My_Servo; // create servo object to control a servo

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

int pos5; // variable to store the servo position

int Speed5; // Number of degrees to move each time a left/right button is pressedServo My_Servo; // create servo object to control a servo

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

int pos6; // variable to store the servo position

int Speed6; // Number of degrees to move each time a left/right button is pressed

void setup()

{

My_Servo1.attach(3); // attaches the servo on pin 9 to the servo object

pos1 = 90; // start at midpoint 90 degrees

Speed1 = 10; //servo moves 3 degrees each time left/right is pushed

My_Servo1.write(pos1); // Set initial position

My_Receiver.enableIRIn(); // Start the receiver

My_Servo2.attach(4); // attaches the servo on pin 9 to the servo object

pos2 = 90; // start at midpoint 90 degrees

Speed2 = 10; //servo moves 3 degrees each time left/right is pushed

My_Servo2.write(pos2); // Set initial position

My_Receiver.enableIRIn(); // Start the receiver

My_Servo3.attach(5); // attaches the servo on pin 9 to the servo object

pos3 = 90; // start at midpoint 90 degrees

Speed3 = 5; //servo moves 3 degrees each time left/right is pushed

My_Servo3.write(pos3); // Set initial position

My_Receiver.enableIRIn(); // Start the receiver

My_Servo4.attach(6); // attaches the servo on pin 9 to the servo object

pos4 = 90; // start at midpoint 90 degrees

Speed4 = 3; //servo moves 3 degrees each time left/right is pushed

My_Servo4.write(pos4); // Set initial position

My_Receiver.enableIRIn(); // Start the receiver

My_Servo5.attach(9); // attaches the servo on pin 9 to the servo object

pos5 = 90; // start at midpoint 90 degrees

Speed5 = 3; //servo moves 3 degrees each time left/right is pushed

My_Servo5.write(pos5); // Set initial position

My_Receiver.enableIRIn(); // Start the receiver

My_Servo6.attach(10); // attaches the servo on pin 9 to the servo object

pos6 = 90; // start at midpoint 90 degrees

Speed6 = 3; //servo moves 3 degrees each time left/right is pushed

My_Servo6.write(pos6); // Set initial position

My_Receiver.enableIRIn(); // Start the receiver

}

void loop()

{

if (My_Receiver.GetResults(&My_Decoder)) {

My_Decoder.decode();

if(My_Decoder.decode_type==MY_PROTOCOL) {

switch(My_Decoder.value) {

case LEFT_ARROW: pos1=min(180,pos1+Speed1); break;

case RIGHT_ARROW: pos1=max(0,pos1-Speed1); break;

case BUTTON_0: pos2=min(180,pos2+Speed2); break;

case BUTTON_1: pos2=max(0,pos2-Speed2); break;

case BUTTON_2: pos3=min(180,pos3+Speed3); break;

case BUTTON_3: pos3=max(0,pos3-Speed3); break;

case BUTTON_4: pos4=min(180,pos4+Speed4); break;

case BUTTON_5: pos4=max(0,pos4-Speed4); break;

case BUTTON_6: pos5=min(180,pos5+Speed5); break;

case BUTTON_7: pos5=max(0,pos5-Speed5); break;

case BUTTON_8: pos6=min(180,pos6+Speed6); break;

case BUTTON_9: pos6=max(0,pos6-Speed6); break;

}

My_Servo1.write(pos1); // tell servo to go to position in variable 'pos'

My_Servo2.write(pos2); // tell servo to go to position in variable 'pos'

My_Servo3.write(pos3); // tell servo to go to position in variable 'pos'

My_Servo4.write(pos4); // tell servo to go to position in variable 'pos'

My_Servo5.write(pos5); // tell servo to go to position in variable 'pos'

My_Servo6.write(pos6); // tell servo to go to position in variable 'pos'

}

My_Receiver.resume();

}

}

Nice job. Well done !!

Kiteman1 year ago

Try asking this in the "Tech" forum, it gets more traffic.

ibenkos (author)  Kiteman1 year ago
Isnt that what I did? I added it in technology as Arduino

This is the Q&A section, which gets less traffic than the main forums.

Tech is here:

https://www.instructables.com/community?categoryGro...

(Hit the New Topic button near the top-right of the page.)

More traffic= more noise.