Pages: [1]   Go Down
Author Topic: Newbie unable to make simple functiuon work !  (Read 294 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  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


 }    

]

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe if you remove that errant semicolon?

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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
}    
Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2051
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 90
Posts: 3528
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: