servo help needed please

Hello all

Im using freeduino v1.16 and a roboduino i/o expansion

this is the code im using

#include <Servo.h> 
 
Servo digger ;  
Servo dig ;
Servo up ;
Servo down ;
 
int pos = 0;                                           
void setup() 

{ 
digger.attach(9); 
} 
 
void loop() 
{ 
  
  for(pos = 75; pos < 120; pos += 1)                 
 {                                 
   digger.write(pos);              
 delay(40);                     
  } 

                                    
  for(pos = 120; pos>=  75; pos-=1)    
  {                                
    digger.write(pos);            
   delay(40);                     
  } 
}

when i use pin 9 and 10 it works fantastic.

i need to run 4 servos and i cant get it to work with any other pins

how can i run 4 servos?

thanks jason

The megaServo library will let you run up to 12 servos on your board: http://www.arduino.cc/playground/Code/MegaServo

#include <MegaServo.h>

Servo digger ;
Servo dig ;
Servo up ;
Servo down ;

int pos = 0;
void setup()
{
  digger.attach(9);
  dig.attach(10);
  up.attach(11);  // you can attach any digital pin 
  down.attach(12);
}

void loop()
{

  for(pos = 75; pos < 120; pos += 1)
 {
   digger.write(pos);
   delay(40);
  }


  for(pos = 120; pos>=  75; pos-=1)
  {
    digger.write(pos);
    delay(40);
  }
}

thankyou mem

i will have a play

hello

ive had a look at the megaservo code and it dosen’t mean anything to me.
im getting lost could someone send me a code with some comments on it to get me started

thankyou

ps im a newbie

The megaServo library will let you run up to 12 servos on your board:

You can try this code

#include <MegaServo.h>

MegaServo digger ;  // max servos is 48 for mega, 12 for other boards
MegaServo dig ;
MegaServo up ;
MegaServo down ;

int pos = 0;
void setup()
{
  digger.attach(9);
  dig.attach(10);
  up.attach(11);  // you can attach any digital pin
  down.attach(12);
}

void loop()
{

  for(pos = 75; pos < 120; pos += 1)
 {
   digger.write(pos);
   dig.write(pos);
   up.write(pos);
   down.write(pos);
   delay(40);
  }


  for(pos = 120; pos>=  75; pos-=1)
  {
    digger.write(pos);
   dig.write(pos);
   up.write(pos);
   down.write(pos);
    delay(40);
  }
}

ive had a look at the megaservo code and it dosen’t mean anything to me. im getting lost could someone send me a code with some comments on it to get me started

The MegaServo playground write-up has a link to the Arduino Servo reference information at the top of the article. This documents the functions and has example code. did you have a look at that?

hello mem

sorry to confuse things but this is the code i would like come comments on

MegaServo Servos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

int pos = 0;      // variable to store the servo position 
int potPin = 0;   // connect a pot to this pin.

void setup()
{
  for( int i =0; i < NBR_SERVOS; i++)
    Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);
}
void loop()
{ 
  pos = analogRead(potPin);   // read a value from 0 to 1023
  for( int i =0; i <NBR_SERVOS; i++) 
    Servos[i].write( map(pos, 0,1023,0,180));   
  delay(15);   
}

thanks jason

hello folks again

I have downloaded the megaservo from the arduino website.
I have also copied and pasted the reply code from BillHo into the file but im now getting an error message when i upload to my board.

can you please explain where im going wrong

23: error: MegaServo.h: No such file or directory In function ‘void setup()’:
In function ‘void loop()’:

thanks jason

Jason, is MegaServo copied into the Arduino Library directory? From the Arduino IDE menu, select Sketch / ImportLibrary - can you see MegaServo in the list of libraries?

see: http://www.arduino.cc/en/Hacking/Libraries

hello mem

sorry im a complete virgin to all of this…your comment has confused me even more…

From the Arduino IDE menu, select Sketch / ImportLibrary - can you see MegaServo in the list of libraries?

please bare with me on this one, can you talk me through it step by step

thanks jason :-?

You should be able to find information on installing libraries using the following keywords in google:
how to install arduino library

if you have any questions after you have read through some of these entries, please do ask.

hello again
Thanks for your help last time.

im using the megaservo library and this is my code but it seems to be a bit jittery am i doing something wrong or could the code be written in a different way.

my aim is to control 4 servos to run a model railway digger.

Thanks jason

// This is for a model digger in Ho scale for a model railway
// Jason fletcher = 01.06.09

#include <MegaServo.h>

MegaServo digger ;  
MegaServo dig ;
MegaServo up ;
MegaServo down ;

int pos = 0;                                           
void setup() 

{ 
digger.attach(8); 
} 
 
void loop() 
{ 
  
  for(pos = 75; pos < 120; pos += 1)                 
 {                                 
   digger.write(pos);              
 delay(40);                     
  } 

                                   
  for(pos = 120; pos>=  75; pos-=1)    
  {                                
    digger.write(pos);            
   delay(40);                     
  } 

//.........................
{ 
down.attach(10);
} 

  
  for(pos = 80; pos < 160; pos += 1) //up                
  {                                 
    down.write(pos); 
    delay(100);  
  
  } 
                                      
  for(pos = 160; pos>=  80; pos -= 1)    //down
  {                                
    down.write(pos);    
   delay(10);                     
  } 


 for(pos = 80; pos < 180; pos += 1) //up                
  {                                 
    down.write(pos); 
    delay(100);  
  
  } 

//.............................................
{ 
 up.attach(9);
} 
 
 for(pos = 130; pos>= 70; pos-= 1)  //up  
 {                                
    up.write(pos);    
    delay(100);                     
 } 
 
 delay(500);

for(pos = 90; pos < 130; pos += 1)    //down             
 {                                 
   up.write(pos); 
   delay(100);                     
 } 


}
//.............................

How are you powering the servos?
You must use a separate supply - don’t use the 5V from the Arduino.
Remember to connect the grounds.

If you’ve still got jitters, make sure the supply is capable of plenty of current.

If you’ve still got problems, have a search for decoupling (I think Grumpy Mike has a tutorial on this)

Hello AWOL

yes i am using a separate power supply

thanks!!

Edited: Thankyou AWOL it was my powersupply!! sorted for now…

Jason, the current needed depends on the servo and the load but 500ma is probably not enough. Perhaps I can say more if I knew what servos are you using and what kind of load each servo has, but I would test with a 1.5amps or more if you can.

BTW, your code keeps attaching pin 10 in loop – it is better if you move that line into setup.

hello mem

can you please explain what you mean. and can you correct my code so that it is correct because at the moment im still playing and i still don’t under stand it all yet

BTW, your code keeps attaching pin 10 in loop – it is better if you move that line into setup.

thanks jason

I think what mem is saying is that you only need to attach the servos to the pins once so that should be done in the setup function.

Currently only the “digger” servo is attached in the setup function. By attaching the up and down servos in your main loop, it is attaching them each time through the loop and this may cause some jittering. Your setup function should look something like this:

void setup()
{
digger.attach(8);
up.attach(9);
down.attach(10);
}

void loop()
{
//the rest of your code
}

thanks Ill Mill, that is it.

thankyou for getting back to me

thanks for your help

jason