Go Down

Topic: Controlling 2 continuous servo motors using tactile clicks. (Read 927 times) previous topic - next topic


Im very new to arduino. I already have the coding to control 1 continuous servo. What i dont know is how to modify the coding so that it controls two servos. What do i need to do guys?

Here is the coding :

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int servoPin = 9;// this pin must be of those with PWM ~

#define STOPpin 3 // push button pin  for STOP
#define CWpin 2 //  push button for CW
#define CCWpin 4 // push button for CCW

int sc[]={180, 90, 0};// servo commands are in order
        //CCW, STOP,CW
String scText[]={"CCW","Stop","CW"};// define texts for 3 action
int statusText;
      //CW button status (CWBS)
      //CCW button status (CCWBS)
      //stop button status (SBS)

void setup() {
  pinMode(STOPpin,INPUT_PULLUP);// set pin for push button STOP
  pinMode(CCWpin,INPUT_PULLUP);// set pin for push button CCW 
  pinMode(CWpin,INPUT_PULLUP);// set pin for push button CW
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  myservo.write(sc[1]);// send STOP command
  statusText=1;// initial value is STOP

void loop() {

    CCWBS = digitalRead(CCWpin);// read status of button CCW
    SBS = digitalRead(STOPpin);// read status of button STOP
    CWBS = digitalRead(CWpin);// read status of button CW
    if(CCWBS ==LOW)
    }else if(SBS ==LOW)
    }else if(CWBS ==LOW)
}// loop

void servoCommand(int n)
   statusText = n;
   Serial.print("Going to  ");
   Serial.print( "(");


You need to add a second servo object. E.g. Servo myotherservo; just below the first one.

then in setup() you need to attach() it to any digital pin (and while you're there correct the comment on the first servo attach() - servos do NOT need to be on PWM pins).

Then in loop() you need some myotherservo.write() commands to do whatever you want it to do with it.

It could hardly be simpler.



Alright thank you Steve. Will do the modifications and test it on my servo

Go Up