Go Down

Topic: Please Guide line on Serial Package Control. (Read 3 times) previous topic - next topic

mem

For 12 servos, if the first pin is 2 then the last pin is 13

gmv

#11
Dec 02, 2010, 09:03 pm Last Edit: Dec 02, 2010, 09:05 pm by gmv Reason: 1
the problem i have now is that i have reserved pin 8 as an input on my pcb , so is there any inconvenient to use any analog pin as an input for data coming in ?

thanks

mem

#12
Dec 02, 2010, 09:40 pm Last Edit: Dec 02, 2010, 09:41 pm by mem Reason: 1
You can create an array to define the pins you want to use.
The sketch below uses pins 2 through 7  and 9 through 14 ( 14 is analog pin 0)

You can change the values used to initialize the servPins array if you want different pins

Code: [Select]
#include <Servo.h>

#define NBR_SERVOS     12        // the number of servos, up to 48 for Mega, 12 for other boards

Servo myServos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

const int servPins[NBR_SERVOS] = {2,3,4,5,6,7,9,10,11,12,13,14};

void setup()
{
 Serial.begin(9600);
 for(int i=0; i < NBR_SERVOS; i++)
   myServos[i].attach(servPins[i]);  
}


void loop()
{
 static int pos = 0;

 if ( Serial.available())
 {
   char ch = Serial.read();

   if(ch >= '0' && ch <= '9')              // is ch a number?  
     pos = pos * 10 + ch - '0';           // yes, accumulate the value
   else if(ch >= 'a' && ch <= 'a'+  NBR_SERVOS) // is ch a letter for one of our servos?
   {
     myServos[ch - 'a'].write(pos);         // yes, save the position in the position array  
     pos = 0;
     int channel =  ch - 'a';
     int  angle = myServos[channel].read();
     int pulseWidth = myServos[channel].readMicroseconds();
     Serial.print("Servo on pin ");
     Serial.print(servPins[channel], DEC);
     Serial.print(": angle = ");  
     Serial.print(angle,DEC),  Serial.print(", pulse = ");  
     Serial.println(pulseWidth,DEC);
   }
   else if (ch == '*')
   {
     // position all the servos
     for(int i=0; i < NBR_SERVOS; i++)
       myServos[i].write(pos);        
     pos = 0;
   }
   else if (ch == '+')
   {
     // sweep the servos from 0 to 180
     for(int angle = 0;angle < 180; )
     {
       for(int i=0; i < NBR_SERVOS; i++)
       {
         myServos[i].write(angle);
       }
       angle = myServos[0].read();    
       Serial.print("Angle = ");
       Serial.print(angle,DEC), Serial.print(", pulse = ");
       Serial.println(myServos[0].readMicroseconds(),DEC);
       ++angle;

       delay(20);
     }
     pos = 0;
   }
   else if (ch == '-')
   {
     // sweep the servos from 180 to 0
     for(int angle = 180;angle >= 0; )
     {
       for(int i=0; i < NBR_SERVOS; i++)
       {
         myServos[i].write(angle);                
       }
       angle = myServos[0].read();  
       Serial.print("Angle = ");
       Serial.print(angle,DEC), Serial.print(", pulse = ");
       Serial.println(myServos[0].readMicroseconds(),DEC);
       --angle;
       delay(20);
     }
     pos = 0;
   }
 }
}  

gmv

#13
Dec 02, 2010, 10:38 pm Last Edit: Dec 02, 2010, 10:40 pm by gmv Reason: 1
mem, thank again, it looks a lot easier this way, so i can use any analog pin as an output(digital) as well, ok thats nice....

To test this skecth i´ll be using windows Xp hyper terminal and send characters over the arduino to control servos, but in the future i´ll be making another pcb to read any ppm frame (futaba or Multiplex) from the radio trainer port,  so i would like to ask you what do you recommend as the best method to read it and send the several servo "pulse width" as characters to the ardu decoder board.

I´ve been searching for info about this for a while and it looks to me that it will take some time till i find the best solution.

thanks

mem

#14
Dec 03, 2010, 10:55 am Last Edit: Dec 03, 2010, 10:56 am by mem Reason: 1
Quote

i´ll be making another pcb to read any ppm frame (futaba or Multiplex) from the radio trainer port,  so i would like to ask you what do you recommend as the best method to read it and send the several servo "pulse width" as characters to the ardu decoder board.


That depends on what you have on your external PCB. I suggest you start another thread to discuss this so we don't hijack this one.
Post a link here so we can find your new thread.

Go Up