How do I make servo motor to turn both directions evenly?

Wearable electronics: boards, conductive materials, and projects from Adafruit!

Moderators: adafruit_support_bill, adafruit

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

How do I make servo motor to turn both directions evenly?

Post by animefruit »

Hello. This is Neal Cole. I am doing an Arduino project. My code basically makes a servo motor sweep one time after the sensors get touched enough. The problem though, is that the servo doesn't move evenly in both directions when it sweeps. Can you tell me how to make my servo motor sweep evenly in both directions when it sweeps?

Here is my code:

Code: Select all

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

// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);

Servo myservo;
const int knockSensor = A0;
//const int led = 13;
const int threshold = 10;
int health=500; // added this
int sensorReading = 0;

//int reading;
//int reading2;
int pos;
//const unsigned long eventInterval = 4000;
//unsigned long previousTime = 0;

void setup() 

{
 pinMode(LED_BUILTIN, OUTPUT);
  //pinMode(led, OUTPUT);
  //digitalWrite(led, HIGH);
  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
  // lcd.begin (16 ,2);

 lcd.begin (20 ,4);
  lcd.clear();
  
  // put your setup code here, to run once:
  Serial.begin(9600);
  myservo.attach(9); //servo at digital pin 9
 myservo.write(0); //initial point for servo
//myservo2.attach(11);
//myservo2.write(0);
}

void loop() 
{
do
  {
     
// unsigned long currentTime = millis();
    myservo.detach();
    //myservo2.detach();
   //-+delay(50);
    
    //reading=analogRead(A0);

    //reading = analogRead(A0); //attached to analog 0
    //reading2=analogRead(A1);
    //lcd.setCursor(0, 1);
  sensorReading = analogRead(knockSensor);
   
   //if(sensorReading < threshold){  //was 900
   //reading=0;
   //}
   lcd.setCursor(0, 1);
 Serial.println(health);
 lcd.println(health); 
   
   if (sensorReading > threshold) {
    //String packet = String("NP,") + millis() + String(',') + sensorReading;
    //Serial.println(packet);
    //digitalWrite(led, LOW);
    Serial.println("Knock!");
health=health-sensorReading;
 //health=health-reading-reading2;


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

    //Serial.print("Sensor Value=");

    int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);   
 
   }  
//delay(100);
  // if(reading2 < 800){
   //reading2=0;
   //}  

    /*if(reading > 0){   //was 900
  Serial.println(sensorReading);
  //delay(100);
  lcd.println(sensorReading);
  //if (currentTime - previousTime >= eventInterval) {
// reading>=280;
}
//if(reading2 > 800){
 // Serial.println(reading2);
 // lcd.println(reading2);
  
*/
//}
    /*
 Serial.print("Sensor Value=");
 health=health-sensorReading;
 //health=health-reading-reading2;

lcd.setCursor(0, 1);
 Serial.println(health);
 lcd.println(health); 
  
  */
  
   } while(health>1);
  digitalWrite(LED_BUILTIN, HIGH); 
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);             
myservo.attach(9);
//myservo2.attach(11);
  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(1);
 // myservo2.write(1);
  // sets the servo position according to the scaled value
  delay(1000);
  // waits for it to get to the position
  myservo.detach();
 // myservo2.detach();
  delay(6000);//how long it pauses
  myservo.attach(9);
  //myservo2.attach(11);
  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(179);
  //myservo2.write(179);
  // sets the servo position according to the scaled value
  delay(1000); // waits for it to get to the position
  myservo.detach();
  //myservo2.detach();
  delay(1000);
  health=500;
}    

I also tried to replace my previous code to make the servo sweep code with some of the code from the Sweep Example Sketch. But this just made my servo motor not sweep at all. Here is the code:

Code: Select all

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

// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);

Servo myservo;
const int knockSensor = A0;
//const int led = 13;
const int threshold = 10;
int health=500; // added this
int sensorReading = 0;

//int reading;
//int reading2;
int pos;
//const unsigned long eventInterval = 4000;
//unsigned long previousTime = 0;

void setup() 

{
 pinMode(LED_BUILTIN, OUTPUT);
  //pinMode(led, OUTPUT);
  //digitalWrite(led, HIGH);
  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
  // lcd.begin (16 ,2);

 lcd.begin (20 ,4);
  lcd.clear();
  
  // put your setup code here, to run once:
  Serial.begin(9600);
  myservo.attach(9); //servo at digital pin 9
 myservo.write(0); //initial point for servo
//myservo2.attach(11);
//myservo2.write(0);
}

void loop() 
{
do
  {
     
// unsigned long currentTime = millis();
    myservo.detach();
    //myservo2.detach();
   //-+delay(50);
    
    //reading=analogRead(A0);

    //reading = analogRead(A0); //attached to analog 0
    //reading2=analogRead(A1);
    //lcd.setCursor(0, 1);
  sensorReading = analogRead(knockSensor);
   
   //if(sensorReading < threshold){  //was 900
   //reading=0;
   //}
   lcd.setCursor(0, 1);
 Serial.println(health);
 lcd.println(health); 
   
   if (sensorReading > threshold) {
    //String packet = String("NP,") + millis() + String(',') + sensorReading;
    //Serial.println(packet);
    //digitalWrite(led, LOW);
    Serial.println("Knock!");
health=health-sensorReading;
 //health=health-reading-reading2;


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

    //Serial.print("Sensor Value=");

    int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);   
 
   }  
//delay(100);
  // if(reading2 < 800){
   //reading2=0;
   //}  

    /*if(reading > 0){   //was 900
  Serial.println(sensorReading);
  //delay(100);
  lcd.println(sensorReading);
  //if (currentTime - previousTime >= eventInterval) {
// reading>=280;
}
//if(reading2 > 800){
 // Serial.println(reading2);
 // lcd.println(reading2);
  
*/
//}
    /*
 Serial.print("Sensor Value=");
 health=health-sensorReading;
 //health=health-reading-reading2;

lcd.setCursor(0, 1);
 Serial.println(health);
 lcd.println(health); 
  
  */
  
   } while(health>1);
  digitalWrite(LED_BUILTIN, HIGH); 
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);             


//Begiinning of Sweep Example Sketch
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 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    //delay(15);  // waits 15 ms for the servo to reach the position
 //Ending of Sweep Example Sketch
   health=500;
  
  
  
  }
}    

Can you tell me how to make my servo motor move evenly in each direction when it sweeps? If so that would be great.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: How do I make servo motor to turn both directions evenly

Post by Franklin97355 »

What does the servo do? how fast does the servo move in each direction? Are you trying to use the code with the comment included or do you have another program? If you do please post it so we can test it.

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

Re: How do I make servo motor to turn both directions evenly

Post by animefruit »

I am not including the comments in my code. And what my servo does is that it moves both directions I think 360 degrees. If don't know how to find how fast the servo moves in each direction. If you know how to find out how fast a servo can move and you need to know this to figure my problem out, than it would be great if you tell me how.

Here is regular code without the code comments :

Code: Select all

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

// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);

Servo myservo;
const int knockSensor = A0;

const int threshold = 10;
int health=500; // added this
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 (16 ,2);

 lcd.begin (20 ,4);
  lcd.clear();
 
  // put your setup code here, to run once:
  Serial.begin(9600);
  myservo.attach(9); //servo at digital pin 9
 myservo.write(0); //initial point for servo

}

void loop()
{
do
  {
     

    myservo.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);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);   
 
   } 

   } while(health>1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);             
myservo.attach(9);

  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(1);
 
  // sets the servo position according to the scaled value
  delay(1000);
  // waits for it to get to the position
  myservo.detach();
 
  delay(6000);//how long it pauses
  myservo.attach(9);
  
  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(179);
  
  // sets the servo position according to the scaled value
  delay(1000); // waits for it to get to the position
  myservo.detach();
  
  delay(1000);
  health=500;
}   
    
Here is my other code with sweep without the code comments:

Code: Select all

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

// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);

Servo myservo;
const int knockSensor = A0;
//const int led = 13;
const int threshold = 10;
int health=500; // added this
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();
 
  // put your setup code here, to run once:
  Serial.begin(9600);
  myservo.attach(9); //servo at digital pin 9
 myservo.write(0); //initial point for servo

}

void loop()
{
do
  {
     

    myservo.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);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);   
 
   } 

 
   } while(health>1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);             


//Begiinning of Sweep Example Sketch
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'
                          
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    
   health=500;
 
 
 
  }
}   
     

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

Re: How do I make servo motor to turn both directions evenly

Post by animefruit »

Did you test out my code?

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

Re: How do I make servo motor to turn both directions evenly

Post by animefruit »

Hello. This is Neal.

That is ok that you didn’t test out my code.

I have a 4 AA battery pack connected to my Arduino and an lcd connected to my Arduino.

I have a problem.

My lcd is not displaying anything unless the Arduino is plugged into my computer.

And it also works after I take it out of the computer and have the battery pack on again without it being plugged in to the computer.

But it doesn’t when I turn the battery off and back on again.

Can you explain to me what could be going wrong?

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: How do I make servo motor to turn both directions evenly

Post by Franklin97355 »

Some of the problems are created when you have serial.begin and serial.print but no serial connection to the computer's serial port. Try commenting out the serial statements and see if it behaves betters

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

Re: How do I make servo motor to turn both directions evenly

Post by animefruit »

I actually already fixed that. it seems to work perfectly fine when I connect the battery to 5V rather than VIN.

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

Re: How do I make servo motor to turn both directions evenly

Post by animefruit »

Now I have another problem. I am trying to make 2 servo motors move in opposite directions.

I need them to move in opposite directions so that when they are facing each other they will go the same direction.

I looked up on the internet how to make 2 servos move in opposite directions.

I got the code from the very bottom of this page:

http://embeddedforus.blogspot.com/2016/ ... otors.html

I edited the code so that the servo motors on pin 9 and the servos motor for pin 10 move in opposite directions.

I also connected the yellow-ish cord to pin 10 for the servo that is supposed to be connected to pin 10.



Here is the code:

Code: Select all

   #include <Servo.h> 
#include "Wire.h"
Servo servoLeft; // Declare left servo signal
Servo servoRight; // Declare right servo signal

void setup() // Built in initialization block
{
servoLeft.attach(9); // Attach left signal to pin 13
servoRight.attach(10); // Attach right signal to pin 12

servoLeft.writeMicroseconds(1700); // 1.7 ms -> counterclockwise
servoRight.writeMicroseconds(1300); // 1.3 ms -> clockwise
}

void loop() // Main loop auto-repeats
{ // Empty, nothing needs repeating
}     


But the servo motor on pin 10 does not move!

Can you tell me why its not working or give me some code that will do the same without making my servo on pin 10 not work?

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

Re: How do I make servo motor to turn both directions evenly

Post by animefruit »

I sent a post at night time last week. I think that wasn't a great to post it that late at night.
So here is a repost in the day time:

Now I have another problem. I am trying to make 2 servo motors move in opposite directions.

I need them to move in opposite directions so that when they are facing each other they will go the same direction.

I looked up on the internet how to make 2 servos move in opposite directions.

I got the code from the very bottom of this page:

http://embeddedforus.blogspot.com/2016/ ... otors.html

I edited the code so that the servo motors on pin 9 and the servos motor for pin 10 move in opposite directions.

I also connected the yellow-ish cord to pin 10 for the servo that is supposed to be connected to pin 10.



Here is the code:

Code: Select all

   #include <Servo.h>
#include "Wire.h"
Servo servoLeft; // Declare left servo signal
Servo servoRight; // Declare right servo signal

void setup() // Built in initialization block
{
servoLeft.attach(9); // Attach left signal to pin 13
servoRight.attach(10); // Attach right signal to pin 12

servoLeft.writeMicroseconds(1700); // 1.7 ms -> counterclockwise
servoRight.writeMicroseconds(1300); // 1.3 ms -> clockwise
}

void loop() // Main loop auto-repeats
{ // Empty, nothing needs repeating
}           

But the servo motor on pin 10 does not move!

Can you tell me why its not working or give me some code that will do the same without making my servo on pin 10 not work?

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

Return to “Wearables”