I have a servo motor and two force sensors that can provide values from 0 to 1023.
I would like to make the servo spin clockwise when I press the first force sensor and to spin counterclockwise when I press the second sensor.
I use map() to convert the sensor value into the servo range and it seems to work.
The problem is that the servo motion is not smooth enough and it seems jerky.
I would like to make the servo slowly increase its position as long as I keep the pression on the force sensor while now it only moves based on the amount of pression.
In addition, when I press the other sensor, the servo considers the new position starting from the center position and not its current position.
How can I solve this problem? Do you have any suggestions?
Thank you!
#include <SoftwareSerial.h>
SoftwareSerial arm(11, 10); // RX, TX
int range = 500;
int time = 2000;
int t_delay = 80;
int reading = 0;
int prev_reading = 0;
int value = 0;
int reading_up = 0;
void setup() {
Serial.begin(9600);
arm.begin(9600);
Serial.println("Laparoscopic Arm - Console");
}
void loop(void) {
reading = analogRead(A1); //attached to analog 0
reading_up = analogRead(A0);
Serial.print("Sensor value down = ");
Serial.println(reading);
Serial.print("Sensor value up = ");
Serial.println(reading_up);
if (reading > 5 && reading_up < 5){
value = map(reading, 6, 1023, 1480, 2200);
Serial.print("Sono in reading_down: ");
Serial.println(reading);
if (value > prev_reading){
move1(18, value, range, time);
prev_reading = value;
}
else {
move1(18, prev_reading, range, time);
}
} else if (reading < 5 && reading_up > 5){
Serial.print("Sono in reading_up: ");
Serial.println(reading_up);
value = map(reading_up, 6, 1023, 1480, 1000);
if (value < prev_reading){
move1(18, value, range, time);
prev_reading = value;
}
else {
move1(18, prev_reading, range, time);
}
}
delay(1000);
}
void move1(int servo, int position, int micro, int timer) {
arm.print("#");
arm.print(servo);
arm.print(" P");
arm.print(position);
arm.print(" S");
arm.print(micro);
arm.print(" T");
arm.println(timer);
Serial.print("#");
Serial.print(servo);
Serial.print(" position:");
Serial.println(position);
delay(timer);
}