Pages: [1]   Go Down
Author Topic: Set the global variable has some question  (Read 697 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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 [code] [/code]
« Last Edit: July 02, 2012, 01:36:33 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lose the gotos, then we can talk

Code:
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:
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.
« Last Edit: July 02, 2012, 02:06:47 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How to  record and access  the latitude and longitude
d1=latitude
d2=longitude

Code:
   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.
« Last Edit: July 03, 2012, 01:53:22 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Posting snippets is just a waste of time.

Did you read my comments yesterday here about terminating strings?
« Last Edit: July 03, 2012, 01:53:37 am by AWOL » Logged

"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.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19367
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1]   Go Up
Jump to: