Não percebi o que quiseste dizer com "a actualização da variável verpos não está correcta em relação ao código inicial" porque pelo menos aparentemente, a coisa está a funcionar!
Em relação a adpatar o código, estava a falar no código que me foi sugerido por alguém no IRC.
Este código:
#include <Servo.h>
void setup(){
Serial.begin(9600);
}
struct vec {int x; int y;};
vec dest;
vec pos;
void printVec(Print &p, const struct vec &c) {
p.print(c.x);
p.print(' ');
p.print(c.y);
}
void updateDestinations() {
dest.x = Serial.parseInt();
dest.y = Serial.parseInt();
unsigned long start = millis();
while (millis() - start < 100) {
while (Serial.read() != -1)
;
}
}
int int_sgn(int n){
if (n > 0){
return +1;
}
if (n < 0){
return -1;
}
return 0;
}
void loop() {
if (Serial.available()) {
updateDestinations();
}
Serial.print(F( "Dest : "));
printVec(Serial, dest);
Serial.print(F("|Pos : "));
printVec(Serial, pos );
const bool arrived = (dest.x == pos.x && dest.y == pos.y);
Serial.println(arrived ? F(" [arrived]") : F(" [traveling]"));
delay(100);
pos.x += int_sgn(dest.x - pos.x) * 2;
pos.y += int_sgn(dest.y - pos.y) ;
}
↧