Servo Motor Probem for making both Move Opposite ways

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

Hello. Can you give me some help?

I am trying to make 2 servo motors move opposite direction because they will be facing each other.


I ran this code that was worked.

But than all the sudden the other servo WAS NOT MOVING.

The only changes of the code that I did before this incident was that I changed the "delay(2000)" for both servos to "delay(1000)"

I commented on the changes in this code:



Code: Select all

         #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(0);

  myservo2.attach(10);
  myservo2.write(179);


}

void loop()
{
  do
  {


    myservo1.detach();
    myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);//was 2000
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(0);//was 0
  myservo2.write(179);//was 179

  delay(2000);  //before my servo stopped I changed this to 1000

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);

  myservo1.write(179);
  myservo2.write(0);

  delay(2000); //before my servo stopped I changed this to 1000

  myservo1.detach();
  myservo2.detach();

  delay(1000);

  health = 500;
}     


Do you know if this change could have caused the problem?

How do I fix this?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Motor Probem for making both Move Opposite ways

Post by adafruit_support_bill »

Do you know if this change could have caused the problem?
That seems unlikely, but it is simple enough to test: If you change it back to "delay(2000)", does it start working again?

If not, the problem is likely in your servo or wiring.

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

I just soldered another servo to replace it and now it works perfectly fine.

Now I want my servos to Turn LESS.

Do you know how I can change my code to make my servos Turn LESS?

And ,I did not notice this, but it looks like my servo on pin 9 is NOT turning opposite directions. Do you know how I can fix that?

Here is my code (which is the same code as last time):

Code: Select all

     #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(0);

  myservo2.attach(10);
  myservo2.write(179);//was 179




}

void loop()
{
  do
  {


    myservo1.detach();
    myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);//was 2000
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(0);//was 0
  myservo2.write(179);//was 179

  delay(2000);//was 2000

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);

  myservo1.write(179);//was 179
  myservo2.write(0);// was 0

  delay(2000); //was 2000

  myservo1.detach();
  myservo2.detach();

  delay(1000);

  health = 500;
}     

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Motor Probem for making both Move Opposite ways

Post by adafruit_support_bill »

To turn less, change the numbers you are sending to the servos:

Code: Select all

  myservo1.attach(9);
  myservo1.write(0);

  myservo2.attach(10);
  myservo2.write(179);//was 179
Use a number higher than 0 for myservo1..

Use a number lower than 179 for myservo2.

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

So I tried changing these numbers that you said for me to change and the servos didn't turn less:

Code: Select all

    myservo1.attach(9);
  myservo1.write(10);//was 0

  myservo2.attach(15);
  myservo2.write(50);//was 179   
I also tried changing the numbers in the bottom of the code here and it still did not turn less:
(In one try I kept the code as above and the other try I changed it back to the way it was)

Code: Select all

  myservo1.write(15);//was 0
  myservo2.write(50);//was 179

  delay(2000);//was 2000

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);

  myservo1.write(50);//was 179
  myservo2.write(15);// was 0     
Is there a difference between the top code that you told me to change and the bottom code that I changed?

Do I change both codes or just one of them, and if so which one do I change?

And if non of that works what do I do?

Here is my code( I will have quotes where I changed things in the code):

Code: Select all

  #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(15);// This is the top part you told me to change to make my servos turn less

  myservo2.attach(15);
  myservo2.write(50); //This is the other top part you told me to change to make my servos turn less




}

void loop()
{
  do
  {


    myservo1.detach();
    myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(15);// This is the other bottom part I changed to make my servos turn less
  myservo2.write(50);//This is the other  bottom part I changed to make my servos turn less

  delay(2000);//was 2000

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);

  myservo1.write(50);//This is the other bottom part I changed to make my servos turn less
  myservo2.write(15);//This is the other bottom part I changed to make my servos turn less

  delay(2000); //was 2000

  myservo1.detach();
  myservo2.detach();

  delay(1000);

  health = 500;
}   

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Motor Probem for making both Move Opposite ways

Post by adafruit_support_bill »

What kind of servos are you using and what are they connected to? Please post some photos of your project.

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

The kind of servo I ordered are these
“ Feetech FS90R 360 Degree Continuous Rotation Micro Servo Motors”. I got them from Amazon here:
https://www.amazon.com/your-orders/pop/ ... =UkN2MmZPB

And everything is connected to my Arduino Nano on a soldering board.

This includes the 5V battery pack that is attached to it, the lcd screen attached to it, the 5 pezio sensors that are attached to it, and the 3 servo motors attached to it.

Here are some pictures:
Attachments
3BE699BE-A247-41BF-91D8-0D2837F3993C.jpeg
3BE699BE-A247-41BF-91D8-0D2837F3993C.jpeg (764.37 KiB) Viewed 662 times

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

Another photo.
Attachments
FD6F748C-21E0-4721-8C9A-C05CE4884F41.jpeg
FD6F748C-21E0-4721-8C9A-C05CE4884F41.jpeg (260.42 KiB) Viewed 662 times
E87F6E4D-5BB6-4627-9F9C-77C6E1AC14D2.jpeg
E87F6E4D-5BB6-4627-9F9C-77C6E1AC14D2.jpeg (260.42 KiB) Viewed 662 times
E46315FB-7DDA-45CA-BDE8-2673A61CF31F.jpeg
E46315FB-7DDA-45CA-BDE8-2673A61CF31F.jpeg (543.43 KiB) Viewed 662 times

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

Another Picture
Attachments
3483B816-225A-48F2-93B7-6F8F9EE12E0D.jpeg
3483B816-225A-48F2-93B7-6F8F9EE12E0D.jpeg (158.15 KiB) Viewed 660 times

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Motor Probem for making both Move Opposite ways

Post by adafruit_support_bill »

So I tried changing these numbers that you said for me to change and the servos didn't turn less:
With continuous rotation servos like those, speed and direction are controlled by the number you write to the servo:
* Numbers higher than 90 will turn in one direction.
* Numbers lower than 90 will turn in the other direction.
* Numbers closer to 90 will be slow.
* Numbers farther from 90 will be fast.

To turn slowly in opposite directions try:

Code: Select all

  myservo1.attach(9);
  myservo1.write(100);

  myservo2.attach(15);
  myservo2.write(80);

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

This top part doesn't seem to affect much when I change it to 80 and 100. I don't think this part tells the servos to turn, correct me if I am wrong but I think this part tells how much it is limited to turning on the servos:

Code: Select all

 Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(100);//was 0 // changing this to 100 seems to not do anything

  myservo2.attach(10);
  myservo2.write(80);//was 179// changing this to 80 seems to not do anything
      
I changed this part of the code to 80 for servo 1 and 100 for servo 2 and than switched it:

Code: Select all

  delay(15);
  myservo1.write(80);//was 0        // I changed this based on what you said
  myservo2.write(100);//was 179    

  delay(2000);//was 2000

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);

  myservo1.write(100);//was 179   // And I changed this based on what you said
  myservo2.write(80);// was 0     
    
When I changed this part of the code all of them turned at the same time except one of the servos that was connected to pin 9. The servo on pin 9 barely turned when all my servos turned in the first turn. And than then in the second turn all the servos moved at the same time.


Here is my entire code again with comments to tell you what I changed:

Code: Select all

  #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(100);//was 0 // changing this to 100 seems to not do anything

  myservo2.attach(10);
  myservo2.write(80);//was 179// changing this to 80 seems to not do anything




}

void loop()
{
  do
  {


    myservo1.detach();
    myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);//was 2000
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(80);//was 0        // I changed this based on what you said
  myservo2.write(100);//was 179    

  delay(2000);//was 2000

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);

  myservo1.write(100);//was 179   // And I changed this based on what you said
  myservo2.write(80);// was 0     

  delay(2000); //was 2000

  myservo1.detach();
  myservo2.detach();

  delay(1000);

  health = 500;
}          

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Motor Probem for making both Move Opposite ways

Post by adafruit_support_bill »

This top part doesn't seem to affect much when I change it to 80 and 100. I don't think this part tells the servos to turn, correct me if I am wrong but I think this part tells how much it is limited to turning on the servos:
That code only runs once at startup. It tells the servos to move, but you immediately call detach at the start of your loop, so the servos probably don't have time to fully move to position.
When I changed this part of the code all of them turned at the same time except one of the servos that was connected to pin 9.
What do you mean by 'all of them except the one connected to pin 9'? You only have code for 2 servos.

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

What I meant about the one servo connected to pin 9 is that there is one physically connected to pin 9 and the other 2 servos are connected to pin 10.

The 2 servos in the code are just the 2 pins I use for my servos.

I didn't know that detach would make it to where it doesn't have time to turn the servos in the top of my code. That is great advice.

Do you think I should take off the detaches in the top of my code ?

Or should I do something else to the detaches in the top of my code?

Where would I put the detaches if I wanted them to NOT be immediately at the start of my loop?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Motor Probem for making both Move Opposite ways

Post by adafruit_support_bill »

Detach is what you call when you want to stop controlling the servo. Most servos will stop moving as soon as you call detach. It is not clear what your loop code is intended to do. But if you want the motors to move at startup, you need to give them time to move.

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

Re: Servo Motor Probem for making both Move Opposite ways

Post by animefruit »

Here is what my loop code is intended to do:
.There is supposed to be number that the program starts out with
.Than any time the piezo sensors are pressed, the number of the pressure on any of the piezo sensors should subtract from the original number(I label the original number as "health" in my code).

.Than when the original number goes down to zero the servos are supposed to sweep
. 2 of the servos should sweep the same directions while the other servo should sweep the opposite direction

These servos are supposed to pull a blind down so we need to make the servos turn a little less than they did with the original code to make the blinds not pull down to far

I took off some of the detaches in the code like you said but each one I took off did not help with the servos turning less

I have the details of how the servos did not work for each detach I took off in my new comments in my code:

Code: Select all

   #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(0);//was 0

  myservo2.attach(10);
  myservo2.write(179);//was 179




}

void loop()
{
  do
  {


    myservo1.detach();  // if I undo these detaches the servos 
    myservo2.detach();  //only move in one direction forever

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);//was 2000
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(15);//was 0
  myservo2.write(50);//was 179

  delay(2000);//was 2000

  myservo1.detach();  //if I undo these detaches the servos only go 
  myservo2.detach();  //in one direcction and than they stop and restarts the health(original number)


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);

  myservo1.write(50);//was 179
  myservo2.write(15);// was 0

  delay(2000); //was 2000

  //myservo1.detach(); // When I take these detaches out:
                        //the servos turn
  //myservo2.detach();  //than they stop 
                        //but than they turn in the same exact direction the second time but also turn MORE
                        //but what I want is for them to turn in the opposite direction or in other words to SWEEP
  delay(1000);

  health = 500;
}    

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”