Error message - error: a function-definition is not allowed here before '{' toke

Hello

I am writing my first code for my Arduino robot. The compiler has thrown up this error message on line 20.

"error: a function-definition is not allowed here before '{' token
a function-definition is not allowed here before '{' token"

I believe my format is good and readable and I have counted all the brackets as suggested in other posts.

The code simply allows the robot to drive through a small square circuit or at least that's what I want it to do.

Best regards

Rob

#include <ArduinoRobot.h>

void setup()
{
Robot.begin();
}

int head, new_head;
void loop()
{
Robot.motorsWrite(100, 100); //Drive forward for one second
delay(1000);
Robot.motorsWrite(0, 0); //stop
delay(500);
Robot.compassRead(); //Use compass to read current heading
head = Robot.compassRead(); //Assign value to head
new_head = head + 90; //define new heading
Robot.motorsWrite(50, -50); //start right turn
void loop() //use loop to monitor heading through turn
{
Robot.compassRead();
head = compassRead();
if (head < new_head) //nested loop stops turn when new heading achieved
delay(0);
else
Robot.motorsWrite(0, 0);
}
delay(1000);
}

void loop()
{
  Robot.motorsWrite(100, 100);      //Drive forward for one second
  delay(1000);
  Robot.motorsWrite(0, 0);          //stop
  delay(500);
  Robot.compassRead();              //Use compass to read current heading
  head = Robot.compassRead();       //Assign value to head
  new_head = head + 90;             //define new heading
  Robot.motorsWrite(50, -50);       //start right turn
  void loop()                       //use loop to monitor heading through turn
  {

You are trying to define another function called loop() in the middle of loop(). That is not allowed.

Thank you for your quick response.

Does that mean nested loops are not possible or do they have to be written in a different format?

Best regards

I suggest you look at the examples in the IDE to see examples of how things are done.
Also look at this web site for other examples.

void setup()
{
  Robot.begin();
}

int head, new_head;
void loop()
{

The code has "int head, new_head" outside any functions. It is not in setup(), and it is not in loop(), nor any other function. It will not work there as I understand it.

Thanks Delta_G for the info.
I had thought that could only be done before the setup().
Then, using the 2 pass compiler, I guess you could even do that way at the bottom of the script. Good info.

Thank you for your help. I didn't realise arduino was so close to C, proem now sorted. My bad for lack of research