TinyGPS and Servos

Hi, i have a lot of questions. I want to start telling you what i want to do.

  1. When i power on my system i want to keep landitude and longitude into two values(PositionA).
  2. Then i dont want to do anything, just capture lan and lng and save it into two values.
  3. When i push a button (input port 9) i want to save the position in the place that will be the system (PositionB).
  4. Then i want with servo movements (left-write) to return from PositionB to PositionA.

Problems:

  1. digitalRead of port9 always is HIGH even i m not plugin any button or switch.
  2. Servo movement like crazy when i plug in my arduino in power (usb from pc) even i dont have any code for movements. //I commented all lines with movements.
  3. My Console writes me 2 lines on every loop (That not so important problem i can add a counter), but why?
  4. If i add delay outside from while() gps not working at all. Also not important at the moment, but also why?

My code is:

#include <Servo.h>

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 4;
static const int TXPin = 3;
static const int LEDGPSPin = 8;
static const int SWITCHGPSPin = 9;
static const int servoPin = 11;

static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps;

SoftwareSerial ss(RXPin, TXPin);
Servo Servo1;

double BaseStartY;
double BaseStartX;
bool ReadBasePosition;

double GPSStartY;
double GPSStartX;
bool ReadGPSPosition;

double DiaforaBaseGPSY;
double DiaforaBaseGPSX;

double PreviousX;
double PreviousY;

double ActiveX;
double ActiveY;


void setup()
{
 Serial.begin(9600);
 ss.begin(GPSBaud);
 
 pinMode(LEDGPSPin, OUTPUT);
 pinMode(SWITCHGPSPin, INPUT);
 
 ReadBasePosition=false;
 ReadGPSPosition=false;

 Servo1.attach(servoPin); 
 Servo1.write(0); 
}
int angle=0;

void loop()
{

 while (ss.available() > 0)
 {
   gps.encode(ss.read());
   if (gps.location.isUpdated())
   {
    
     delay(1000);
     digitalWrite(LEDGPSPin, HIGH);

     ActiveX=gps.location.lng();
     ActiveY=gps.location.lat();
     if (!ReadBasePosition)//KATEGRAPSE THN ARXIKH TOPOTHESIA (PositionA)
     {
       
        BaseStartX=ActiveX;
        BaseStartY=ActiveY;
        ReadBasePosition=true;
     }
     else//EXOUME DIABASEI THN TOPOTHESIA EKKINISHS KAI KSEKINAME
     {
        
       //TO SEE WHAT HAPPENS!!
       //Serial.print(digitalRead(SWITCHGPSPin));
       if (digitalRead(SWITCHGPSPin)==HIGH) //ALWAYS HIGH WHY????? I DONT PLUGIN ANY BUTTON in PORT 9
       {
         
          if (!ReadGPSPosition) // PositionB)
          {
            GPSStartX==ActiveX;
            GPSStartY=ActiveY;
            ReadGPSPosition=true;
          }
          else
          {
           
            //SEE Values to see if GPS work propertly
            Serial.print(" LATITUDE= ");
            Serial.print(ActiveY, 6);
            Serial.print(" LONGITUDE= "); 
            Serial.println(ActiveX, 6);
            
            if (ActiveX<BaseStartX && ActiveY > BaseStartY)//(-X,Y)
            {
               //servo movements
               //if (ActiveX<PreviousX) turn servo right...
               //etc etc etc
               //..
               //..Example for commands i use
              //   for(angle = 0; angle < 180; angle += 1)    // command to move from 0 degrees to 180 degrees 
               //   {                                  
              //    Servo1.write(angle);                 
               //    delay(15);                       
              //  } 
            }
            else if (ActiveX>BaseStartX && ActiveY > BaseStartY)//(X,Y)
            {
              //servo movements
              //if (ActiveX<PreviousX) turn servo left...
              //etc etc etc
              //..
              //..
            }
            else if (ActiveX<BaseStartX && ActiveY < BaseStartY)//(-X,-Y)
            {
              //servo movements
              //if (ActiveY<PreviousY) turn servo right...
              //etc etc etc
              //..
              //..
              
            }
            else if (ActiveX>BaseStartX && ActiveY < BaseStartY)//(X,-Y)
            {
              //servo movements
              //if (ActiveY<PreviousY) turn servo left...
              //etc etc etc
              //..
              //..
            }
          }
       }
       else
       {
         ReadGPSPosition=false;
       }
     }
     PreviousX=ActiveX;
     PreviousY=ActiveY;
   }
 }
 //TO GPS DEN EXEI SHMA
 digitalWrite(LEDGPSPin, LOW);

}

A schematic/circuit diagram (NOT Fritzing) would help.

Do you have a pull-down resistor on pin 9? If not it is floating so might measure any random value.

What servo are you using and how is it connected and powered? Not from the 5V pin or connected via a breadboard I hope.

Steve

What kind of controller is used?
Read "How to use this forum". It tells waht information You should supply.

Servo movement like crazy when i plug in my arduino in power (usb from pc) even i dont have any code for movements.

That is a sure sign of an inadequate servo power supply.

DO NOT use the 5V Arduino output for powering servos. Use a separate power supply (4xAA batteries works for 1 or 2 small servos), or a power brick supplying 5 to 6V at at least 1 Ampere per servo. Be sure to connect the grounds.

  1. digitalRead of port9 always is HIGH even i m not plugin any button or switch.

That is how a floating input might read. You need a pullup or pulldown resistor connected to the input so that when the switch is open (or nothing is connected to the input) the input is pulled to a known level.
See here.

groundFungus:
That is how a floating input might read. You need a pullup or pulldown resistor connected to the input so that when the switch is open (or nothing is connected to the input) the input is pulled to a known level.
See here.

i dont plug anything on port 9 at this moment, normally must always be LOW.

jremington:
That is a sure sign of an inadequate servo power supply.

DO NOT use the 5V Arduino output for powering servos. Use a separate power supply (4xAA batteries works for 1 or 2 small servos), or a power brick supplying 5 to 6V at at least 1 Ampere per servo. Be sure to connect the grounds.

Yes thats right i use 5V from Arduino

slipstick:
A schematic/circuit diagram (NOT Fritzing) would help.

Do you have a pull-down resistor on pin 9? If not it is floating so might measure any random value.

What servo are you using and how is it connected and powered? Not from the 5V pin or connected via a breadboard I hope.

Steve

Railroader:
What kind of controller is used?
Read "How to use this forum". It tells waht information You should supply.

I upload my circuit

So take some notice of what people are telling you.

Put a 10K resistor between pin 9 and GND and it will read LOW. Then power the servo with a separate battery.

Steve

achillf:
i dont plug anything on port 9 at this moment, normally must always be LOW.

Then what's that wire going from the Switching regulator to pin 9?

If nothing is plugged into pin 9 and pin 9 is not set up as pulldown then how do you expect a Low to magically appear at pin 9?

Yes thats right i use 5V from Arduino

No. Not right. It 's wrong, wrong, wrong to run motors, a servo is a motor, from the 5V pin of an Arduino, STM 32, DUE, or ESP32.

What is the device connected between the servo and the Aruduino Uno's power pins? A switching regulator with an enable pin? Do not do that. Do not power motors and switching regulators from the Uno's 5V pin.

And please please show a real schematic and not a FritzThing.

You want pin 9 low, check out digitialWrite()

But that will NOT help until you fix the problem of powering the switching regulator and the motor from the Uno's 5V pin.

Thanks for advises, i will do that when i order resistors, we have lockdown here for covid, so must to wait to delivery resistors. I will come back for my results

How are you expecting resistors will solve the issues you have?

Ok what i did.

  1. I change the INPUT to INPUT_PULLUP and works fine.
  2. I give power on servo from external battery 5v but still servo goes like crazy again.
    Note if i make program without SoftwareSerial librady servo works fine, when include softwareserial something happens
  • As i read i found that SerialSoftware and Servo libraries cant work together.

What kind of battery did you use for the servo's, did you connect the ground of the battery to the Arduino ground?

I don't use software serial, causes to many issues/problems.

My guess, softwareserial and the pwm are are using the same timer or use a different pin for servo.

Idahowalker:
My guess, softwareserial and the pwm are are using the same timer or use a different pin for servo.

Is that not a known issue with Software serial, although I did think the issue cropped up at compile time, with an error caused by conflicts with interrupts.
NeoSWserial has a workaround I recall .............

srnet:
Is that not a known issue with Software serial, although I did think the issue cropped up at compile time, with an error caused by conflicts with interrupts.
NeoSWserial has a workaround I recall .............

Ι try also NeoSWserial, same problem.

achillf:
Ι try also NeoSWserial, same problem.

I did not say that just using NeoSWserial would directly help, but it does have a workaround for the IRQ problem, did you try it ?

My friends, i solve the problem i replace SoftwareSerial with NeoSWSerial, i connect gps's transmitter to port 9 and i remove external power supply from servo and i connect servo's power cable direct to arduino. All works perfectly