So I'm working on a code that basically receives x and y coordinates and stores them in a variable. Whenever a coordinate is received, it goes through a conditional statement to store them in a variable, x and y, then passes it to a servo mover function. This servo motor function then moves according to what's given. It works well, but, as the values get updated over time, the servo' position always resets then goes back to where it was, again and again. Even when the coordinates are kept the same, it just resets then returns to its position over and over again.
How do I make it so that not only does it not reset then return to where it was but instead make it move to where it is supposed to go, but also to stop it from resetting anyway when the position is kept the same?
The below code is what I tried to do to basically alleviate the "position reset when coordinate is the same" issue. Unfortunately, it did not work.
bool constant = false;
// Rest of code inbetween, irrelevant to issue
// Relevant codes below
void statusCodes() {
if (received_str == "1") {
Serial.println("Raspberry Pi - Connection Established");
} else if (received_str == "2") {
Serial.println("Raspberry Pi - Data Received");
} else if (received_str == "100") {
Serial.println("Raspberry Pi - Status OK");
} else if (received_str[0] == '(' ) {
Serial.println("Coordinates Received, Ready to Delete Target!");
if (constant == true) { // checks if this constant boolean is true
movement = false;
} else {
movement == true;
}
int r = sscanf(received_str.c_str(), "(%d, %d)", &x, &y);
Serial.print("X: ");
Serial.println(x);
Serial.print("Y: ");
Serial.println(y);
}
}
void onDetectTarget() {
x = map(x, 0, 1023, 0, 180);
y = map(y, 0, 1023, 0, 180);
if (movement == true) {
for (pos = 0; pos <= x; pos += 1) {
myservo.write(pos);
delay(15);
}
movement = false; // corrected
pos = 0; // corrected
}
}
void compareOldNew() {
x_old = x;
y_old = y;
delay(900);
if (x == x_old && y == y_old) {
constant = true;
} else {
constant = false;
}
}