error: expected unqualified-id before while

i am recieving an error expected unqualified-id before while at this point.

float turn;
float t = turn;
while (t < -180) t += 360;
while (t > 180) t -= 360;

could someone please give some directions on how to go about it.

Start by posting your complete program.

My guess is that you have a missing { or } in the program before the lines that you posted. Auto formatting the code in the IDE can help to show up such problems as can putting each { and } alone on its own line

UKHeliBob:
Start by posting your complete program.

My guess is that you have a missing { or } in the program before the lines that you posted. Auto formatting the code in the IDE can help to show up such problems as can putting each { and } alone on its own line

// Imports
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
// Pin variables
#define GPS_TX_PIN 6
#define GPS_RX_PIN 3

#define BLUETOOTH_TX_PIN 10
#define BLUETOOTH_RX_PIN 11

// Bluetooth GPS input
#define SOP '<'
#define EOP '>'

// You must then add your 'Declination Angle' to the compass, which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: 23° 4' E (Positive),
#define DECLINATION_ANGLE +0.34f

#define COMPASS_OFFSET 0.35f

#define GPS_UPDATE_INTERVAL 1000

#define GPS_WAYPOINT_TIMEOUT 25

// Struct to combine our coordinates into one struct for ease of use
struct GeoLoc {
float lat;
float lon;
};

// GPS
TinyGPSPlus gps;
#define GPS_BAUD 9600

// Motors
Servo myservo;

// Master Enable
bool enabled = true;

// Bluetooth input
bool started = false;
bool ended = false;
char inData[80]; // creates an 80 character array called "inData"
byte index; //creates a variable type=byte called "index"
double Long; //variable for longitude coordinate
double Lat; //variable for latitude coordinate

// Serial components
SoftwareSerial bluetoothSerial(BLUETOOTH_TX_PIN, BLUETOOTH_RX_PIN);
SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN); // TXD to digital pin 6

/* Compass */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

GeoLoc checkGPS() {
Serial.println("Reading onboard GPS: ");
unsigned long start = millis();
while (millis() - start < GPS_UPDATE_INTERVAL) {
// If we recieved new location then take the coordinates and pack them into a struct
if (feedgps())
return gpsdump();
}

GeoLoc robotLoc;
robotLoc.lat = 0.0;
robotLoc.lon = 0.0;

return robotLoc;
}

// Get and process GPS data
GeoLoc gpsdump() {
GeoLoc robotLoc;
robotLoc.lat = gps.location.lat();
robotLoc.lon = gps.location.lng();

Serial.print(robotLoc.lat, 7); Serial.print(", "); Serial.println(robotLoc.lon, 7);

return robotLoc;
}

// Feed data as it becomes available
bool feedgps() {
while (gpsSerial.available() > 0) {
if (gps.encode(gpsSerial.read()))
return true;
}
return false;
}

void displayCompassDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}

#ifndef DEGTORAD
#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f
#endif

float geoBearing(struct GeoLoc &a, struct GeoLoc &b) {
float y = sin(b.lon-a.lon) * cos(b.lat);
float x = cos(a.lat)*sin(b.lat) - sin(a.lat)*cos(b.lat)*cos(b.lon-a.lon);
return atan2(y, x) * RADTODEG;
}

float geoDistance(struct GeoLoc &a, struct GeoLoc &b) {
const float R = 6371000; // radius of earth in metres
float p1 = a.lat * DEGTORAD;
float p2 = b.lat * DEGTORAD;
float dp = (b.lat-a.lat) * DEGTORAD;
float dl = (b.lon-a.lon) * DEGTORAD;

float x = sin(dp/2) * sin(dp/2) + cos(p1) * cos(p2) * sin(dl/2) * sin(dl/2);
float y = 2 * atan2(sqrt(x), sqrt(1-x));

// returns distance in meters
return R * y;
}

float geoHeading() {
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);

float heading = atan2(event.magnetic.y, event.magnetic.x);

// Offset
heading -= DECLINATION_ANGLE;
heading -= COMPASS_OFFSET;

if(heading < 0)
heading += 2*PI;

if(heading > 2PI)
heading -= 2
PI;

float headingDegrees = heading * 180/M_PI;

// Map to -180 - 180
while (headingDegrees < -180) headingDegrees += 360;
while (headingDegrees > 180) headingDegrees -= 360;

return headingDegrees;
}

float turn;
float t = turn;
while (t < -180) t += 360;
while (t > 180) t -= 360;

Serial.print("turn: ");
Serial.println(t);
Serial.print("original: ");
Serial.println(turn);

float t_modifier = (180.0 - abs(t)) / 180.0;
float autoSteerA = 1;
float autoSteerB = 1;

if (t < 0) {
autoSteerB = t_modifier;
} else if (t > 0){
autoSteerA = t_modifier;
}

Serial.print("steerA: "); Serial.println(autoSteerA);
Serial.print("steerB: "); Serial.println(autoSteerB);

}

void driveTo(struct GeoLoc &loc, int timeout) {
gpsSerial.listen();
GeoLoc robotLoc = checkGPS();
bluetoothSerial.listen();

if (robotLoc.lat != 0 && robotLoc.lon != 0 && enabled) {
float distance = 0;
//Start move loop here
do {
gpsSerial.listen();
robotLoc = checkGPS();
bluetoothSerial.listen();

distance = geoDistance(robotLoc, loc);
float bearing = geoBearing(robotLoc, loc) - geoHeading();

Serial.print("Distance: ");
Serial.println(distance);

Serial.print("Bearing: ");
Serial.println(geoBearing(robotLoc, loc));

Serial.print("Heading: ");
Serial.println(geoHeading());

drive(distance, bearing);
timeout -= 1;
} while (distance > 1.0 && enabled && timeout>0);

stop();
}
}

void setupCompass() {
/* Initialise the compass /
if(!mag.begin())
{
/
There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}

/* Display some basic information on this sensor */
displayCompassDetails();
}

void setup()
{
// Attaching servo
myservo.attach(13)

//Debugging via serial
Serial.begin(115200);

// Compass
setupCompass();

//GPS
gpsSerial.begin(GPS_BAUD);

//Bluetooth
bluetoothSerial.begin(9600);

}

// Testing
void testDriveNorth() {
float heading = geoHeading();
int testDist = 5;
Serial.println(heading);

while(!(heading < 5 && heading > -5)) {
drive(testDist, heading);
heading = geoHeading();
Serial.println(heading);
delay(500);
}

stop();
}

void loop()
{

while (bluetoothSerial.available() > 0)
{
char inChar = ((byte)bluetoothSerial.read());
if (inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if (inChar == EOP)
{
ended = true;
break;
}
else
{
if (index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}

if (started && ended)
{
char *token = strtok(inData, ",");
boolean first = true;
while (token)
{
double val = atof(token);
token = strtok(NULL, ",");
if (first){
Lat = val;
}else{
Long = val;
}
first = false;
}

started = false;
ended = false;
index = 0;
inData[index] = '\0';

GeoLoc waypointLoc;
waypointLoc.lat = Lat;
waypointLoc.lon = Long;

Serial.print(waypointLoc.lat, 7); Serial.print(", "); Serial.println(waypointLoc.lon, 7);
driveTo(waypointLoc, GPS_WAYPOINT_TIMEOUT);

}
}

You have code not in a function after the geoHeading() function. Where should the function end ?

Did you try the advice in reply #1 about formatting the code ?

UKHeliBob:
You have code not in a function after the geoHeading() function. Where should the function end ?

Did you try the advice in reply #1 about formatting the code ?

i have but ends with the errors highlighted here

}

float turn;
float t = turn;
while (t < -180) t += 360;
while (t > 180) t -= 360;

Serial.print("turn: ");
Serial.println(t);
Serial.print("original: ");
Serial.println(turn);

float t_modifier = (180.0 - abs(t)) / 180.0;
float autoSteerA = 1;
float autoSteerB = 1;

if (t < 0) {
autoSteerB = t_modifier;
} else if (t > 0) {
autoSteerA = t_modifier;
}

float geoHeading()
{
  /* Get a new sensor event */
  sensors_event_t event;
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  // Offset
  heading -= DECLINATION_ANGLE;
  heading -= COMPASS_OFFSET;
  if (heading < 0)
    heading += 2 * PI;
  if (heading > 2 * PI)
    heading -= 2 * PI;
  float headingDegrees = heading * 180 / M_PI;
  // Map to -180 - 180
  while (headingDegrees < -180) headingDegrees += 360;
  while (headingDegrees >  180) headingDegrees -= 360;
  return headingDegrees;
}   //<<<< end of the geoHeading() function


float turn;
float t = turn;
while (t < -180) t += 360;  //<<<<<< code not in a function
while (t >  180) t -= 360;

Please note the use of code tags and use them in future when you post code