how to control 20 RC servo motors using Arduino mega

edited May 2017 in Arduino

HI Guys,

1.I am working on a project where i need to run 20 RC servo motors in a set of 4 consist of 5 motors each.

2.The logic here is each set has to run by specific angle based on pc system time(H1H2:M1M2). ranges for H1[0-2], H2 [0-9], M1[0-5],M2[0-9].say the time is 23:47, for the value of H1(2) one set of motor will run by 70 degrees and for H2(3) second set of motor will run by 35 degree.similarly for M1(4) third set and M2(7) forth set.

3.my challenge here is to control 20 servos.Am using PWM pins on arduino mega by uploading servo lib. my mega has only 12 PWM pins[2-13]. [44,45,46] are not working as PWM but working as digital. Thus i can control only 12.

Eg. arduino.analogWrite([pin 2-13], angle);

4.i have also tried arduino.pinMode(pin[22-53], Arduino.SERVO); arduino.servoWrite(pin[22-53],90); but it does pin%16 and runs the reminder pin no. if it exists.

Eg. arduino.pinMode(pin[39], Arduino.SERVO); //39%16=7(runs PWM pin 7) arduino.pinMode(pin[44], Arduino.SERVO); //44%16=12(runs PWM pin 12)

5.Please note in the below code i have marked pin no 14-17 for representations purpose only to display H1. These pins do not work for servos.

6.please suggest any solution as how to control the rest of the servo motors. I want to keep the code as simple as possible.

below is the processing code:

 

import processing.serial.*;
import cc.arduino.*;
 
Arduino arduino;
int h;
int m;
int s;
int h1;
int h2;
int m1;
int m2;
int s1;
int s2; 
int aftr_m1=99;
int aftr_m2=99;
int aftr_h1=99;
int aftr_h2=99;
int pin=34;
void setup()
{

  // Prints out the available serial ports.
  println(Arduino.list());
  
  
  arduino = new Arduino(this, Arduino.list()[2], 57600);

  
}
void disp_hour(int v_h1,int v_h2)
{

 if (aftr_h1!=v_h1)
 {
   //tens position of hour(H1) arduino pin[14-17]
  if(v_h1==0){  
        arduino.analogWrite(14, 10);arduino.analogWrite(15, 90);
        arduino.analogWrite(16, 90);arduino.analogWrite(17, 90);
         }

  if(v_h1==1){ 
        arduino.analogWrite(14, 90);arduino.analogWrite(15, 10);
        arduino.analogWrite(16, 10);arduino.analogWrite(17, 10); 
         }
  
  if(v_h1==2){ 
        arduino.analogWrite(14, 90);arduino.analogWrite(15, 10);
        arduino.analogWrite(16, 10);arduino.analogWrite(17, 90); 
         }
  
  aftr_h1=v_h1;
 }

 if (aftr_h2!=v_h2)
 {
   //once position of hour(H2) arduino pin[10-13]
  if(v_h2==0){ 
        arduino.analogWrite(10, 10);arduino.analogWrite(11, 90);
        arduino.analogWrite(12, 90);arduino.analogWrite(13, 90); 
         }
  
  if(v_h2==1){ 
        arduino.analogWrite(10, 90);arduino.analogWrite(11, 10);
        arduino.analogWrite(12, 10);arduino.analogWrite(13, 10); 
         }
  
  if(v_h2==2){ 
        arduino.analogWrite(10, 90);arduino.analogWrite(11, 10);
        arduino.analogWrite(12, 10);arduino.analogWrite(13, 90); 
         }

  aftr_h2=v_h2;
  
 }

}
void disp_min(int v_m1,int v_m2)
{

 if (aftr_m1!=v_m1)
 {
  //tens position of minute(M1) arduino pin[2-5]
  if(v_m1==0){ 
        arduino.analogWrite(2, 10);arduino.analogWrite(3, 90);
        arduino.analogWrite(4, 90);arduino.analogWrite(5, 90); 
         }
  
  if(v_m1==1){ 
        arduino.analogWrite(2, 90);arduino.analogWrite(3, 10);
        arduino.analogWrite(4, 10);arduino.analogWrite(5, 10); 
         }
  
  if(v_m1==2){ 
        arduino.analogWrite(2, 90);arduino.analogWrite(3, 10);
        arduino.analogWrite(4, 10);arduino.analogWrite(5, 90); 
         }

  aftr_m1=v_m1;
 }

 if (aftr_m2!=v_m2)
 {
  //once position of minute(M2) arduino pin[6-9]
  if(v_m2==0){ 
        arduino.analogWrite(6, 10);arduino.analogWrite(7, 90);
        arduino.analogWrite(8, 90);arduino.analogWrite(9, 90); 
         }
  
  if(v_m2==1){ 
        arduino.analogWrite(6, 90);arduino.analogWrite(7, 10);
        arduino.analogWrite(8, 10);arduino.analogWrite(9, 10); 
         }
  
  if(v_m2==2){ 
        arduino.analogWrite(6, 90);arduino.analogWrite(7, 10);
        arduino.analogWrite(8, 10);arduino.analogWrite(9, 90); 
         }
  
  aftr_m2=v_m2;
  
 }
}

void draw ()
{


h=hour();
h2=int(h%10);
h1=int(h/10);

m=minute();
m2=int(m%10);
m1=int(m/10);

s=second();
s2=int(s%10);
s1=int(s/10);

//println("m1:m2 ",m1+":"+m2);
disp_min(m1,m2);//pass tens,once place for minutes
disp_hour(h1,h2);//pass tens,once place for hour

//test
delay(2000);
arduino.servoWrite(pin,0);    //pin=34, 34%16 = 2, PWM pin 2 runs
delay(2000);
arduino.servoWrite(pin,90);

}

thanks.

Answers

  • pwm expander??

    You can explain it better. Why not use your serial word?

  • Can you use additional hardware to arrange your motors into a 5x4 grid? So motor 1 rotates when row pin 0 and column pin 1 are both set, motor 7 when row 1 column 2 pins are both set?

Sign In or Register to comment.