Hi!
I have a project that needs a servo that rotates 0 - 360 degrees and has precise position control. Control would have to be by the Arduino. Is there any way to perform this code control?
I kind of managed to control it using a potentiometer through some code I created, but it’s not accurate. Also, the project requires me to discard the potentiometer and use the serial port to perform position control.
I will share my code if anyone is interested in helping.
Is there any way to perform this code-only control? If not, is there another way to perform position control?
Code:
#include <Servo.h>
Servo myservo;
int receber = 0;
const int servoStop = 90;
const int servoSpeed = 50;
int potVal= 0;
int potStop= 0;
int servoStart;
int flag1 =0;
int flag2= 0;
int mapear;
int entreiaqui = 0;
void setup() {
myservo.attach(6);
}
void loop() {
receber = analogRead(A0);
potVal = map (receber, 0, 1023, 0, 5400);
if (potVal > potStop) {
int potSpeed = potStop - potVal;
servoStart = servoStop + (servoSpeed + potSpeed);
if(servoStart < 0 || servoStart > 180)
{
entreiaqui = entreiaqui + 1;
}
else{
//mapear = map(servoStart, -5260, 140, 0, 180);
myservo.write(servoStart);
}
flag1 = flag1 + 1;
}
else if (potVal < potStop) {
int potSpeed = potStop - potVal;
servoStart = servoStop - (servoSpeed - potSpeed);
if(servoStart < 0 || servoStart > 180)
{
entreiaqui = entreiaqui + 1;
}
//mapear = map(servoStart, -5260, 140, 0, 180);
myservo.write(servoStart);
flag2 = flag2 + 1;
}
else myservo.write(servoStop);
potStop = potVal;
//servoStart = 0;
delay(40);
}