Society of Robots - Robot Forum

Software => Software => Topic started by: nfwill on May 26, 2011, 08:34:12 PM

Title: 50$ Robot: expected identifier or '(' before 'while'
Post by: nfwill on May 26, 2011, 08:34:12 PM
Hello,
I have been following the 50$ robot tutorial and I have only the programming step left!

I added the Photovore_v1.c to source files under the AVR Studio, and followed the tutorial to the point of "Rebuild All"

At that point, I received 1 error and 3 warnings; the error being "expected identifier or '(' before while."

Here is the code that I copied and pasted in:



LED_off();//turn LED on


   while(1)
      {
      //store sensor data
      sensor_left=a2dConvert8bit(5);
      sensor_right=a2dConvert8bit(4);


      //detects more light on left side of robot
      if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
         {//go left
         servo_left(44);
         servo_right(44);
         }

      //detects more light on right side of robot
      else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)
         {//go right
         servo_left(25);
         servo_right(25);
         }

      //light is about equal on both sides
      else
         {//go straight
         servo_left(25);
         servo_right(44);
         }


      /* Servo Test Code
      i=250;
      while(i>0)
         {
         servo_left(40);
         i--;
         }

      i=250;
      while(i>0)
         {
         servo_left(24);
         i--;
         }
      */

      //rprintf("Initialization Complete\r\n");
      
      //output message to serial (use hyperterminal)
      //print("Hello, World! Read My Analog: %u\r\n", sensor_0);

      delay_cycles(500);//a small delay to prevent crazy oscillations
      }





My Intuition of my issue:
I have little knowledge of C; I am somewhat conversant in Python and Basic, I would think there must be "definitions" or parameters that I am missing. The code above is simply the bit that the downloaded Photovore_v1.c file shows to be added.
Any help is appreciated!

nfwill
Title: Re: 50$ Robot: expected identifier or '(' before 'while'
Post by: Admin on May 26, 2011, 08:42:49 PM
It looks right . . . I suspect there is other code above or below that, which could cause a problem . . .

Also, try to match the line number of the error to the actual code.
Title: Re: 50$ Robot: expected identifier or '(' before 'while'
Post by: garrettg84 on May 27, 2011, 07:16:57 AM
is LED_off() supposed to be a function?

the following code would make this a function

Code: [Select]
LED_off() {//turn LED on
   while(1)
      {
      //store sensor data
      sensor_left=a2dConvert8bit(5);
      sensor_right=a2dConvert8bit(4);


      //detects more light on left side of robot
      if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
         {//go left
         servo_left(44);
         servo_right(44);
         }

      //detects more light on right side of robot
      else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)
         {//go right
         servo_left(25);
         servo_right(25);
         }

      //light is about equal on both sides
      else
         {//go straight
         servo_left(25);
         servo_right(44);
         }


      /* Servo Test Code
      i=250;
      while(i>0)
         {
         servo_left(40);
         i--;
         }

      i=250;
      while(i>0)
         {
         servo_left(24);
         i--;
         }
      */

      //rprintf("Initialization Complete\r\n");
     
      //output message to serial (use hyperterminal)
      //print("Hello, World! Read My Analog: %u\r\n", sensor_0);

      delay_cycles(500);//a small delay to prevent crazy oscillations
      }


Based on code spacing and the trailing '}' you have either not copied the entire code, or the code you copied was bunk to begin with.
Title: Re: 50$ Robot: expected identifier or '(' before 'while'
Post by: richiereynolds on May 27, 2011, 09:52:09 AM
Is that the whole file you're posting or a snippet? The whole file should probably have some #includes etc. at the top
Title: Re: 50$ Robot: expected identifier or '(' before 'while'
Post by: nfwill on May 27, 2011, 09:08:57 PM
Thank you for your responses Admin, garrettg84 and richiereynolds.

I first tried garrettg84's code; the compiler unfortunately complained and mentioned more 'undefined variables'.

My problem ended up being I read too much into the process, and richiereynolds implication that a 'snippet' can be the source of my issues helped. The code I did use was a snippet; complications I faced with AVR Studio caused me to use the snippet which at the time made a lot of sense: the 'entire code' mentioned using a specific part of its own code, perhaps I misunderstood. Getting the entire code into AVR through crude 'copying and pasting' rather than browsing through the files allowed it to successfully compile and functions correctly. I am very excited to be finishing up my first robot!

Thank you all for your time and help!

Much Appreciated,

nfwill