Servomotor stop at actual position problem

Hi to all of you that read this topic.

I am an MSc. Electronics that is being involved in one project of production of cheap robotic hand for academic and further use and i have run in some issues when doing this.

I have a problem in my code so far, because i don't know how to stop the servomotor at its actual position when it is moving from 0 degree to 100 degree (minimum maximum position). I have read a lot on the internet and i have not found a way so far to find a command that stops the servomotor at its actual position. This position will be trigered from one of 2 preasure sensors that will detect the exact preasure and than sand the motor the command to do so. This in short is my problem. Please help if possible because i am late in the project deadline. Below you can find the actual code so far.:

//#include <Adafruit_PWMServoDriver.h> //include Adafruit servomotor library
#include <Servo.h> //include servomotor library
Servo Servo1; //Define servomotor name Servo1
const int stepsPerRevolution = 60;
int pos = 0; //Start position of servomotor
#define fsrpin1 A0 //Define preasure sensor1 input pin
#define fsrpin2 A1 //Define preasure sensor2 input pin
#define thermistor_pin A2
#define thermistor_analog_read_pin A3
int ledpin1 = 7; // Define Led pin d7
int ledpin2 = 8; // Define Led pin d8
int ledpin3 = 9; // Define Led pin d9
int fsrreading1; // The current reading from the FSR1
int fsrreading2; // The current reading from the FSR2
int state2 = HIGH; // The current state of the output pin
int state3 = HIGH; // The current state of the output pin
int previous1 = 0; // The previous reading from the FSR
int previous2 = 0; // The previous reading from the FSR
int feedbackPin = A5; // FEEDBAKPIN OF SERVOMOTOR AS INPUT
int val;
long time = 0; // The last time the output pin was toggled
long debounce = 40; // The debounce time, increase if the output flickers
double celcius_thermistor_temperature;

void setup() {
  Serial.begin(9600); // initiate serial monitor
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  pinMode(ledpin3, OUTPUT);
  pinMode(fsrreading1, INPUT);
  pinMode(fsrreading2, INPUT);
  pinMode(thermistor_pin, OUTPUT);
  pinMode(thermistor_analog_read_pin, INPUT);
  pinMode(10, INPUT); // define power up pin. The pin that makes the robotic hand move.
  Servo1.attach(6); // Attach Servo1 to pin 6
}

void loop() {
  val = analogRead(feedbackPin);
  Serial.println("---------");
  Serial.println(val);
  val = map(val, 46, 390, 0, 100);
  if (digitalRead(10) == LOW) {
    digitalWrite (ledpin1, HIGH);
    Servo1.write(100);   // Make servo go to move maximum 100 grade.
    delay(500);
    if (fsrreading1 >= 20 || fsrreading2 >= 20 ) {
      //digitalWrite (ledpin1, HIGH);
      //Servo1.write(val);    // Make servo to stop
      //Servo1.write(pos);
      pos = pos;
      fsrreading1 = analogRead(fsrpin1);
      fsrreading2 = analogRead(fsrpin2);
      if (fsrreading1 < previous1 - 8 || fsrreading2 < previous2 - 8) {
        //digitalWrite (ledpin1, HIGH);
        //val = val + 1; //Make servo move to (current value of val)+1
        //Servo1.write(val+1);
        //pos = val;
        fsrreading1 = analogRead(fsrpin1);
        fsrreading2 = analogRead(fsrpin2);
        previous1 = fsrreading1;
        previous2 = fsrreading2;
        delay(50);
      }
    }
  }
  if(pos < 0)
    {
      // sigurohu qe nuk po e hapim doren me shum se vlera maksimale 
      // e hapjes prej 0 grad
      pos = 0;
    }
    else if(pos > 100)
    {
      // sigurohu qe nuk po e mbyllim doren me shum se vlera maksimale e 
      // mbylljes prej 180 grad
      pos = 100;
    }
  //else
  //{
    //digitalWrite (ledpin1, LOW);//Make servo go to position 0 (start position)
    //Servo1.write(0);
    //delay(500);
  //}
  fsrreading1 = analogRead(fsrpin1);
  fsrreading2 = analogRead(fsrpin2);
  Serial.println(fsrreading1);
  Serial.println(fsrreading2);
  if (fsrreading1 > 500 && previous1 < 500 && millis() - time > debounce) {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;
    time = millis();
  }
  digitalWrite(ledpin2, state2);
  previous1 = fsrreading1;
  if (fsrreading2 > 500 && previous2 < 500 && millis() - time > debounce) {
    if (state3 == HIGH)
      state3 = LOW;
    else
      state3 = HIGH;
    time = millis();
  }
  digitalWrite(ledpin3, state3);
  previous2 = fsrreading2;

  charge_input_capacitor();
  int raw_thermistor_reading = read_thermistor_data();
  double unfiltered_resistance = calculate_resistance_from_analog_read(raw_thermistor_reading);
  double filtered_resistance = filter_resistance_reading(unfiltered_resistance);
  float kelvin_thermistor_temperature = calculate_temperature_from_resistance(filtered_resistance);
  float celcius_thermistor_temperature = kelvin_thermistor_temperature - 273.15;
  float fahrenheit_thermistor_temperature = ((celcius_thermistor_temperature) * 9 / 5.0) + 32.0;
  Serial.println(celcius_thermistor_temperature);
  delay(900);
  if (celcius_thermistor_temperature >= 29) {
    digitalWrite(ledpin2, HIGH);
    Servo1.write(0);
    delay(10000);
  }
}


void charge_input_capacitor() {
  digitalWrite(thermistor_pin, HIGH);
  delay(100);
}
int read_thermistor_data() {
  int raw_thermistor_reading = analogRead(thermistor_analog_read_pin);
  digitalWrite(thermistor_pin, LOW);
  return raw_thermistor_reading;
}
double calculate_resistance_from_analog_read(int raw_thermistor_reading) {
  double voltage_divider_resistor = 10000.0;
  if (raw_thermistor_reading >= 1023) {
    return 999999.9;
  }
  double unfiltered_resistance = voltage_divider_resistor * (raw_thermistor_reading / (1023.0 - raw_thermistor_reading));
  return unfiltered_resistance;
}
double filter_resistance_reading(double unfiltered_resistance) {
  double calibration_factor = -3.27487396E-07 * unfiltered_resistance + 8.25744292E-03;
  double filtered_resistance = unfiltered_resistance * (1 + calibration_factor);
  return filtered_resistance;
}
float calculate_temperature_from_resistance(double thermistor_resistance) {
  double SH_A_constant = 1.21500454194620E-03;
  double SH_B_constant = 2.05334949463842E-04;
  double SH_C_constant = 3.19176316497180E-06;
  double SH_D_constant = -2.93752010251114E-08;
  double natural_log_of_resistance = log(thermistor_resistance);
  float thermistor_temperature_kelvin = 1 / ( SH_A_constant + SH_B_constant * natural_log_of_resistance + SH_C_constant * pow(natural_log_of_resistance, 2) + SH_D_constant * pow(natural_log_of_resistance, 3));
  return thermistor_temperature_kelvin;
}
````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

But.... what is the "actual" position of a servo? I've never heard that term applied to a servo before.

Welcome to the forum

A servo will stop at its commanded position, however, if you have a continuous rotation "servo" then you cannot control its position, only its speed and direction. Which do you have ?

Please describe what happens when you try the Servo Sweep example in the IDE

The idea is that when the servo is moving from 0 degree to 100 degree at one precise moment for exxample at 30 degree(cant be precisely sad) the preasure sensor must command the servomotor to stop wich means that the object of interest is being grabbed from the robotic hand and now is the moment that the servomotor stops at its actual precize moment for further use
Just for that i have added a feedback pin from the potentiometer of the servomotor that i want to use to detect the current position from the servomotor and in some way to use to stop the servomotor. I dont know if i am clear for understanding.

You should have mentioned that in your first post. Please add any other hidden important information about your project now, in a new message.

i dont know but i have
Tower pro MG946R servomotor

Tell me about the first pass through loop.

So, is it the case, you have no code for this yet, and you want help to write it?

Oh, that is hardly important at all so no wonder you didn't mention it originally

This command means that if the preasure sensor one is detecting 20 or more than 20 in preasure or preasure sensor 2 is detecting 20 or more than 20 than the idea is to stop the servomotor but i dont know how

If your English is not good, please post schematics and diagrams, photos to help us understand what you are trying to do. Wait, do that even if your English is good...

At the moment, your project is extremely unclear and a mystery to anyone who can't stand beside you and see it.




That is a good start. We will need some wiring diagrams to make sense of the connections... I assume you already have those? There are still some missing pieces of info... still no idea about the pressure sensors.

...but you haven't measured the pressure yet
That's why I asked about the first pass through loop

you are right i was going to move down the code after measuring the preasure and temperature first

The preasure sensors are working flawlesly and the temperature sensor is working flawlesly for the moment i dont have problems with that part of the code just for clarification

At final stage the 2 preasure sensors will be mounted on 2 fingers at top for making the measurement of the preasure of the object being held and the temperature sensor too.

I updated the code by making the preasure and temperature measure first like below:

//#include <Adafruit_PWMServoDriver.h> //include Adafruit servomotor library
#include <Servo.h> //include servomotor library
Servo Servo1; //Define servomotor name Servo1
const int stepsPerRevolution = 60;
int pos = 0; //Start position of servomotor
#define fsrpin1 A0 //Define preasure sensor1 input pin
#define fsrpin2 A1 //Define preasure sensor2 input pin
#define thermistor_pin A2
#define thermistor_analog_read_pin A3
int ledpin1 = 7; // Define Led pin d7
int ledpin2 = 8; // Define Led pin d8
int ledpin3 = 9; // Define Led pin d9
int fsrreading1; // The current reading from the FSR1
int fsrreading2; // The current reading from the FSR2
int state2 = HIGH; // The current state of the output pin
int state3 = HIGH; // The current state of the output pin
int previous1 = 0; // The previous reading from the FSR
int previous2 = 0; // The previous reading from the FSR
int feedbackPin = A5; // FEEDBAKPIN OF SERVOMOTOR AS INPUT
int val;
long time = 0; // The last time the output pin was toggled
long debounce = 40; // The debounce time, increase if the output flickers
double celcius_thermistor_temperature;

void setup() {
  Serial.begin(9600); // initiate serial monitor
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  pinMode(ledpin3, OUTPUT);
  pinMode(fsrreading1, INPUT);
  pinMode(fsrreading2, INPUT);
  pinMode(thermistor_pin, OUTPUT);
  pinMode(thermistor_analog_read_pin, INPUT);
  pinMode(10, INPUT); // define power up pin. The pin that makes the robotic hand move.
  Servo1.attach(6); // Attach Servo1 to pin 6
}

void loop() {
  val = analogRead(feedbackPin);
  Serial.println("---------");
  
  digitalWrite(ledpin2, state2);
  previous1 = fsrreading1;
  if (fsrreading2 > 500 && previous2 < 500 && millis() - time > debounce) {
    if (state3 == HIGH)
      state3 = LOW;
    else
      state3 = HIGH;
    time = millis();
  }
  digitalWrite(ledpin3, state3);
  previous2 = fsrreading2;

  charge_input_capacitor();
  int raw_thermistor_reading = read_thermistor_data();
  double unfiltered_resistance = calculate_resistance_from_analog_read(raw_thermistor_reading);
  double filtered_resistance = filter_resistance_reading(unfiltered_resistance);
  float kelvin_thermistor_temperature = calculate_temperature_from_resistance(filtered_resistance);
  float celcius_thermistor_temperature = kelvin_thermistor_temperature - 273.15;
  float fahrenheit_thermistor_temperature = ((celcius_thermistor_temperature) * 9 / 5.0) + 32.0;
  Serial.println(celcius_thermistor_temperature);
  delay(900);
  if (celcius_thermistor_temperature >= 29) {
    digitalWrite(ledpin2, HIGH);
    Servo1.write(0);
    delay(10000);
  }
  Serial.println(val);
  val = map(val, 46, 390, 0, 100);
  if (digitalRead(10) == LOW) {
    digitalWrite (ledpin1, HIGH);
    Servo1.write(100);   // Make servo go to move maximum 100 grade.
    delay(500);
    if (fsrreading1 >= 20 || fsrreading2 >= 20 ) {
      //digitalWrite (ledpin1, HIGH);
      //Servo1.write(val);    // Make servo to stop
      //Servo1.write(pos);
      pos = pos;
      fsrreading1 = analogRead(fsrpin1);
      fsrreading2 = analogRead(fsrpin2);
      if (fsrreading1 < previous1 - 8 || fsrreading2 < previous2 - 8) {
        //digitalWrite (ledpin1, HIGH);
        //val = val + 1; //Make servo move to (current value of val)+1
        //Servo1.write(val+1);
        //pos = val;
        fsrreading1 = analogRead(fsrpin1);
        fsrreading2 = analogRead(fsrpin2);
        previous1 = fsrreading1;
        previous2 = fsrreading2;
        delay(50);
      }
    }
  }
  if(pos < 0)
    {
      // sigurohu qe nuk po e hapim doren me shum se vlera maksimale 
      // e hapjes prej 0 grad
      pos = 0;
    }
    else if(pos > 100)
    {
      // sigurohu qe nuk po e mbyllim doren me shum se vlera maksimale e 
      // mbylljes prej 180 grad
      pos = 100;
    }
  //else
  //{
    //digitalWrite (ledpin1, LOW);//Make servo go to position 0 (start position)
    //Servo1.write(0);
    //delay(500);
  //}
  fsrreading1 = analogRead(fsrpin1);
  fsrreading2 = analogRead(fsrpin2);
  Serial.println(fsrreading1);
  Serial.println(fsrreading2);
  if (fsrreading1 > 500 && previous1 < 500 && millis() - time > debounce) {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;
    time = millis();
  }
}


void charge_input_capacitor() {
  digitalWrite(thermistor_pin, HIGH);
  delay(100);
}
int read_thermistor_data() {
  int raw_thermistor_reading = analogRead(thermistor_analog_read_pin);
  digitalWrite(thermistor_pin, LOW);
  return raw_thermistor_reading;
}
double calculate_resistance_from_analog_read(int raw_thermistor_reading) {
  double voltage_divider_resistor = 10000.0;
  if (raw_thermistor_reading >= 1023) {
    return 999999.9;
  }
  double unfiltered_resistance = voltage_divider_resistor * (raw_thermistor_reading / (1023.0 - raw_thermistor_reading));
  return unfiltered_resistance;
}
double filter_resistance_reading(double unfiltered_resistance) {
  double calibration_factor = -3.27487396E-07 * unfiltered_resistance + 8.25744292E-03;
  double filtered_resistance = unfiltered_resistance * (1 + calibration_factor);
  return filtered_resistance;
}
float calculate_temperature_from_resistance(double thermistor_resistance) {
  double SH_A_constant = 1.21500454194620E-03;
  double SH_B_constant = 2.05334949463842E-04;
  double SH_C_constant = 3.19176316497180E-06;
  double SH_D_constant = -2.93752010251114E-08;
  double natural_log_of_resistance = log(thermistor_resistance);
  float thermistor_temperature_kelvin = 1 / ( SH_A_constant + SH_B_constant * natural_log_of_resistance + SH_C_constant * pow(natural_log_of_resistance, 2) + SH_D_constant * pow(natural_log_of_resistance, 3));
  return thermistor_temperature_kelvin;
}
````````````````````````````````````

Sorry i made some mistakes on previous code now i am uploading the modified one

//#include <Adafruit_PWMServoDriver.h> //include Adafruit servomotor library
#include <Servo.h> //include servomotor library
Servo Servo1; //Define servomotor name Servo1
const int stepsPerRevolution = 60;
int pos = 0; //Start position of servomotor
#define fsrpin1 A0 //Define preasure sensor1 input pin
#define fsrpin2 A1 //Define preasure sensor2 input pin
#define thermistor_pin A2
#define thermistor_analog_read_pin A3
int ledpin1 = 7; // Define Led pin d7
int ledpin2 = 8; // Define Led pin d8
int ledpin3 = 9; // Define Led pin d9
int fsrreading1; // The current reading from the FSR1
int fsrreading2; // The current reading from the FSR2
int state2 = HIGH; // The current state of the output pin
int state3 = HIGH; // The current state of the output pin
int previous1 = 0; // The previous reading from the FSR
int previous2 = 0; // The previous reading from the FSR
int feedbackPin = A5; // FEEDBAKPIN OF SERVOMOTOR AS INPUT
int val;
long time = 0; // The last time the output pin was toggled
long debounce = 40; // The debounce time, increase if the output flickers
double celcius_thermistor_temperature;

void setup() {
  Serial.begin(9600); // initiate serial monitor
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  pinMode(ledpin3, OUTPUT);
  pinMode(fsrreading1, INPUT);
  pinMode(fsrreading2, INPUT);
  pinMode(thermistor_pin, OUTPUT);
  pinMode(thermistor_analog_read_pin, INPUT);
  pinMode(10, INPUT); // define power up pin. The pin that makes the robotic hand move.
  Servo1.attach(6); // Attach Servo1 to pin 6
}

void loop() {
  
  Serial.println("---------");
  fsrreading1 = analogRead(fsrpin1);
  fsrreading2 = analogRead(fsrpin2);
  Serial.println(fsrreading1);
  Serial.println(fsrreading2);
  if (fsrreading1 > 500 && previous1 < 500 && millis() - time > debounce) {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;
    time = millis();
  }
  digitalWrite(ledpin2, state2);
  previous1 = fsrreading1;
  if (fsrreading2 > 500 && previous2 < 500 && millis() - time > debounce) {
    if (state3 == HIGH)
      state3 = LOW;
    else
      state3 = HIGH;
    time = millis();
  }
  digitalWrite(ledpin3, state3);
  previous2 = fsrreading2;

  charge_input_capacitor();
  int raw_thermistor_reading = read_thermistor_data();
  double unfiltered_resistance = calculate_resistance_from_analog_read(raw_thermistor_reading);
  double filtered_resistance = filter_resistance_reading(unfiltered_resistance);
  float kelvin_thermistor_temperature = calculate_temperature_from_resistance(filtered_resistance);
  float celcius_thermistor_temperature = kelvin_thermistor_temperature - 273.15;
  float fahrenheit_thermistor_temperature = ((celcius_thermistor_temperature) * 9 / 5.0) + 32.0;
  Serial.println(celcius_thermistor_temperature);
  delay(900);
  if (celcius_thermistor_temperature >= 29) {
    //digitalWrite(ledpin2, HIGH);
    Servo1.write(0);
    delay(10000);
  }
  val = analogRead(feedbackPin);
  Serial.println(val);
  val = map(val, 46, 390, 0, 100);
  if (digitalRead(10) == LOW) {
    digitalWrite (ledpin1, HIGH);
    Servo1.write(100);   // Make servo go to move maximum 100 grade.
    delay(500);
    if (fsrreading1 >= 20 || fsrreading2 >= 20 ) {
      //digitalWrite (ledpin1, HIGH);
      Servo1.write(val);    // Make servo to stop
      //Servo1.write(pos);
      //pos = pos;
      //fsrreading1 = analogRead(fsrpin1);
      //fsrreading2 = analogRead(fsrpin2);
      if (fsrreading1 < previous1 - 8 || fsrreading2 < previous2 - 8) {
        //digitalWrite (ledpin1, HIGH);
        //val = val + 1; //Make servo move to (current value of val)+1
        Servo1.write(val+1);
        //pos = val;
        //fsrreading1 = analogRead(fsrpin1);
        //fsrreading2 = analogRead(fsrpin2);
        //previous1 = fsrreading1;
        //previous2 = fsrreading2;
        delay(50);
      }
    }
  }
  if(pos < 0)
    {
      // sigurohu qe nuk po e hapim doren me shum se vlera maksimale 
      // e hapjes prej 0 grad
      Servo1.write(0);
    }
    else if(pos > 100)
    {
      // sigurohu qe nuk po e mbyllim doren me shum se vlera maksimale e 
      // mbylljes prej 180 grad
      Servo1.write(100);
    }
  //else
  //{
    //digitalWrite (ledpin1, LOW);//Make servo go to position 0 (start position)
    //Servo1.write(0);
    //delay(500);
  //}
  fsrreading1 = analogRead(fsrpin1);
  fsrreading2 = analogRead(fsrpin2);
  Serial.println(fsrreading1);
  Serial.println(fsrreading2);
  if (fsrreading1 > 500 && previous1 < 500 && millis() - time > debounce) {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;
    time = millis();
  }
}


void charge_input_capacitor() {
  digitalWrite(thermistor_pin, HIGH);
  delay(100);
}
int read_thermistor_data() {
  int raw_thermistor_reading = analogRead(thermistor_analog_read_pin);
  digitalWrite(thermistor_pin, LOW);
  return raw_thermistor_reading;
}
double calculate_resistance_from_analog_read(int raw_thermistor_reading) {
  double voltage_divider_resistor = 10000.0;
  if (raw_thermistor_reading >= 1023) {
    return 999999.9;
  }
  double unfiltered_resistance = voltage_divider_resistor * (raw_thermistor_reading / (1023.0 - raw_thermistor_reading));
  return unfiltered_resistance;
}
double filter_resistance_reading(double unfiltered_resistance) {
  double calibration_factor = -3.27487396E-07 * unfiltered_resistance + 8.25744292E-03;
  double filtered_resistance = unfiltered_resistance * (1 + calibration_factor);
  return filtered_resistance;
}
float calculate_temperature_from_resistance(double thermistor_resistance) {
  double SH_A_constant = 1.21500454194620E-03;
  double SH_B_constant = 2.05334949463842E-04;
  double SH_C_constant = 3.19176316497180E-06;
  double SH_D_constant = -2.93752010251114E-08;
  double natural_log_of_resistance = log(thermistor_resistance);
  float thermistor_temperature_kelvin = 1 / ( SH_A_constant + SH_B_constant * natural_log_of_resistance + SH_C_constant * pow(natural_log_of_resistance, 2) + SH_D_constant * pow(natural_log_of_resistance, 3));
  return thermistor_temperature_kelvin;
}