Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Controlling two server with arduino uno
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Controlling two server with arduino uno

by J_Cummings on Sat Jan 25, 2020 4:00 pm

I am new to trying to use the arduino. I have purchased a starter kit and have completed all the starter kit projects. My project is to control two servos with 4 momentary switches. My plan is to have two switches per servo one controlling movement right and the other left. When any of the switch are pressed nothing happens. My external power is 5v at 3.A max
Any help will be greatly appreciated.

Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>

#define S1LEFT 12
#define S1RIGHT 2
#define S2LEFT 8
#define S2RIGHT 4

  Servo servo1;
  Servo servo2;

  int angle = 90;
  int angleStep = 10;

void setup() {
  servo1.attach(9);
  servo2.attach(10);

  pinMode(S1LEFT,INPUT_PULLUP);
  pinMode(S1RIGHT,INPUT_PULLUP);
 
  pinMode(S2LEFT,INPUT_PULLUP);
  pinMode(S2RIGHT,INPUT_PULLUP);
}

void loop() {
  if(digitalRead(S1RIGHT)==LOW || digitalRead(S2RIGHT==LOW)){
      sweep(servo1, angle + angleStep);
      sweep(servo2, angle + angleStep);
  }

  if(digitalRead(S2LEFT)==LOW || digitalRead(S1LEFT==LOW)){
      sweep(servo1, angle - angleStep);
      sweep(servo2, angle - angleStep);
  }
}

void sweep(Servo &servo, int angle){
  servo.write(angle);
  if(angle < 0) angle = 0;
  if(angle > 180) angle = 180;
  delay(50);
}
Attachments
Servos2Button.png
drawing of project
Servos2Button.png (168.49 KiB) Viewed 110 times
servo2.ino
sketch of project
(801 Bytes) Downloaded 5 times
Last edited by adafruit_support_bill on Sat Jan 25, 2020 4:21 pm, edited 1 time in total.
Reason: Pleas use [code] tags when posting code to the forums

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Re: Controlling two server with arduino uno

by adafruit_support_bill on Sat Jan 25, 2020 4:22 pm

Your servos need a ground connection to the power supply.

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling two server with arduino uno

by J_Cummings on Sat Jan 25, 2020 4:31 pm

adafruit_support_bill wrote:Your servos need a ground connection to the power supply.


I install a jumper to the other ground on the arduino and the other end to the bread board b16. I am still not getting anything?

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Re: Controlling two server with arduino uno

by adafruit_support_bill on Sat Jan 25, 2020 4:34 pm

Please post some photos showing your setup and all the connections.

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling two server with arduino uno

by J_Cummings on Sat Jan 25, 2020 5:21 pm

adafruit_support_bill wrote:Please post some photos showing your setup and all the connections.


Thanks for all of your help. I have attached an updated drawing. I have already attached my sketch. Any input on the sketch will be greatly appreciated. Is there a different way to do this? is the an error in my sketch?

Thanks again for your time
Attachments
Servos2Button.png
Servos2Button.png (171.01 KiB) Viewed 96 times

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Re: Controlling two server with arduino uno

by adafruit_support_bill on Sat Jan 25, 2020 6:08 pm

The sketch looks OK. But actual photos may offer clues to the problem.

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling two server with arduino uno

by J_Cummings on Sat Jan 25, 2020 11:08 pm

Here are two pictures of my project. The one switch is in the wrong location in the photo. I moved it back.
Attachments
ProjectS22.jpg
second photo of test project
ProjectS22.jpg (148.09 KiB) Viewed 88 times
ProjectS21.jpg
photo of test project
ProjectS21.jpg (138.97 KiB) Viewed 88 times

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Re: Controlling two server with arduino uno

by adafruit_support_bill on Sun Jan 26, 2020 8:11 am

If you run the servo 'sweep' example, does the servo on pin 9 move?
Change the pin in the code to test pin 10 also.

https://www.arduino.cc/en/tutorial/sweep

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling two server with arduino uno

by J_Cummings on Sun Jan 26, 2020 3:55 pm

I tested both servos with the sweep sketch. I found out the two servos both only sweep 90 degrees. The servos are Traxxas RC steering servos, (2075X). I had removed to the servos for ones with higher torque. I can't find any specifications on these servos. Is it possible that these servos start at 0 degrees and move 90 degrees each direction? I used the servo from my Arduino starter kit and that servo sweeps 180 degrees.

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Re: Controlling two server with arduino uno

by adafruit_support_bill on Sun Jan 26, 2020 4:08 pm

I'm not familiar with those servos. This is the Servo Database page on them: https://servodatabase.com/servo/traxxas/2075

It sounds like the servo part of the circuit is OK. If nothing moves when you press the buttons, then there may be a problem in your button circuit.

Try adding some serial output to your loop so you can see in the Serial Monitor when the buttons are pressed.

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling two server with arduino uno

by J_Cummings on Mon Jan 27, 2020 2:03 pm

I tested with the sweep sketch on both of the Traxxas seros. The servos only move 90 degrees. I tested the sweep sketch with the mini servo that came with my starter kit it move 180 degrees. I am thinking that the Traxxas servos start at the center as zero and move positive 90 degrees and negative 90 degrees. How would I modify the sweep sketch to test the theory?

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Re: Controlling two server with arduino uno

by adafruit_support_bill on Mon Jan 27, 2020 2:19 pm

You can specify the min and max pulse width as optional parameters to the attach call:
https://www.arduino.cc/en/Reference/ServoAttach

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling two server with arduino uno

by J_Cummings on Sun Feb 02, 2020 3:31 pm

I have replaced both servo's with mini servo's that work with the sweep servo sketch. I have added serial monitor to see that is going on when the switches are depressed. I get no output to the serial monitor when the switches are depressed. I will add the changed code to this post and new pictures of the project.

Code: Select all | TOGGLE FULL SIZE
/*
 * Controlling two servos with push buttons. When the left button
 * is press the servo moves left in ten degree increments. When the
 * right putton is press the servo moves right in ten degree increments
 */

#include <Servo.h>

  Servo servo1;  // create servo object servo 1
  Servo servo2;  // creare servo pbject servo 2

  int angle =90;  // inital angle for servo's
  int angleStep =10;  // increment to move servo


#define LEFTS1 12  // pin 12 connected to servo 1 left button
#define RIGHTS1 2  // pin 2 connected to servo 1 right button
#define LEFTS2 8  // pin 8 connected to servo 2 left button
#define RIGHTS2 4 // pin 4 connected to servo 2 right button


void setup() {
  Serial.begin(9600);  //setup serial
  servo1.attach(9);  // attaches servo 1 to pin 9
  servo2.attach(10); // attaches servo 2 to pin 10

  pinMode(LEFTS1,INPUT_PULLUP);  // assign pin 12 as input for servo 1 left button
  pinMode(RIGHTS1,INPUT_PULLUP); // assign pin 2 as input for servo 1 right button
 
  pinMode(LEFTS2,INPUT_PULLUP);  // assign pin 8 as input for servo 2 left button
  pinMode(RIGHTS2,INPUT_PULLUP); // assign pin 4 as input for servo 2 right button

  servo1.write(angle); // send servo 1 to middle at 90 degrees
  servo2.write(angle); // send servo 2 to middle at 90 degrees
  Serial.println("Servo Button Test ");
}

void loop() {
  while(digitalRead(RIGHTS1)==LOW || digitalRead(RIGHTS2==LOW)){

      if (angle > 0 && angle <= 180) {
        angle = angle - angleStep;
        if(angle < 0){
          angle = 0;
        }else{
          servo1.write(angle);  // moves servo 1 right to desired angle
          servo2.write(angle);  // moves servo 2 right to desired angle
          Serial.print("Moved to: ");
          Serial.print(angle);  //print the angle
          Serial.println(" degree");
        }
      }

 delay(50);  // waits for the servo to move there
  }// while

  while(digitalRead(LEFTS1)==LOW || digitalRead(LEFTS2==LOW)){

      if (angle >= 0 && angle <= 180){
        angle = angle + angleStep;
        if(angle >180){
          angle =180;
        }else{
       servo1.write(angle);  // move servo 1 left to desired angle
       servo2.write(angle);  // move servo 2 left to desired angle
       Serial.print("Moved to: ");
       Serial.print(angle);  // print the angle
       Serial.println(" degree");   
        }
  }

   delay(50);  // waits for servo to move there
   }//


Attachments
20220-2.jpg
20220-2.jpg (186.44 KiB) Viewed 50 times
20220-1.jpg
20220-1.jpg (152.87 KiB) Viewed 50 times
servo2v2.ino.ino
(2.38 KiB) Downloaded 3 times
Last edited by adafruit_support_bill on Mon Feb 03, 2020 6:42 am, edited 1 time in total.
Reason: Added code in-line

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Re: Controlling two server with arduino uno

by adafruit_support_bill on Mon Feb 03, 2020 6:57 am

I get no output to the serial monitor when the switches are depressed.

Do you at least see the "Servo Button Test " oiutput at startup?

The button wiring looks good and I don't see any obvious issues in the code. Try adding this code at the very top of your loop:

Serial.print("R1 ");
Serial.println(digitalRead(RIGHTS1));
Serial.print("R2 ");
Serial.println(digitalRead(RIGHTS2));

Serial.print("L1 ");
Serial.println(digitalRead(LEFTS1));
Serial.print("L21 ");
Serial.println(digitalRead(LEFTS2));

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling two server with arduino uno

by J_Cummings on Mon Feb 03, 2020 10:43 pm

Using the same wiring I wrote a new sketch. I now have both servos moving. Serial out put "Servo Button Test"
Servo 2 moved to: 100 degree
Servo 2 move to: 90 degree
Servo 2 move to: 80 degree
Servo 2 move to: 70 degree
Servo 2 move to: 60 degree
Servo 2 move to: 50 degree
Servo 2 move to: 40 degree
Servo 2 move to: 30 degree
Servo 2 move to: 20 degree
Servo 2 move to: 10 degree
Servo 2 move to: 0 degree
Servo 2 moved to: 10 degree
Servo 2 moved to: 20 degree
Servo 2 moved to: 30 degree
Servo 2 moved to: 40 degree
Servo 2 moved to: 50 degree
Servo 2 moved to: 60 degree
Servo 2 moved to: 70 degree
Servo 2 moved to: 80 degree
Servo 2 moved to: 90 degree
Servo 2 moved to: 100 degree
Same out put for Servo 1. Now I have a new issue in my code that the servos are not moving 180 and the servos are not starting at 90 degrees. I will include my new code for any input on any errors that you see. Thanks again for help. Your Help is greatly appreciated.

Code: Select all | TOGGLE FULL SIZE
/*
 * Controlling two servos with push buttons. When the left button is press
 * the servo moves left in 10 degree increments. When the right button is
 * press the servo moves right in 10 degree increments.
 */

 #include <Servo.h>

 Servo servo1;  // create servo object servo 1
 Servo servo2;  // creates servo object servo 2

 int angle =90;  // inital angle for servos
 int angleStep =10;  // increment to move servos

 #define S1LEFT 12  // pin 12 connected to servo 1 left button
 #define S1RIGHT 2  // pin 2 connected to servo 1 right button
 #define S2LEFT 8  // pin 8 connected to servo 2 left button
 #define S2RIGHT 4  // ping4 connected to servo 2 right button
 
void setup() {
  Serial.begin(9600);   // setup serial
  servo1.attach(9);  // attaches servo 1 to pin 9
  servo2.attach(10);  // attaches servo 2 to pin 10

  pinMode(S1LEFT,INPUT_PULLUP); // assigns pin 12 as input for servo 1 left button
  pinMode(S1RIGHT,INPUT_PULLUP);  // assigns pin 2 as input for servo 1 right button

  pinMode(S2LEFT,INPUT_PULLUP);  // assigns pin 8 as input for servo 2 left button
  pinMode(S2RIGHT,INPUT_PULLUP);  // assigns pin 4 as input for servo 2 right button

   servo1.write(angle);  // sends servo 1 to middle at 90 degress
   servo2.write(angle);  // semds servo 2 to middle at 90 degress
   Serial.println("Servo Button Test");

}

void loop() {
  while(digitalRead(S1RIGHT) == LOW){

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
      if(angle < 0){
        angle = 0;
      }else{
        servo1.write(angle);  // move servo 1 to desired angle
        Serial.print("Servo 1 move to: ");
        Serial.print(angle);  // print the angle
        Serial.println(" degree");
      }
    }

  delay(50);  // waits for servo to move to position 
  }// while

  while(digitalRead(S1LEFT) == LOW){

    if (angle >= 0 && angle + angle <=180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
      }else{
        servo1.write(angle);  // move servo 1 to desired angle
        Serial.print("Servo 1 moved to: ");
        Serial.print(angle);  // print the angle
        Serial.println(" degree");
        }
    }
  delay(50);  // waits for servo to get to desired location 
  }//while

   while(digitalRead(S2RIGHT) == LOW){

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
      if(angle < 0){
        angle = 0;
      }else{
        servo2.write(angle);  // move servo 2 to desired angle
        Serial.print("Servo 2 move to: ");
        Serial.print(angle);  // print the angle
        Serial.println(" degree");
      }
    }

  delay(50);  // waits for servo to move to position 
  }// while

  while(digitalRead(S2LEFT) == LOW){

    if (angle >= 0 && angle + angle <=180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
      }else{
        servo2.write(angle);  // move servo 2 to desired angle
        Serial.print("Servo 2 moved to: ");
        Serial.print(angle);  // print the angle
        Serial.println(" degree");
        }
    }
  delay(50);  // waits for servo to get to desired location
  }//

 
}
Attachments
servo2v3.ino.ino
(3.06 KiB) Downloaded 3 times
Last edited by adafruit_support_bill on Tue Feb 04, 2020 6:51 am, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

J_Cummings
 
Posts: 12
Joined: Sat Mar 10, 2018 5:14 pm

Please be positive and constructive with your questions and comments.