Hello, I am implementing an Arduino code that utilizes a Gy-gps6mv2 GPS module and controls 2 servo motors. The code for the servo control and for the GPS module location acquisition work perfectly separately, however when I compile both together, the servos act uncontrollably (even executing movements greater than 180 degrees, which conventionally would not be possible), and when the GPS module is disconnected this behavior stops and they start to respond normally.
//---------------azimuth----------------------------------------------------------------
#include <QMC5883LCompass.h>
QMC5883LCompass compass;
//---------------azimuth----------------------------------------------------------------
//---------------GPS--------------------------------------------------------------------
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
//---------------GPS--------------------------------------------------------------------
//---------------servo------------------------------------------------------------------
#include <Servo.h>
Servo myservoH;
Servo myservoV;
//---------------servo------------------------------------------------------------------
void setup() {
//---------------azimuth----------------------------------------------------------------
compass.init();
compass.setCalibration(-481, 751, -673, 778, -10, 782);
//---------------azimuth----------------------------------------------------------------
//---------------GPS--------------------------------------------------------------------
ss.begin(GPSBaud);
//---------------GPS--------------------------------------------------------------------
//---------------servo------------------------------------------------------------------
myservoH.attach(9); // Conecta o servo horizontal no pino 9
myservoV.attach(11); // Conecta o servo horizontal no pino 11
//Move ambos para 90º
myservoH.write(90);
myservoV.write(90); //Posição neutra vertical na verdade acontece com 80º
//---------------servo------------------------------------------------------------------
Serial.begin(9600);
delay(1000);
}
void loop() {
if(Serial.available() > 0){
String input = Serial.readString(); //Leando o input serial
String function = input.substring(0, input.indexOf(",")); //extraindo o nome da função
//---------------azimuth---------------------------------------------------------------------
if(function == "getAzimuth"){
int azimuth = getAzimuth();
Serial.println(azimuth);
}
//---------------azimuth---------------------------------------------------------------------
//---------------GPS-------------------------------------------------------------------------
else if(function == "getLocation"){
String localizacao = getLocation();
Serial.println(localizacao);
}
//---------------GPS-------------------------------------------------------------------------
//---------------servo-----------------------------------------------------------------------
else if(function == "moveServoH"){
int p = input.substring(input.lastIndexOf(",")+1).toInt();
int retorno = moveServoH(p);
Serial.println(retorno);
}
else if(function == "moveServoV"){
int p = input.substring(input.lastIndexOf(",")+1).toInt();
int retorno = moveServoV(p);
Serial.println(retorno);
}
//---------------servo-----------------------------------------------------------------------
else{
Serial.println("Função não encontrada");
}
}
}
//---------------azimuth---------------------------------------------------------------------
int getAzimuth(){
compass.read();
int az = compass.getAzimuth();
return(az);
}
//---------------azimuth---------------------------------------------------------------------
//---------------GPS-------------------------------------------------------------------------
String getLocation(){
while(true)
{
while (ss.available() > 0){
if (gps.encode(ss.read()))
{
if (gps.location.isValid() && gps.altitude.isValid())
{
String Loc = String(gps.location.lat(), 6)+","+String(gps.location.lng(), 6)+","+String(gps.altitude.meters(), 2);
return Loc;
break;
}
}
}
}
}
//---------------GPS-------------------------------------------------------------------------
//---------------servo-----------------------------------------------------------------------
int moveServoH(int newpos) {
if (newpos < 0 || newpos > 180) {
// impede que o servo seja movido para fora da faixa de 0-180 graus
return(0);
}
int pos = myservoH.read();
if(pos>newpos) {
for (; pos >= newpos; pos--) {
myservoH.write(pos);
delay(50);
}
}else{
for (; pos <= newpos; pos++) {
myservoH.write(pos);
delay(50);
}
}
return(1);
}
int moveServoV(int newpos) {
if (newpos < 0 || newpos > 97) {
// impede que o servo seja movido para fora da faixa de 0-97 graus
return(0);
}
int pos = myservoV.read();
if(pos>newpos) {
for (; pos >= newpos; pos--) {
myservoV.write(pos);
delay(15);
}
}else{
for (; pos <= newpos; pos++) {
myservoV.write(pos);
delay(15);
}
}
return(1);
}
//---------------servo-----------------------------------------------------------------------