My code which saves strings in a class array, sent from a COM port through an application I've written in visual c++, works fine when I run it standalone. When I add the code in my final project it stucks. The additional code that runs in my final code is just some pin definitions, variable declarations, and pinmode in the setup proccess.
Standalone code:
#include <stdlib.h>
class Coordinates
{
private:
float latitude;
float longitude;
public:
void setLatitude(float aLatitude){latitude = aLatitude;}
void setLongitude(float aLongitude){longitude = aLongitude;}
double getLatitude(){return latitude;}
double getLongitude(){return longitude;}
};
bool comm_done1 = false;
char buffer1[8];
char buffer2[8];
char buffer3[8];
Coordinates allCoords[7];
int times,p;
void setup()
{
Serial.begin(115200);
}
void loop()
{
if(!comm_done1)
{
while(Serial.available() < 1 ){ }
buffer1[0] = Serial.read();
times = atoi(buffer1)*16;
if(times < 112)
{
while (Serial.available() < times){ }
for(int j=0; j<times/16; j++)
{
for(int i=0; i<8; i++) buffer2[i] = Serial.read();
for(int i=0; i<8; i++) buffer3[i] = Serial.read();
allCoords[j].setLatitude(atof(buffer2));
allCoords[j].setLongitude(atof(buffer3));
}
}
else
{
for(int i=0; i<10; i++) {Serial.print("Sec ");Serial.println(i);delay(1000); }
for(int i=0;i<atoi(buffer1);i++)
{
Serial.print(i+1);
Serial.print(" Lat: ");
Serial.print(allCoords[i].getLatitude(),5);
Serial.print(" Lon: ");
Serial.println(allCoords[i].getLongitude(),5);
}
} // else
} //loop
Inside the main loop I print the lat and long values every 10 secs.
Now, all that I add in the final code, are those declerations:
/*************************************************************************************************
* LIBRARIES *
*************************************************************************************************/
#include <Wire.h>
#include <stdlib.h>
/*************************************************************************************************
* PIN SETUP *
*************************************************************************************************/
#define motor1a 5
#define motor1b 6
#define motor2a 11
#define motor2b 12
#define pingPin 4
#define fix2DPin 8
/*************************************************************************************************
* GLOBAL VARIABLES *
*************************************************************************************************/
class Coordinates
{
private:
float latitude;
float longitude;
public:
void setLatitude(float aLatitude){latitude = aLatitude;}
void setLongitude(float aLongitude){longitude = aLongitude;}
double getLatitude(){return latitude;}
double getLongitude(){return longitude;}
};
char buffer0[8];
char buffer1[8];
char buffer2[8];
char buffer3[8];
Coordinates allCoords[18];
int times,p;
bool path_saved = false;
int wp_cnt = 0;
long distance_sensor, distance_tmp;
int compassAdress = 0x21, slaveMicroControllerAddress = 2;
float current_latitude, current_longitude;
int heading, calibrated_heading, desired_heading;
float distance_to_next_point;
//unsigned long time, tmp;
char buffer[17];
char *latEnd;
/*************************************************************************************************
* CONTROLLER'S SETUP *
*************************************************************************************************/
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
pinMode(motor1a, OUTPUT);
pinMode(motor1b, OUTPUT);
pinMode(motor2a, OUTPUT);
pinMode(motor2b, OUTPUT);
pinMode(fix2DPin, INPUT);
}
The final code seems to stuck in one of the while loops where I wait to receive serial data from my computer's serial COM port through the application. Its also unable to connect to the IDE serial port so I can't figure out where it stucks exactly. Could those declarations affect the serial communication between the serial COM port of my PC and the arduino board? If not, why on earth the final code stucks? This is very odd