Remote Control and Datalogger Robot

Hello

I’m trying to do a small AGV guided by GPS. In this first program I control the robot remotely by bluetooth. The robot records the coordenates while is making the route. I have tried the remote control and bluetooth and the GPS with a micro SD card. They both work right but separately. I don’t know why it doensn’t work when I fix all the codes together.
When I fix all the program, the remote control works but the datalloger doesn’t.

Could you help me? I’m using Arduino Uno, GPS module, MicroSD card module, bluetooth HC-06 module and DC motors.

[
#include <SD.h> //Llibreria tarjeta memoria.
#include <SPI.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h> //Llibreria GPS

TinyGPSPlus gps;  
SoftwareSerial ss(4, 3);
SoftwareSerial BT(5,6);


int izqA = A0; //Motor-1 A(+) connectat al pin A0
int izqB = A1; //Motor-1 B(-) connectat al pin A1
int derA = A2; //Motor-2 A(+) connectat al pin A2
int derB = A3; //Motor-2 B(-) connectat al pin A3
int vel = 130; //Estableix velocitat motors
int estado ='c'; //Para motors
int ledGPS= 7; // LED estat GPS




File coor;
File lat1;
File lng1;
int id=0;


void setup(){
Serial.begin(115200);
BT.begin(9600);
pinMode(derA, OUTPUT);
pinMode(derB, OUTPUT);
pinMode(izqA, OUTPUT);
pinMode(izqB, OUTPUT);
pinMode(10, OUTPUT); //Pin CS mòdul SD.

pinMode(ledGPS, OUTPUT);
digitalWrite(ledGPS, LOW);


Serial.print(F("Iniciando SD ..."));
  if (!SD.begin(8))
  {
    Serial.println(F("Error al iniciar"));
    return;
  }
  Serial.println(F("Iniciado correctamente"));
  
delay(3000);

 
 coor=SD.open("coor.txt", FILE_WRITE); 
 if (coor)
 { 
 coor.print(F("Id")); 
 coor.print(",");
 coor.print(F("Date"));
 coor.print(",");
 coor.print(F("Time"));
 coor.print(",");
 coor.print(F("Latitude"));
 coor.print(",");
 coor.print(F("Longitude"));
 coor.close();
 }
 
 
}

void loop(){
if(BT.available()>0){ // lee el bluetooth y almacena en estado
  estado = BT.read();
}
if(estado=='a'){ 
Forward();
}
if(estado=='d'){ 
Right();
}
if(estado=='c'){ 
STOP();
}
if(estado=='b'){ 
Left();
} 
if(estado=='e'){ 
Backwards();
}
 

while (ss.available()>0)
gps.encode(ss.read());
digitalWrite(ledGPS, HIGH);

if (gps.location.isUpdated())
{
 blinkLED();
 id++;
 coordenates();
 latitude1();
 longitude1();

 delay(1000);
}
}


void blinkLED()
{
digitalWrite(ledGPS, HIGH);
delay(1000);
digitalWrite(ledGPS, LOW);
delay(1000);
}

void Forward()
{
analogWrite(derB, 0); 
analogWrite(izqB, vel); 
analogWrite(derA, vel); 
analogWrite(izqA, 0); 
}

void Right()
{
analogWrite(derB, vel); 
analogWrite(izqB, vel); 
analogWrite(derA, 0); 
analogWrite(izqA, 0); 
}

void STOP() 
{
analogWrite(derB, 0); 
analogWrite(izqB, 0); 
analogWrite(derA, 0); 
analogWrite(izqA, 0); 
}

void Left()
{
analogWrite(derB, 0); 
analogWrite(izqB, 0);
analogWrite(derA, vel);
analogWrite(izqA, vel); 
}

void Backwards()
{
analogWrite(derB, vel); 
analogWrite(izqB, 0);
analogWrite(derA, 0); 
analogWrite(izqA, vel); 
}

void coordenates()
{
coor=SD.open("coor.txt", FILE_WRITE);
 coor.print(id);
 coor.print(gps.date.day(),5);
 coor.print(gps.date.month(),5);
 coor.print(gps.location.lat(),5);
 coor.println(gps.location.lng(),5);
 coor.close();
}

void latitude1()
{
lat1=SD.open("lat1.txt", FILE_WRITE);
lat1.println(gps.location.lat(),5);
lat1.close();
}

void longitude1()
{
lng1=SD.open("lng1.txt", FILE_WRITE);
lng1.println(gps.location.lat(),5);
lng1.close();
}
]
SoftwareSerial ss(4, 3);
SoftwareSerial BT(5,6);

I'd guess that the BT instance is used to talk to the BlueTooth device. I can't begin to guess what the ss instance is used to talk to.

You are aware that only one instance of SoftwareSerial can listen at a time, right?

int estado ='c'; //Para motors

It makes NO sense to store a character value in an int variable. There are appropriate types to store character data in and there are appropriate values to store in an int variable.

BT.begin(9600);

Where do you initialize the ss instance?

 coordenates();
 latitude1();
 longitude1();

Good function names have a noun (what to operate on) and a verb (what operation to perform), at a minimum, and they do NOT have numbers in them. Is latitude a noun or a verb?

Calling those functions when you do not have good data from the GPS is pointless.

First, thank you for replying me.

Well, you’re right the BT is about de bluetooth.

I have changed some code and corrected the mistakes:

I have put estado as a char.

You were right about ss was missing. Now I initialize it. The ss if for initializing the gps.

I have changed the the name functions as you suggested.

After all the changes, I have the same problem… if the remote control works, the gps datalogger doesn’t. The same in reverse, if the gps datalogger works, the remote control doesn’t. I have tried changing the order and many thigs but it still doenst work all together.
!!!
I think the problem is what you have said about the SoftwareSerial. Only one can be listened at a time. So, is not posible to use a remote control while I’m getting GPS data? Maybe, I have to move to a position, then STOP and get the data, stop the data and the continue…
I think that with an Arduino Uno is not possible… I should use an Arduino MEGA??
Any ideas?

I attach the code:

#include <SD.h> //Llibreria tarjeta memoria.
#include <SPI.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h> //Llibreria GPS

TinyGPSPlus gps;  
SoftwareSerial ss(4, 3);
SoftwareSerial BT(5,6);


int izqA = A0; //Motor-1 A(+) connectat al pin A0
int izqB = A1; //Motor-1 B(-) connectat al pin A1
int derA = A2; //Motor-2 A(+) connectat al pin A2
int derB = A3; //Motor-2 B(-) connectat al pin A3
int vel = 130; //Estableix velocitat motors
char estado ='c'; //Para motors
int ledGPS= 7; // LED estat GPS




File coor;
File lat1;
File lng1;
int id=0;


void setup(){
Serial.begin(115200);
ss.begin(9600);
BT.begin(9600);
pinMode(derA, OUTPUT);
pinMode(derB, OUTPUT);
pinMode(izqA, OUTPUT);
pinMode(izqB, OUTPUT);
pinMode(10, OUTPUT); //Pin CS mòdul SD.

pinMode(ledGPS, OUTPUT);
digitalWrite(ledGPS, LOW);


Serial.print(F("Iniciando SD ..."));
  if (!SD.begin(8))
  {
    Serial.println(F("Error al iniciar"));
    return;
  }
  Serial.println(F("Iniciado correctamente"));
  
delay(3000);

 
 coor=SD.open("coor.txt", FILE_WRITE); 
 if (coor)
 { 
 coor.print(F("Id")); 
 coor.print(",");
 coor.print(F("Date"));
 coor.print(",");
 coor.print(F("Time"));
 coor.print(",");
 coor.print(F("Latitude"));
 coor.print(",");
 coor.print(F("Longitude"));
 coor.close();
 }
 
 
}

void loop(){
if(BT.available()>0){ // lee el bluetooth y almacena en estado
  estado = BT.read();
}
if(estado=='a'){ 
Forward();
}
if(estado=='d'){ 
Right();
}
if(estado=='c'){ 
STOP();
}
if(estado=='b'){ 
Left();
} 
if(estado=='e'){ 
Backwards();
}
 

while (ss.available()>0)
gps.encode(ss.read());
digitalWrite(ledGPS, HIGH);

if (gps.location.isUpdated())
{
 blinkLED();
 id++;
 coordenates();
 latitude();
 longitude();


}
}


void blinkLED()
{
digitalWrite(ledGPS, HIGH);
delay(1000);
digitalWrite(ledGPS, LOW);
delay(1000);
}

void Forward()
{
analogWrite(derB, 0); 
analogWrite(izqB, vel); 
analogWrite(derA, vel); 
analogWrite(izqA, 0); 
}

void Right()
{
analogWrite(derB, vel); 
analogWrite(izqB, vel); 
analogWrite(derA, 0); 
analogWrite(izqA, 0); 
}

void STOP() 
{
analogWrite(derB, 0); 
analogWrite(izqB, 0); 
analogWrite(derA, 0); 
analogWrite(izqA, 0); 
}

void Left()
{
analogWrite(derB, 0); 
analogWrite(izqB, 0);
analogWrite(derA, vel);
analogWrite(izqA, vel); 
}

void Backwards()
{
analogWrite(derB, vel); 
analogWrite(izqB, 0);
analogWrite(derA, 0); 
analogWrite(izqA, vel); 
}

void coordenates()
{
coor=SD.open("coor.txt", FILE_WRITE);
 coor.print(id);
 coor.print(gps.date.day(),5);
 coor.print(gps.date.month(),5);
 coor.print(gps.location.lat(),5);
 coor.println(gps.location.lng(),5);
 coor.close();
}

void latitude()
{
lat1=SD.open("lat1.txt", FILE_WRITE);
lat1.println(gps.location.lat(),5);
lat1.close();
}

void longitude()
{
lng1=SD.open("lng1.txt", FILE_WRITE);
lng1.println(gps.location.lat(),5);
lng1.close();
}

After all the changes, I have the same problem... if the remote control works, the gps datalogger doesn't. The same in reverse, if the gps datalogger works, the remote control doesn't.

So, you are saying that if you are listening to the bluetooth device, you can't get data from the GPS, and if you are listening to the GPS, you can't get data from the bluetooth device.

Well, that's the way SoftwareSerial works.

Get a Mega, with 4 hardware serial ports, and quit trying to use SoftwareSerial.