Author Topic: epected identifier?  (Read 2057 times)

0 Members and 1 Guest are viewing this topic.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
epected identifier?
« on: November 27, 2009, 03:45:56 AM »
I have written some code and when i click build the only error is

"expected identifier or '(' before '{' token"

any ideas

greatly appreciated

Joker94

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: epected identifier?
« Reply #1 on: November 27, 2009, 03:50:44 AM »
i'll post the code to see if that helps

Code: [Select]
{
int main(void)

{
//declare variables here
//int i=250;//a 'whatever' variable
int sensor_left=0;//left photoresistor
int threshold=8;

//port D4 low

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

//detects more light on left side of robot
if(sensor_left > threshold)
{//go left
servo_left(44);
return 0:
}
}

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: epected identifier?
« Reply #2 on: November 27, 2009, 05:48:11 AM »
You have an { with no corresponding } on the first line. Delete it and see if that helps.

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
    • Current projects
Re: epected identifier?
« Reply #3 on: November 27, 2009, 07:43:32 AM »
Probably unrelated, but it's

Code: [Select]
return 0;
and not
Code: [Select]
return 0:
And I don't understand why you would do a return 0 here. Do you realize that it will exit the main function? (which is not a good thing to do on a microcontroller)

Chelmi.

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: epected identifier?
« Reply #4 on: November 27, 2009, 05:32:43 PM »
Thanksa for that

thanks for pointing that out chelmi, i did find that bug in my code.

I try what you said hopslink

thanks alot

joker94

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: epected identifier?
« Reply #5 on: November 27, 2009, 05:35:34 PM »
hopslink, what you said didn't work, it still asks for the same thing

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: epected identifier?
« Reply #6 on: November 27, 2009, 05:46:36 PM »
return 0 should be outside the while loop, but still in the main loop.

int main
{
while(1)
{
}
return 0;
}

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: epected identifier?
« Reply #7 on: November 27, 2009, 06:10:21 PM »
now my code looks like

Code: [Select]
int main(void)

{
//declare variables here
//int i=250;//a 'whatever' variable
int sensor_left=0;//left photoresistor
int threshold=8;


//port D4 low

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

//detects more light on left side
if(sensor_left > threshold)
//go left
servo_left(44);

return 0;
}

and the arrors are

good
avr-gcc.exe  -mmcu=atmega8 -Wall -gdwarf-2 -std=gnu99 -DF_CPU=1000000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT LDRrecognition.o -MF dep/LDRrecognition.o.d  -c  ../LDRrecognition.c

warning
../LDRrecognition.c:16: warning: implicit declaration of function 'a2dConvert8bit'

warning
../LDRrecognition.c:22: warning: implicit declaration of function 'servo_left'

good
avr-gcc.exe -mmcu=atmega8 -Wl,-Map=LDRrecognition.map LDRrecognition.o     -o LDRrecognition.elf

error
C:\MYROBOTS\LDRrecognition\default/../LDRrecognition.c:16: undefined reference to `a2dConvert8bit'

« Last Edit: November 27, 2009, 06:13:00 PM by Joker94 »

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: epected identifier?
« Reply #8 on: November 27, 2009, 08:09:53 PM »
Sorry, yes you will get the same error again as there was another missing closing brace } towards the end of the code. I missed it before as the code is not laid out particularly tidily, I blame the example you are following  :P (that, and I wasn't looking hard enough  :-[). If you keep braces and indenting well organised this is easy to spot. Take a peek at the code below and hopefully you will see what I mean. Corresponding braces align vertically and the indenting makes them obvious.

Your latest code has solved the brace imbalance, but you have removed the braces for the if statement and shortened the while loop so it no longer includes the if statement. This means that your if code will never be executed. I have put these back below and moved the return statement to it's proper place outside the while loop.

Code: [Select]
int main()
{
//declare variables here
//int i=250;//a 'whatever' variable
int sensor_left=0;//left photoresistor
int threshold=8;

//port D4 low

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

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

}

return 0;
}
 

This is the code tidied. Unfortunately it still won't compile without errors but now they are due to missing function code. The servo_left()  and a2dConvert8bit() functions are missing. Typically these are in a different file which must be added to this one with a #include filename statement. You will also need some initialisation code for the a2d and the ports. Hope this helps! 

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: epected identifier?
« Reply #9 on: November 27, 2009, 11:30:25 PM »
great thanks alot

as you have probably guessed this is my first attempt at code.

i'll have a go and see what happens

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: epected identifier?
« Reply #10 on: November 28, 2009, 01:12:53 AM »
well, now only one error comes up.

error
C:\MYROBOTS\LDRrecognition\default/../LDRrecognition.c:17: undefined reference to `a2dConvert8bit'
make: *** [LDRrecognition.elf] Error 1

i am not sure what to do about it

thanks again

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
    • Current projects
Re: epected identifier?
« Reply #11 on: November 28, 2009, 11:22:40 AM »
well, now only one error comes up.

error
C:\MYROBOTS\LDRrecognition\default/../LDRrecognition.c:17: undefined reference to `a2dConvert8bit'
make: *** [LDRrecognition.elf] Error 1

i am not sure what to do about it

thanks again

You used the function a2dConvert8bit in your code. You've probably added a #include statement to declare this function and get rid of the warning about implicit declaration. However, to produce the final executable, the compiler needs to know the content of the function a2dConvert8bit. The .h just tells him "this is how one should call a2dConvert8bit" but does not actually tells what this function does. This is defined somewhere else in a library. So you need to tell the compiler to look in this library for undefined references.

What environment are you using? winAVR? this a2dConvert8bit function is part of the $50 robot?

Offline Joker94Topic starter

  • Supreme Robot
  • *****
  • Posts: 1,119
  • Helpful? 26
Re: epected identifier?
« Reply #12 on: November 28, 2009, 03:56:44 PM »
thanks chelmi

yeh i am using the $50 robot library for this.

i'll give it a go and see how i go.

thanks

 


data_list