Go Down

Topic: Newbie unable to make simple functiuon work ! (Read 451 times) previous topic - next topic

DavidFMarks

  Help please .I am new to this C programming and its driving me up the wall ! I am trying to use a function in a simple program to move a servo. I have checked out the use of functions on the arduino site and in four books and I still can't see what is wrong with the following code which produces the error message "VertEyes was not declared in this scope. I realise that I must be doing something stupid but none of the tutorials have so far enlightened me ! The code is as follows
[//Arduino Servo Test sketch

#include <Servo.h>
Servo VerticalEyes; // Define our Servo


void setup()
{
   VerticalEyes.attach(10); //      connect servo to digital pin 10
   
}

void loop()
{
int vpos=90;                 //define servo position as 90 degrees

VertEyes(vpos);             //call VertEyes function to move servo to vpos
}

int VertEyes(int vpos);                    // set up function VertEyes

{          
VerticalEyes.write(vpos);               // move servo


}    

]


SurferTim

Maybe if you remove that errant semicolon?

Code: [Select]
int VertEyes(int vpos)                    // set up function VertEyes
{           
VerticalEyes.write(vpos);               // move servo
}     

DavidFMarks

Aha ! Thanks for that. I hadn't registered the fact that there is no semicolon after a function declaration.. The bad news is that I still get the same error message when the semicolon is removed.

SurferTim

Code: [Select]
#include <Servo.h>
Servo VerticalEyes; // Define our Servo

void setup()
{
  VerticalEyes.attach(10); //      connect servo to digital pin 10
}

void loop()
{
 int vpos=90;                 //define servo position as 90 degrees
 VertEyes(vpos);             //call VertEyes function to move servo to vpos
}

int VertEyes(int vpos)                    // set up function VertEyes
{          
VerticalEyes.write(vpos);               // move servo
}    

michinyon

Move the VertEyes() function above the loop() function.

So when the compiler gets to the call to VertEyes(vpos) inside loop(),  it knows what it is.

DavidFMarks

Thank a lot folks. My scarecrows eyes are now moving !!

UKHeliBob

What determines whether a function needs to be before loop() ?
I have never needed to do this, particularly as I prefer the loop() function to be near the top of the code where it is easily found.

In this case is it significant that VertEyes is defined as returning an int but doesn't return anything ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Quote
The bad news is that I still get the same error message when the semicolon is removed.

Nice of you to share that message.

Whether the function needs to be defined before loop(), or after, depends on the function arguments. The IDE is able to define function prototypes for functions in the same .ino file that do no use references. The function prototypes are not always positioned correctly, though.

The function prototypes allow for forward references - calling a function that has not yet been defined - because the compiler knows what the function will look like.

Whether the use has to explicitly define/position the function prototype(s) depends on whether the function uses references and whether the sketch uses conditional compilation. Neither appears to be true in this case, but the undisclosed error message would have told us a lot.

Go Up