Society of Robots - Robot Forum
Software => Software => Topic started by: Joker94 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
-
i'll post the code to see if that helps
{
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:
}
}
-
You have an { with no corresponding } on the first line. Delete it and see if that helps.
-
Probably unrelated, but it's
return 0;
and not
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.
-
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
-
hopslink, what you said didn't work, it still asks for the same thing
-
return 0 should be outside the while loop, but still in the main loop.
int main
{
while(1)
{
}
return 0;
}
-
now my code looks like
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'
-
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.
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!
-
great thanks alot
as you have probably guessed this is my first attempt at code.
i'll have a go and see what happens
-
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
-
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?
-
thanks chelmi
yeh i am using the $50 robot library for this.
i'll give it a go and see how i go.
thanks