It's Holiday Sale Weekend at Adafruit - use the code ADATY to get 15% off items storewide! Some restrictions apply.
0

How do I make servo motor to turn both directions evenly?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

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

by animefruit on Sun Sep 26, 2021 10:17 pm

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 | TOGGLE FULL SIZE
#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 | TOGGLE FULL SIZE
   #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.

animefruit
 
Posts: 201
Joined: Tue Feb 25, 2020 1:04 pm

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

by franklin97355 on Mon Sep 27, 2021 1:44 am

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.

franklin97355
 
Posts: 22318
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

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

by animefruit on Sat Oct 02, 2021 12:42 am

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 | TOGGLE FULL SIZE
   #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 | TOGGLE FULL SIZE
  #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;
 
 
 
  }
}   
     

animefruit
 
Posts: 201
Joined: Tue Feb 25, 2020 1:04 pm

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

by animefruit on Wed Oct 27, 2021 7:20 pm

Did you test out my code?

animefruit
 
Posts: 201
Joined: Tue Feb 25, 2020 1:04 pm

Please be positive and constructive with your questions and comments.