Compilation error. Pleas help!!!

Hi everyone!

As you see on the subject i have a problem in my arduino project.
This project is about a little robot that cleans and is a bluetooth-controlled via Visual Studio 2010 robot too.

And when i verified the “.ino” project, in this case the name is “Amidai_V2.5.ino”, i got the next error message:

  • “Amidai_V2.5.ino:75:1: error: expected initializer before ‘while’ Compilation error”

And i dont know what to do about it, i just start programming on arduino
NOTE : Some parts of the code are in italian…
NOTE: Some codes i copied from some pages and adapted them according to my project specifications.

Here is the code:

/* Here is the define and void sector
*/
#define DURATA_GIRO 100
#define MOV_I 200
#define MOV_A 10

#define trigger_sx 2
#define echo_sx 4
#define trigger_an 7
#define echo_an 8
#define trigger_dx 9
#define echo_dx 12

#include <Servo.h> 
Servo myservo; 

byte limite=12;
byte byte_S,byte_A,byte_D;

byte valore_lettura;
byte VEL_AVANTI=200;
byte VEL_INDIETRO=80;
byte VEL_LATERALE=90;
int C=0;
long duration;
long lettura_sx;
long lettura_an;
long lettura_dx;
unsigned long time=0;

void fermo(void);
void scansione (void);
void indietro(unsigned char);
void avanti(unsigned char );
void senso_O(void);
void senso_A(void);
void movimenti(void);
void SCRITTURA(void);

//Read string for control the robot.
String readString;

void setup()
{

//This part is for the clean mode.

  myservo.attach(10); 
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(3,OUTPUT);

  pinMode( trigger_sx, OUTPUT );
  pinMode( echo_sx, INPUT );
  pinMode( trigger_dx, OUTPUT );
  pinMode( echo_dx, INPUT );
  pinMode( trigger_an, OUTPUT );
  pinMode( echo_an, INPUT );

//This part is for Control mode.
  Serial.begin(9600);
  delay(2000);


  Serial.begin(9600);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);


}
 
  void loop()
//Here is the sector for the function ( when i press a key in my computer start a function to control or put in automatic clean mode the robot ).
while (Serial.available()) {
delay(10);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;
}
}
if (readString.length() >0) {


if (readString == "k") {
    //This is the function for the automatic clean mode when i press the key "k", as you see above.
  time = millis();

  if(time>=600000) 
  { 
    fermo();
    return;
  }

  scansione();

  movimenti();

}
void fermo(void)
{
  digitalWrite(3,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(11,LOW); 
}
void indietro(unsigned char indietro)
{
  do
  {
    C++;
    digitalWrite(3,LOW);
    analogWrite(5,VEL_INDIETRO);
    analogWrite(6,VEL_INDIETRO);
    digitalWrite(11,LOW);
    delay(1);
  }
  while(C<indietro);
  C=0;
  return;
}
void avanti(unsigned char avanti)
{
  do
  {
    C++;
    analogWrite(3,VEL_AVANTI);
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);
    analogWrite(11,VEL_AVANTI);
    delay(1);
  }
  while(C<avanti);
  C=0;
  return;
}
void senso_O(void)
{
  do
  {
    C++;
    digitalWrite(3,LOW);
    analogWrite(5,VEL_LATERALE);
    digitalWrite(6,LOW);
    analogWrite(11,VEL_LATERALE);
    delay(1);
  }
  while(C<DURATA_GIRO);
  C=0;
  return;
}
void senso_A(void)
{
  do
  {
    C++;
    analogWrite(3,VEL_LATERALE);
    digitalWrite(5,LOW);
    analogWrite(6,VEL_LATERALE);
    digitalWrite(11,LOW);
    delay(1);
  }
  while(C<DURATA_GIRO);
  C=0;
  return;
}
void SCRITTURA(void)
{
  Serial.print("Sx=");
  Serial.print(lettura_sx);
  Serial.print("      AN=");
  Serial.print(lettura_an);
  Serial.print("        Dx=");
  Serial.print(lettura_dx);
  Serial.print("        VL=");
  Serial.println(valore_lettura);

  delay(100);
}

if (readstring == "p"){
//the clean mode finishes here and starts the manual control and the robot advance.
String = "f"
}

//Here i can control the robot completely manual control.

if (readString == "f") {

      digitalWrite(10, HIGH);
      digitalWrite(11, HIGH);
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);

}

if (readString == "b") {

      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
      digitalWrite(12, HIGH);
      digitalWrite(13, HIGH);
}

if (readString == "l") {

      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      digitalWrite(12, LOW);
      digitalWrite(13, HIGH);
}

if (readString == "r") {

      digitalWrite(10, LOW);
      digitalWrite(11, HIGH);
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
}

if (readString == "x") {

      digitalWrite(2, HIGH);

}

if (readString == "y") {


      digitalWrite(2, LOW);
}

if (readString == "w") {

      digitalWrite(3, HIGH);

}

if (readString == "z") {


      digitalWrite(3, LOW);
}


if (readString == "s") {
  
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);
      
}
readString="";
}
}

Thank you for your time and atention.

You have several mismatched {} or missing brackets.
Got thru your code IN REVERSE to identify them, going forward the new 1.6.5. I am using does not identify the {} pairs.

Also “time” must be used somewhere else , so I would change it to myTime. I did not go any further with redefining time - do the brackets first.
It may be OK AFTER you clear the brackets pairs.
You will get more or better error codes after that.

Make sure you have “verbose” turned on ( google it ) it is helpful.

/* Here is the define and void sector
*/
#define DURATA_GIRO 100
#define MOV_I 200
#define MOV_A 10

#define trigger_sx 2
#define echo_sx 4
#define trigger_an 7
#define echo_an 8
#define trigger_dx 9
#define echo_dx 12

#include <Servo.h>
Servo myservo;

byte limite = 12;
byte byte_S, byte_A, byte_D;

byte valore_lettura;
byte VEL_AVANTI = 200;
byte VEL_INDIETRO = 80;
byte VEL_LATERALE = 90;
int C = 0;
long duration;
long lettura_sx;
long lettura_an;
long lettura_dx;
unsigned long mytime = 0;

void fermo(void);
void scansione (void);
void indietro(unsigned char);
void avanti(unsigned char );
void senso_O(void);
void senso_A(void);
void movimenti(void);
void SCRITTURA(void);

//Read string for control the robot.
String readString;

void setup()
{

  //This part is for the clean mode.

  myservo.attach(10);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(3, OUTPUT);

  pinMode( trigger_sx, OUTPUT );
  pinMode( echo_sx, INPUT );
  pinMode( trigger_dx, OUTPUT );
  pinMode( echo_dx, INPUT );
  pinMode( trigger_an, OUTPUT );
  pinMode( echo_an, INPUT );

  //This part is for Control mode.
  Serial.begin(9600);
  delay(2000);


  Serial.begin(9600);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);


}

void loop()
{
  //Here is the sector for the function ( when i press a key in my computer start a function to control or put in automatic clean mode the robot ).
  while (Serial.available()) {
    delay(10);
    if (Serial.available() > 0) {
      char c = Serial.read();
      readString += c;
    }
  };

  if (readString.length() > 0) {


    if (readString == "k") {
      //This is the function for the automatic clean mode when i press the key "k", as you see above.
      mytime = millis();

      if (mytime >= 600000)
      {
        fermo();
        return;
      }

      scansione();

      movimenti();

    }





    void fermo(void)
    {
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(11, LOW);
    }
    void indietro(unsigned char indietro)
    {
      do
      {
        C++;
        digitalWrite(3, LOW);
        analogWrite(5, VEL_INDIETRO);
        analogWrite(6, VEL_INDIETRO);
        digitalWrite(11, LOW);
        delay(1);
      }
      while (C < indietro);
      C = 0;
      return;
    }
    void avanti(unsigned char avanti)
    {
      do
      {
        C++;
        analogWrite(3, VEL_AVANTI);
        digitalWrite(5, LOW);
        digitalWrite(6, LOW);
        analogWrite(11, VEL_AVANTI);
        delay(1);
      }
      while (C < avanti);
      C = 0;
      return;
    }
    void senso_O(void)
    {
      do
      {
        C++;
        digitalWrite(3, LOW);
        analogWrite(5, VEL_LATERALE);
        digitalWrite(6, LOW);
        analogWrite(11, VEL_LATERALE);
        delay(1);
      }
      while (C < DURATA_GIRO);
      C = 0;
      return;
    }
    void senso_A(void)
    {
      do
      {
        C++;
        analogWrite(3, VEL_LATERALE);
        digitalWrite(5, LOW);
        analogWrite(6, VEL_LATERALE);
        digitalWrite(11, LOW);
        delay(1);
      }
      while (C < DURATA_GIRO);
      C = 0;
      return;
    }
    void SCRITTURA(void)
    {
      Serial.print("Sx=");
      Serial.print(lettura_sx);
      Serial.print("      AN=");
      Serial.print(lettura_an);
      Serial.print("        Dx=");
      Serial.print(lettura_dx);
      Serial.print("        VL=");
      Serial.println(valore_lettura);

      delay(100);
    }

    if (readstring == "p") {
      //the clean mode finishes here and starts the manual control and the robot advance.
      String = "f"
    }

    //Here i can control the robot completely manual control.

    if (readString == "f") {

      digitalWrite(10, HIGH);
      digitalWrite(11, HIGH);
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);

    }

    if (readString == "b") {

      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
      digitalWrite(12, HIGH);
      digitalWrite(13, HIGH);
    }

    if (readString == "l") {

      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      digitalWrite(12, LOW);
      digitalWrite(13, HIGH);
    }

    if (readString == "r") {

      digitalWrite(10, LOW);
      digitalWrite(11, HIGH);
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
    }

    if (readString == "x") {

      digitalWrite(2, HIGH);

    }

    if (readString == "y") {


      digitalWrite(2, LOW);
    }

    if (readString == "w") {

      digitalWrite(3, HIGH);

    }

    if (readString == "z") {


      digitalWrite(3, LOW);
    }


    if (readString == "s") {

      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);

    }
    readString = "";
  }
}

Hello and welcome,

You are missing a few { and }, and you cannot have code outside of any function.

Indenting your code correctly is necessary ;)

Right, your formatting is atrocious. If you formatted correctly, or even just used ctrl+t in the Arduino IDE (I realised you mentioned visual studio, but if anything formatting is easier in that!), it would reformat for you and you'd see all the {} you missed out.

You need a { after your void loop().

You have stray if() statements in global space, what are you expecting these to do?

I did try to do Auto Format in 1.6.5. and IT DOES NO longer works! See my other post.

Amidai_V2.5.ino:75:1: error: expected initializer before 'while' Compilation error"

This tells you that the compiler detected an error at, or very close to, line 75 of your code. That is a good place to start looking.

Wow!!! thanks for everyone for helping me!!! I would apply what each one of you tach me. Thanks to -Vaclav -guix -tammytam

And can you tell me if my english is good, medium, or TERRIBLE? because i am not North American...

Calicol: Wow!!! thanks for everyone for helping me!!! I would apply what each one of you tach me. Thanks to -Vaclav -guix -tammytam

And can you tell me if my english is good, medium, or TERRIBLE? because i am not North American...

There is a saying in NA "good enough for government work " - in you case of English usage loosely translated to "if you can get the message across you are doing just fine ".

Majority, but there are notable exceptions, of people participating here are civilized and polite and will not comment on your English.

What IDE release are you using? As I pointed out the latest one (1.6.5) will not help you formatting the code if there are mismatched {} or other item of code , but in you case older release would at least give you error indicating that the code cannot be "Auto Formate(ed) ". It is better than nothing IMHO.

PS "Auto Format" feature, not sure where it is documented , in IDE Tools menu does make your code more humanely readable by adding tabs ( spaces) where appropriate.

Good luck Vaclav