Go Down

Topic: Set the global variable has some question (Read 738 times) previous topic - next topic

s55666

Jul 02, 2012, 08:22 am Last Edit: Jul 02, 2012, 08:36 am by AWOL Reason: 1
When we add the "float a1,b1;" this program can not into "void setup".
We just set the variable. But we can't run the program.

Code: [Select]
#include <NewSoftSerial.h>            // Add to library folder
#include <TinyGPS.h>  // Add to library folder
#include <string.h>
float a1,b1; //add this row,the program can not into the "void setup"
double d1,d2;
char *pEnd;
TinyGPS gps;
NewSoftSerial nss(7, 255);            // Yellow wire to pin 6
char value[20];
int i;

void setup()
{
 Serial.begin(9600);
 nss.begin(4800);
 Serial.println("Reading GPS");
 delay(1000);
wait:
 if(Serial.available()>0)
 {
   for(i=0;i<20;i++)
   {
   value[i] = Serial.read();
   }
   Serial.println(value);
   char *pEnd;
   d1=strtod(value,&pEnd);
   d2=strtod(pEnd,NULL);
   Serial.println(d1,6);
   Serial.println(d2,6);
   goto wait2;
 }
 else
 {
 //Serial.println('1');
 goto wait;
 }
wait2:delay(500);
}
void loop() {
 Serial.println('2');
 bool newdata = false;
 unsigned long start = millis();
 while (millis() - start < 5000) {  // Update every 5 seconds
   if (feedgps())
     newdata = true;
 }
 if (newdata) {
   gpsdump(gps);
 }
}

// Get and process GPS data
void gpsdump(TinyGPS &gps) {
 float flat, flon, x,y,lat2,long2,d3,d4,d5,d6,d7,a3,a4,a5,a6,a7,c;
 unsigned long age;
 gps.f_get_position(&flat, &flon, &age);
 x=flat,y=flon;
 Serial.print(x, 6); Serial.print(", ");
 Serial.println(y, 6);
 float course_to (float x, float y, float d1, float d2);
 {
   if ((d2-y)<0)
   {
d3=(24-x)*111.11111*1000;
d4=(sqrt(sq(d1-x)+sq(d2-y)))*111.11111*1000;
 d5=(sqrt(sq(d2-y)+sq(d1-24)))*111.11111*1000;
 d6=((sq(d3)+sq(d4)-sq(d5))/(2*d3*d4));
 d7=((acos(d6))*57.29578)*(-1);
   }
   else
   {
   d3=(24-x)*111.11111*1000;
d4=(sqrt(sq(d1-x)+sq(d2-y)))*111.11111*1000;
 d5=(sqrt(sq(d2-y)+sq(d1-24)))*111.11111*1000;
 d6=((sq(d3)+sq(d4)-sq(d5))/(2*d3*d4));
 d7=((acos(d6))*57.29578);
}
   if((y-b1)<0)
   {
 a3=(24-a1)*111.11111*1000;
 a4=(sqrt(sq(x-a1)+sq(y-b1)))*111.11111*1000;
 a5=(sqrt(sq(y-b1)+sq(x-24)))*111.11111*1000;
 a6=((sq(a3)+sq(a4)-sq(a5))/(2*a3*a4));
 a7=((acos(a6))*57.29578)*(-1);
   }
 else if((y-b1)>0)
 {
     a3=(24-a1)*111.11111*1000;
 a4=(sqrt(sq(x-a1)+sq(y-b1)))*111.11111*1000;
 a5=(sqrt(sq(y-b1)+sq(x-24)))*111.11111*1000;
 a6=((sq(a3)+sq(a4)-sq(a5))/(2*a3*a4));
 a7=((acos(a6))*57.29578);
 }
 else
 {
   a7=0;
 }
 c=a7-d7;
 Serial.println(c,6);  //delta angle,H-T
 Serial.println(d4,6); //car to target distance
 Serial.println(d7,6); //cat to target angle
 a1==x,b1==y;
}
}
// Feed data as it becomes available
bool feedgps(){
 while (nss.available()) {
   if (gps.encode(nss.read()))
     return true;
 }
 return false;
}

Moderator edit: Replace quote tags with [font=Verdana][[/font]code] [font=Verdana][[/font]/code]

AWOL

#1
Jul 02, 2012, 08:37 am Last Edit: Jul 02, 2012, 09:06 am by AWOL Reason: 1
Lose the gotos, then we can talk

Code: [Select]
if(Serial.available()>0)
 {
   for(i=0;i<20;i++)
   {
   value[i] = Serial.read();
   }

Let's all recite the mantra: "See if there's at least one character available in the receive buffer, then go ahead and read all twenty of them...oooooommmmm"

Code: [Select]
d1=strtod(value,&pEnd);
Risky strategy, relying on the the left-over nulls in the array to terminate your string for you, 'cos there aren't any.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

s55666

#2
Jul 03, 2012, 08:17 am Last Edit: Jul 03, 2012, 08:53 am by AWOL Reason: 1
How to  record and access  the latitude and longitude
d1=latitude
d2=longitude

Code: [Select]
   char *pEnd;
   d1=strtod(value,&pEnd);
   d2=strtod(pEnd,NULL);
   Serial.println(d1,6);
   Serial.println(d2,6);


Moderator edit: quote tags swapped for code tags.

AWOL

#3
Jul 03, 2012, 08:45 am Last Edit: Jul 03, 2012, 08:53 am by AWOL Reason: 1
Posting snippets is just a waste of time.

Did you read my comments yesterday here about terminating strings?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon

I see what you did there. You ignored the advice in the earlier thread and started a new one.

Threads merged.

Don't keep doing that please. If you don't understand the advice, say so.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

AWOL

For help, you need to post all you code.
Hopefully by now, you'll have corrected the serial reading so that you wait for each character to be available before reading it (or waiting until all the characters you expect are available) and got rid of the "goto"s, and terminated the string before calling any string operations.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up