Go Down

Topic: Need Help Programming Multiple Continuous Rotation Servos (Read 1 time) previous topic - next topic

abrodech

Nov 27, 2011, 07:12 pm Last Edit: Nov 27, 2011, 10:41 pm by abrodech Reason: 1
So I have 3 continuous rotation Parallax servos, 3 pushbutton switches, and a motor, hooked up to my breadboard/arduino duemilanove on the digital pins. The servos are each attached to a pinion in a rack and pinion system, so that each one will descend a piston of sorts. Whenever a pushbutton is depressed, it needs to cause the corresponding servo to rotate slowly in one direction as long as it is being pushed, and when released, the single servo will stop. What I am getting from the code I have is that whenever I push a button, the corresponding servo will rotate, but the other two will either twitch or rotate as well. I looked this up and what ive gotten is that theres a delay from the arduino having to read multiple inputs at once, and the servos are updating too fast without response from the arduino, and thus twitch or spin. Please help me out. Its my first code Ive ever written for the arduino, and its for a class project thats due soon.

Quote

#include <SoftwareSerial.h>

#include <Servo.h>
Servo servo;
Servo servo2;
Servo servo3;

int switchone = 2;
int switchtwo = 3;
int switchthree = 4;

int val;
int val2;
int val3;

void setup ()
{
pinMode(switchone, INPUT);
pinMode(switchtwo, INPUT);
pinMode(switchthree, INPUT);

servo.attach(5);
servo2.attach(6);
servo3.attach(7);
}
void loop (){
 
 val = digitalRead(switchone); //Value from switch one
 val2 = digitalRead(switchtwo); //Value from switch two
 val3 = digitalRead(switchthree); //Value from switch three
   
 if (val == HIGH) //If switch one is on, then run servo
 {
 
   servo.attach(5);
   servo.write(90);

 }
 else //If switch one is off, then turn off servo
 {
   servo.detach();
 }
 
 if (val2 == HIGH) //If swtich two is on, then run servo2
 {
   servo2.attach(6);
   servo2.write(90);
 }
 else //If switch two is off, the turn off servo2
 {
   servo2.detach();
 }  
 
   
 if (val3 == HIGH) //If switch three is on, run servo3
 {  
 servo3.attach(7);
 servo3.write(90);
 }
 else //If switch three is off, turn off servo3
 {
  servo3.detach();
 }  
}


zoomkat

I've had continous rotation servos connected to my arduino that priodically twitched while resting at the neutral value (but not detached). If you don't have your buttons kept low with a resistor, you may be getting some floating high inputs. The code below puts the button between the pin and ground such that button press pulls the pin low. Otherwise the pin is kept high internally in the arduino.

Code: [Select]

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Go Up