2 Servo's sweep opposite directions via push button.

Hello wonderful people. I spent hours and hours browsing this forum last night, trying out tonnes of examples using zoomcats many hundreds of helpful codes from various threads but none of them seemed to work for me. For some reason connecting a switch straight up to ground without a resistor doesn't work for me. Im not sure what I'm doing wrong. I am very new to Electronics, Arduino and coding in general. I have many many hobbies and my basic needs from electronics make it very difficult for me to find the motivation to learn the entire skill as opposed to the very very basics for my requirements, however I've hit a dead end. I wouldn't consider myself a lazy girl, i just have very tight schedules on projects.
My basic needs are to have a button pressed and 2 servos respond. Servo A sweeps from 0-180 while Servo B sweeps from 180-0. Then on second press servo a sweeps from 0-180 while servo b sweeps from 180-0. Due to being unable get zoomcats "no resistor" codes to work.

My setup is as follows:
6v supply is connected to 5v mini via raw
Servo A on 6v, ground and pin 9
Servo B on 6v, ground and pin 10
Switch between Pin 2 and 6v and a resistor from pin 2 to ground (I have tried removing 6v and resistor then connecting switch straight to gnd so i can use zoomcats codes but they refuse to work)

Why am i unable to remove that resistor and connect switch straight to ground then simply use Zoomcats many examples?

Maybe there is even a more suitable code than this for obtaining my requirements? Thank you so much in advance for any help any of you can give me. I know you guys get annoyed with noobs asking basic questions but i have genuinely spent hours and hours looking for answers so please be nice :slight_smile: x

This code works for me, but both servos swing in the same direction. They also both start at 90 when power is added

#include <Servo.h> 


// Set digital pin numbers:
const int servoPin = 10;
const int servoPin1 = 9; // The number of the Servo pin
const int buttonPin = 2; // The number of the Pushbutton
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0; // Variable for reading direction of the servo


Servo myservo; // Create servo object to control a servo 
Servo myservo1;
int pos = 0; // Variable to store the servo position 
int pos1 = 180;


void setup() {
myservo.attach(10); 
myservo1.attach(9); // attaches the servo on pin 9 to the servo object 
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input


}


void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);


if (directionState == 0){
//The button is pushed
if (buttonState == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 180; pos=pos+1)
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’ 
delay(5); // waits 5ms for the servo to reach the position 
myservo1.write(pos); // tell servo to go to position in variable ‘pos’ 
delay(5); // waits 5ms for the servo to reach the position 

}
}


} else if (directionState == 1) {
// The button is pushed
if (buttonState == HIGH) {
directionState = 0; // The direction for the servo is anti-clockwise 


// goes from 180 degrees to 0 degrees in steps of 1 degree 
for(pos = 180; pos>=1; pos=pos-1)
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’ 
delay(5); // waits 5ms for the servo to reach the position 
myservo1.write(pos); // tell servo to go to position in variable ‘pos’ 
delay(5); // waits 5ms for the servo to reach the position 
}
}
}
}

For some reason connecting a switch straight up to ground without a resistor doesn't work for me

It would if you connected the other side to the right place AND enabled the internal pullup resistor.

myservo.write(pos); // tell servo to go to position in variable ‘pos’ 
myservo1.write(pos); // tell servo to go to position in variable ‘pos’

Since you write the SAME value to each servo, of course they are going to go the same way. If you write 180-pos to one, it will go the other way.

I have no idea what the correct place is, so i think I'll be avoiding that option.

I have changed the code as per your instructions and things are amazing!!!!!! They're both moving in the same direction. Thank you so so so soooo much.

Couple of little problems though. Now one of the servos moves slightly before the other one and slightly further.
Also, when i upload the code, they both move to centre and twitch a bit. how could i get them to start at 0 and 180 respectively?

Here is my altered code.

 #include <Servo.h> 

 // Set digital pin numbers:
 const int servoPin = 10;
 const int servoPin1 = 9; // The number of the Servo pin
 const int buttonPin = 2;  // The number of the Pushbutton
 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo

 Servo myservo;  // Create servo object to control a servo 
 Servo myservo1;
 int pos = 0;  // Variable to store the servo position 

 void setup() {
   myservo.attach(10); 
   myservo1.attach(9); // attaches the servo on pin 8 to the servo object 
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input

 }

 void loop(){
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);

   if (directionState == 0){
     //The button is pushed
     if (buttonState == HIGH) {
       directionState = 1;// The direction for the servo is clockwise
       // goes from 0 degrees to 180 degrees in steps of 1 degree
       for(pos = 0; pos < 180; pos=pos+1)
       {
         myservo.write(pos);  // tell servo to go to position in variable ‘pos’ 
         delay(5);  // waits 15ms for the servo to reach the position 
          myservo1.write(180-pos);  // tell servo to go to position in variable ‘pos’ 
         delay(5);  // waits 15ms for the servo to reach the position 
         
       }
     }

   } else if (directionState == 1) {
     // The button is pushed
     if (buttonState == HIGH) {
       directionState = 0;  // The direction for the servo is anti-clockwise 

       // goes from 180 degrees to 0 degrees in steps of 1 degree 
       for(pos = 180; pos>=1; pos=pos-1)
       {
         myservo.write(pos);  // tell servo to go to position in variable ‘pos’ 
         delay(5);  // waits 15ms for the servo to reach the position 
         myservo1.write(180-pos);  // tell servo to go to position in variable ‘pos’ 
         delay(5);  // waits 15ms for the servo to reach the position 
       }
     }
   }
 }
for(pos = 0; pos < 180; pos=pos+1)
       {
         myservo.write(pos);  // tell servo to go to position in variable ‘pos’ 
         delay(5);  // waits 15ms for the servo to reach the position 
          myservo1.write(180-pos);  // tell servo to go to position in variable ‘pos’ 
         delay(5);  // waits 15ms for the servo to reach the position 
         
       }

If you want them to move at the same time, why have two delays (both of which have incorrect comments)?

Not actually sure. I assumed I'd need a delay for each servo. I have now removed 2 of the delays however, one of the servos still moves before the other one. If i remove all 4 delays, the servos just spit and stutter. Have managed to get them to begin at 180 and 0 respectively by adding my servo.write to the start of the void loop.
New code:

 #include <Servo.h> 

 // Set digital pin numbers:
 const int servoPin = 10; // The number of Servo pin
 const int servoPin1 = 9; // The number of Servo pin 1
 const int buttonPin = 2;  // The number of the Pushbutton
 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo

 Servo myservo;  // Create servo object to control a servo 
 Servo myservo1; // Create a servo object to control a second servo
 int pos = 0;  // Variable to store the servo position 

 void setup() {
   myservo.attach(10); // attaches servo on pin 10
   myservo1.attach(9); // attaches second servo on pin 9  
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
  
 }

 void loop(){
    myservo.write(0);
   myservo1.write(180);
   buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:

   if (directionState == 0){
     //The button is pushed
     if (buttonState == HIGH) {
       directionState = 1;// The direction for the servo is clockwise
       // goes from 0 degrees to 180 degrees in steps of 1 degree
       for(pos = 0; pos < 180; pos=pos+1)
       {
         myservo.write(pos);  // tell servo to go to position in variable ‘pos’ 
         
     myservo1.write(180-pos);  // tell servo to go to position in variable ‘180-pos’ 
         delay(5);  // waits 5ms for the servo to reach the position 
         
       }
     }

   } else if (directionState == 1) {
     // The button is pushed
     if (buttonState == HIGH) {
       directionState = 0;  // The direction for the servo is anti-clockwise 

        
       for(pos = 180; pos>=0; pos=pos-1) // goes from 180 degrees to 0 degrees in steps of 1 degree
       {
       myservo.write(pos);  // tell servo to go to position in variable ‘pos’ 
       myservo1.write(180-pos);  // tell servo to go to position in variable ‘180-pos’ 
         delay(5);  // waits 5ms for the servo to reach the position 
       }
     }
   }
 }

You have two loops - one for each servo. If you want to make them move at the same time you need to control both servos within the same loop.

Also note that servos are not precise and you may find that 170deg on one corresponds to 175deg on the other (for example). You will need to experiment to find the appropriate values for the two servos you want to use. If you need more precision you should look at servo.writeMicroseconds().

...R

As much as i would love to use "servo.writeMicroseconds" as it seems more precise, I have absolutely no idea how to apply it. I've never even come across this term before. Google isn't being much help.

Edit: Cancel that, I appear to have figured it out.
Ummmm, now i have go this far, can anyone tell me how to remove the resistor from the switch?... It confuses me.
New Code...

 #include <Servo.h> 

 //Set digital pin numbers:
 const int servoPin = 10; // The number of Servo pin
 const int servoPin1 = 9; // The number of Servo pin 1
 const int buttonPin = 2;  // The number of the Pushbutton
 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo

 Servo myservo;  // Create servo object to control a servo 
 Servo myservo1; // Create a servo object to control a second servo
 int pos = 0;  // Variable to store the servo position 

 void setup() {
 myservo.attach(10); // attaches servo on pin 10
 myservo1.attach(9); // attaches second servo on pin 9  
 pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
  
 }

 void loop()
 {
   myservo.write(0);
   myservo1.write(180);
   buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:

   if (directionState == 0){//The button is pushed
     if (buttonState == HIGH) 
     {
       directionState = 1;// The direction for the servo is clockwise
       for(pos = 0; pos < 180; pos=pos+1)// goes from 0 degrees to 180 degrees in steps of 1 degree
       {
        myservo.writeMicroseconds(1000);  // tell servo to go to position in variable ‘pos’ 
        myservo1.writeMicroseconds(2000);  // tell servo to go to position in variable ‘180-pos’ 
        delay(5);  // waits 5ms for the servo to reach the position 
         
       }
     }

   } else if (directionState == 1) { // The button is pushed
     if (buttonState == HIGH) 
     {
       directionState = 0;  // The direction for the servo is anti-clockwise 
       for(pos = 180; pos>=0; pos=pos-1) // goes from 180 degrees to 0 degrees in steps of 1 degree
       {
        myservo.writeMicroseconds(2000);  // tell servo to go to position in variable ‘pos’ 
        myservo1.writeMicroseconds(1000);  // tell servo to go to position in variable ‘180-pos’ 
        delay(5);  // waits 5ms for the servo to reach the position 
       }
     }
   }
 }

xxxx

       for(pos = 180; pos>=0; pos=pos-1) // goes from 180 degrees to 0 degrees in steps of 1 degree
       {
        myservo.writeMicroseconds(2000);  // tell servo to go to position in variable ‘pos’ 
        myservo1.writeMicroseconds(1000);  // tell servo to go to position in variable ‘180-pos’ 
        delay(5);  // waits 5ms for the servo to reach the position 
       }

What is going on here ? The comments bear no relationship to the code, the for loop variable is not used inside the for loop, the servos will move just once then stay put and you do this 180 times. Why ?

If you want the servos to move in small steps you must use the for loop variable in the writeMicroseconds() command but it will need to match the value required by writeMicroseconds(), ie not 180 to 0

My apologies. In my excitement, i edited the commands as per instructions, but didn't edit the comments. Not sure of the accurate comment so I have removed "pos".

I guess some of this code is now redundant, but not sure which parts to remove and I'm sure they're not doing any harm laying dormant?...

 #include <Servo.h> 

 //Set digital pin numbers:
 const int servoPin = 10; // The number of Servo pin
 const int servoPin1 = 9; // The number of Servo pin 1
 const int buttonPin = 2;  // The number of the Pushbutton
 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo

 Servo myservo;  // Create servo object to control a servo 
 Servo myservo1; // Create a servo object to control a second servo
 int pos = 0;  // Variable to store the servo position 

 void setup() {
 myservo.attach(10); // attaches servo on pin 10
 myservo1.attach(9); // attaches second servo on pin 9  
 pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
  
 }

 void loop()
 {
   myservo.write(0);
   myservo1.write(180);
   buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:

   if (directionState == 0){//The button is pushed
     if (buttonState == HIGH) 
     {
       directionState = 1;// The direction for the servo is clockwise
       for(pos = 0; pos < 180; pos=pos+1)// goes from 0 degrees to 180 degrees in steps of 1 degree
       {
        myservo.writeMicroseconds(1000);  // tell servo to go to position 
        myservo1.writeMicroseconds(2000);  // tell servo to go to position 
        delay(10);  // waits 10ms for the servo to reach the position 
         
       }
     }

   } else if (directionState == 1) { // The button is pushed
     if (buttonState == HIGH) 
     {
       directionState = 0;  // The direction for the servo is anti-clockwise 
       for(pos = 180; pos>=0; pos=pos-1) // goes from 180 degrees to 0 degrees in steps of 1 degree
       {
        myservo.writeMicroseconds(2000);  // tell servo to go to position
        myservo1.writeMicroseconds(1000);  // tell servo to go to position
        delay(10);  // waits 10ms for the servo to reach the position 
       }
     }
   }
 }
       for(pos = 0; pos < 180; pos=pos+1)// goes from 0 degrees to 180 degrees in steps of 1 degree
       {
        myservo.writeMicroseconds(1000);  // tell servo to go to position 
        myservo1.writeMicroseconds(2000);  // tell servo to go to position 
        delay(10);  // waits 10ms for the servo to reach the position 
         
       }

Changing the comments does not make the code correct. You are still telling the servos to do the same thing 180 times.myservo.writeMicroseconds(1000);will make the servo go to one end of its range and it will stay there because you never change the value of the number of microseconds. Ty a loop like this

  for (int ms = 1000; ms <= 2000; ms++)
  {
     servo1.writeMicroseconds(ms);
     delay(10);
  }

UKHeliBob:

       for(pos = 0; pos < 180; pos=pos+1)// goes from 0 degrees to 180 degrees in steps of 1 degree

{
        myservo.writeMicroseconds(1000);  // tell servo to go to position
        myservo1.writeMicroseconds(2000);  // tell servo to go to position
        delay(10);  // waits 10ms for the servo to reach the position
         
       }


Changing the comments does not make the code correct. You are still telling the servos to do the same thing 180 times.` myservo.writeMicroseconds(1000); `will make the servo go to one end of its range and it will stay there because you never change the value of the number of microseconds. Ty a loop like this

for (int ms = 1000; ms <= 2000; ms++)
  {
     servo1.writeMicroseconds(ms);
     delay(10);
  }

I will try adding this when I get home from work. It confuses me though because my code is doing as I ask of it. I press the switch once and both servos move in opposite directions, I press it again and they move back. Is this extra code draining battery or something? What makes that part of the code my enemy if I'm currently getting my desired results?

They move because you tell them to go directly to the new positions at full speed. The for loop tells them to do it 180 times but they don't move 180 times because they are already in position after the first time. Comment out the for loop and its closing brace and the servos will still move but just once.

What my example for loop does is to move the servo slowly in 1000 very small steps with a short delay between each step. Adjust the number of steps and/or the delay between them to suit your needs or if you don't need slow movement remove the for loop and the delay().

UKHeliBob:
They move because you tell them to go directly to the new positions at full speed. The for loop tells them to do it 180 times but they don't move 180 times because they are already in position after the first time. Comment out the for loop and its closing brace and the servos will still move but just once.

What my example for loop does is to move the servo slowly in 1000 very small steps with a short delay between each step. Adjust the number of steps and/or the delay between them to suit your needs or if you don't need slow movement remove the for loop and the delay().

OK, well i did as you asked, now they move to the positions as they did before, but now they buzz for about 5 seconds and won't respond to button presses during this time.

New code:

 #include <Servo.h> 

 //Set digital pin numbers:
 const int servoPin = 10; // The number of Servo pin
 const int servoPin1 = 9; // The number of Servo pin 1
 const int buttonPin = 2;  // The number of the Pushbutton
 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo

 Servo myservo;  // Create servo object to control a servo 
 Servo myservo1; // Create a servo object to control a second servo
 int pos = 0;  // Variable to store the servo position 

 void setup() {
 myservo.attach(10); // attaches servo on pin 10
 myservo1.attach(9); // attaches second servo on pin 9  
 pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
  
 }

 void loop()
 {
   myservo.write(0);
   myservo1.write(180);
   buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:

   if (directionState == 0){//The button is pushed
     if (buttonState == HIGH) 
     {
       directionState = 1;// The direction for the servo is clockwise
       for (int ms = 1000; ms <= 2000; ms++)
       {
        myservo.writeMicroseconds(1000);  // tell servo to go to position 
        myservo1.writeMicroseconds(2000);  // tell servo to go to position 
        delay(10);  // waits 10ms for the servo to reach the position 
         
       }
     }

   } else if (directionState == 1) { // The button is pushed
     if (buttonState == HIGH) 
     {
       directionState = 0;  // The direction for the servo is anti-clockwise 
       for (int ms = 1000; ms <= 2000; ms++)
       {
        myservo.writeMicroseconds(2000);  // tell servo to go to position
        myservo1.writeMicroseconds(1000);  // tell servo to go to position
        delay(10);  // waits 10ms for the servo to reach the position 
       }
     }
   }
 }
for (int ms = 1000; ms <= 2000; ms++)
       {
        myservo.writeMicroseconds(1000);  // tell servo to go to position 
        myservo1.writeMicroseconds(2000);

What's the point of the loop?

AWOL:

for (int ms = 1000; ms <= 2000; ms++)

{
        myservo.writeMicroseconds(1000);  // tell servo to go to position
        myservo1.writeMicroseconds(2000);



What's the point of the loop?

I have no idea what a "loop" is. Terminology is something I've dodged like crazy. I've just done as i was told by the other guy.

In programming a loop is when a piece of code is repeated several times.

In this case

for (int ms = 1000; ms <= 2000; ms++)
       {

it is repeated 1000 times with the value ms increasing by 1 each time

A little bit of reading seems to be indicated ... ?

...R

OK, well i did as you asked

Actually you didn't

I suggested

for (int ms = 1000; ms <= 2000; ms++)
  {
     servo1.writeMicroseconds(ms);
     delay(10);
  }

You wrote

for (int ms = 1000; ms <= 2000; ms++)
       {
        myservo.writeMicroseconds(2000);  // tell servo to go to position
        myservo1.writeMicroseconds(1000);  // tell servo to go to position
        delay(10);  // waits 10ms for the servo to reach the position 
       }

Terminology is something I've dodged like crazy.

You had better give up trying to use an Arduino then.

You had better give up trying to use an Arduino any computer then.

I fixed that for you.

PaulS:

You had better give up trying to use an Arduino any computer then.

I fixed that for you.

As with any hobby or interest, there are always veterans such as yourselves that get unreasonably frustrated with noobs like me, completely forgetting that they started somewhere once upon a time. I am actually a professional magician. I've spent thousands upon thousands of hours studying the art of magic, which is an undying passion of mine. I require this simple mechanism to achieve a magic illusion. My lack of knowledge is not based on laziness. I have devoted so much time to the art that i love, i am simply dipping into a passion that you all love to take away the small part that I require. I have no drive to learn an entire new skill set which i could never make any use of, other than this very very small thing. I admin a magic group which has people from all skill levels and I regularly witness condescending attitudes from the more experienced members towards the lesser experienced members. I personally believe this attitude to be entirely unnecessary. There will always be people with different levels of interest in any given subject. Does the fact my interest or needs don't flow as deep as yours mean that I am not worth your time? Maybe that I am a lower life form that can be spoken down to?... Having seen this attitude time and time again in the magic community i knew what to expect and i have put off posting here for weeks. When i eventually did pick up the courage i attempted to be as polite and lovely as i could. After all, I'm a stranger on your territory. Still, my attitude failed and i was right to expect the worst for trespassing on your territory. I assure you, if any of you came into my territory in the magical arts, you would be greeted with open arms and no question is to stupid or worth patronising you for.

This comment: "You had better give up trying to use any computer then." was absolutely unnecessary and actually very rude. Im not sure why you felt the need to be so condescending. Not only are my computing skills for my day to day requirements more than competent but also totally unrelated. Have i personally insulted YOU in some way? Did you feel big for having a joke at my expense? Do you normally regard women in such a condescending manner? The fact I've even bought the components and trying to get them to move myself is more than hundreds of thousands of other people bother to do! I could just buy this entire setup prebuilt for considerably less money than my time spent learning is worth.

Would you spend time learning an entire new skill set when you only require a small snippet of it from a community that is designed to help? Its not as if i haven't put the time in. Ive spent hundreds of hours watching tutorials and trying to get this very basic thing on my own, but i failed. Its like learning an entire new language just for a few words, without knowing where those words are located. I don't expect it handed to me on a plate but i also don't expect my inquisitiveness to be mocked.

After seeing how members of groups treat newbies it was a last resort coming here for answers. At least i got close to finding my answer.

If you don't wish to help me then don't comment. Im not holding a gun to your head or forcing you. I also take comments like the above very personally so I'd appreciate if you didn't comment on this thread again please.

Thank you so much for your help everyone else. My code kind of does as i wish it to do. That will have to do.

In future, just be nice to people. Being nice costs nothing and makes another person smile. Like i was last night when i thought my code was complete. Now i just feel a twat.

UKhelibob. I will change my code to read what you have suggested. Thank you. Your comment on giving up was also unnecessary though.

A woman scorned :frowning:

In future, just be nice to people. Being nice costs nothing and makes another person smile. Like i was last night when i thought my code was complete. Now i just feel a twat.

Don't let some of the forum "regulars" get to you with their "help". Several appear to have been born with broomsticks where the sun doesn't shine, and newbies (and myself) often grind their sticks. That being said, have you gotten "button" control of the servos yet? Below is some test code that might be close to what you need. I currently don't have a servo connected to my arduino for testing, so this may/may not work. Also note that servos should have a separate power supply and not be powered from the arduino.

//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 myservo;
int pos = 0;    // variable to store the servo position 

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

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
    {                                  // in steps of 1 degree 
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
  }
}