Servo not declared

Hi,

I know this question has been answered before, but all the answers are specific to the user’s code and they do not help me, for me I face the issue {‘servo’ was not declared in this scope} for every code I try to upload into arduino uno R3, so I do not understand whay I am facing this issue. I would also post the error messages, but they are extremely long and exceed the word limit. I will try to provide as many details regardless.

My project is building a prosthetic arm using the arm model from inmoov, an easyVR shield & module plus 3, an arduino uno R3, breadboard, a 9v battery with a connector, a breadboard, several jumper wires and 5 motors (MG 995).

I have built my arduino circuit, and its based off a proper online schematic, which I have attached below. Please keep in mind I am only using 5 motors, one for each finger of my arm, and I have replaced the 4 aa batteries with a 9v battery. Please let me know if there is an issue with this, I am very new so I am not very familiar with this.

The project I am doing and the codes I am using are based off of and come from: -

  1. DIY Prosthetic Hand & Forearm (Voice Controlled) : 14 Steps (with Pictures) - Instructables
  2. http://inmoov.fr/hand-and-forarm/

To restate, I face the error {‘servo’ was not declared in this scope} for several different codes, all from different places. The codes I have used were not made by me but have worked for everyone else, or at least they have not faced this error, so I am wondering why I am facing it.

Please let me know what the issue is, and how I can fix this. This is the last stage of my project, and it is really frustrating as this seems like quite a small error.

Thanks

control_6_sg90s_independently_using_arduino.pdf (387 KB)

Post your code

The error messages are too long, but I can post the original codes. There are mainly 2 I will be using, which are :

For setting the motors at 90 degrees:

[pre]#include <Servo.h>

Servo servothumb; // Define thumb servo
Servo servoindex; // Define index servo
Servo servomajeure;
Servo servoringfinger;
Servo servopinky;
Servo servowrist;
Servo servobiceps;
Servo servorotate;
Servo servoshoulder;
Servo servoomoplat;
Servo servoneck;
Servo servorothead;

void setup() { 
 servothumb.attach(2); // Set thumb servo to digital pin 2
 servoindex.attach(3); // Set index servo to digital pin 3
 servomajeure.attach(4);
 servoringfinger.attach(5);
 servopinky.attach(6);
 servowrist.attach(7);
 servobiceps.attach(8);
 servorotate.attach(9);
 servoshoulder.attach(10);
 servoomoplat.attach(11);
 servoneck.attach(12);
 servorothead.attach(13);
 
} 

void loop() { // Loop through motion tests
 alltovirtual(); // Example: alltovirtual
 delay(4000); // Wait 4000 milliseconds (4 seconds)
//alltorest(); // Uncomment to use this
//delay(4000); // Uncomment to use this
//alltomax(); // Uncomment to use this
//delay(2000); // Uncomment to use this
 
 
 
}
// Motion to set the servo into "virtual" 0 position: alltovirtual
void alltovirtual() { 
 servothumb.write(0);
 servoindex.write(0);
 servomajeure.write(0);
 servoringfinger.write(0);
 servopinky.write(0);
 servowrist.write(0);
 servobiceps.write(0); 
 servorotate.write(20); //Never less then (20 degree)
 servoshoulder.write(30); //Never less then (30 degree)
 servoomoplat.write(10); //Never less then (10 degree)
 servoneck.write(0);
 servorothead.write(0);
}
// Motion to set the servo into "rest" position: alltorest
void alltorest() { 
 servothumb.write(0);
 servoindex.write(0);
 servomajeure.write(0);
 servoringfinger.write(0);
 servopinky.write(0);
 servowrist.write(0);
 servobiceps.write(0); 
 servorotate.write(90); //Never less then (20 degree)
 servoshoulder.write(30); //Never less then (30 degree)
 servoomoplat.write(10); //Never less then (10 degree)
 servoneck.write(90);
 servorothead.write(90);
}



// Motion to set the servo into "max" position: alltomax
void alltomax() {
 servothumb.write(180);
 servoindex.write(180);
 servomajeure.write(180);
 servoringfinger.write(180);
 servopinky.write(180);
 servowrist.write(180);
 servobiceps.write(85); //Never more then (85 or 90degree)
 servorotate.write(110); //Never more then (110 degree)
 servoshoulder.write(130); //Never more then (130 degree)
 servoomoplat.write(70); //Never more then (70 degree)
 servoneck.write(180);
 servorothead.write(180);
 
}
[pre]
[pre]
[pre]
[pre]


[b]The main code I need:
[/b]

[/pre]

[/pre]

[/pre]
[/pre]

[pre]#include <Servo.h>
Servo pinky, ringF, middleF, indexF, thumb;
int bend = 800, straight = 2200, half = 1800, wait = 500, pbend = 2200, pstraight = 800, phalf = 1300;

#if defined(ARDUINO) && ARDUINO >= 100
 #include "Arduino.h"
 #include "SoftwareSerial.h"
 SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
 #include "WProgram.h"
 #include "NewSoftSerial.h"
 NewSoftSerial port(12,13);
#endif

#include "EasyVR.h"
EasyVR easyvr(port);

//Groups and Commands
enum Groups
{
 GROUP_0 = 0,
 GROUP_1 = 1,
};

enum Group0 
{
 G0_JILL = 0,
};

enum Group1 
{
 G1_TEST = 0,
 G1_PINCH = 1,
 G1_GRAB = 2,
 G1_ROCK = 3,
 G1_GNARLY = 4,
 G1_POINT = 5,
 G1_PEACE = 6,
 G1_FLICK = 7,
 G1_NEUTRAL = 8,
 G1_TYPE = 9,
 G1_RELAX = 10,
 G1_MOUSE = 11,
 G1_WAVE = 12,
 G1_THUMBS_UP = 13,
 G1_PENCIL = 14,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
 pinky.attach(3);
 ringF.attach(5);
 middleF.attach(6);
 indexF.attach(9);
 thumb.attach(10);
 neutral();
 delay(500);
 
 // bridge mode?
 if (bridge.check())
 {
 cli();
 bridge.loop(0, 1, 12, 13);
 }
 // run normally
 Serial.begin(9600);
 port.begin(9600);

 if (!easyvr.detect())
 {
 Serial.println("EasyVR not detected!");
 for (;;);
 }

 easyvr.setPinOutput(EasyVR::IO1, LOW);
 Serial.println("EasyVR detected!");
 easyvr.setTimeout(5);
 easyvr.setLanguage(0);

 group = EasyVR::TRIGGER; //<-- start group (customize)
}

void action();

void loop()
{
 easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)

 Serial.print("Say a command in Group ");
 Serial.println(group);
 easyvr.recognizeCommand(group);

 do
 {
 // can do some processing while waiting for a spoken command
 }
 while (!easyvr.hasFinished());
 
 easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off

 idx = easyvr.getWord();
 if (idx >= 0)
 {
 // built-in trigger (ROBOT)
 group = GROUP_1;
 return;
 }
 idx = easyvr.getCommand();
 if (idx >= 0)
 {
 // print debug message
 uint8_t train = 0;
 char name[32];
 Serial.print("Command: ");
 Serial.print(idx);
 if (easyvr.dumpCommand(group, idx, name, train))
 {
 Serial.print(" = ");
 Serial.println(name);
 }
 else
 Serial.println();
 easyvr.playSound(0, EasyVR::VOL_FULL);
 // perform some action
 action();
 }
 else // errors or timeout
 {
 if (easyvr.isTimeout())
 Serial.println("Timed out, try again...");
 int16_t err = easyvr.getError();
 if (err >= 0)
 {
 Serial.print("Error ");
 Serial.println(err, HEX);
 }
 }
}

void action()
{
 switch (group)
 {
 case GROUP_0:
 switch (idx)
 {
 case G0_JILL:
 // write your action code here
 group = GROUP_1;
 break;
 }
 break;
 case GROUP_1:
 switch (idx)
 {
 Serial.println("here");
 case G1_TEST:
 test();
 break;
 case G1_PINCH:
 pinch();
 break;
 case G1_GRAB:
 grab();
 break;
 case G1_ROCK:
 rock();
 break;
 case G1_GNARLY:
 gnarly();
 break;
 case G1_POINT:
 point();
 break;
 case G1_PEACE:
 peace();
 break;
 case G1_FLICK:
 flick();
 break;
 case G1_NEUTRAL:
 neutral();
 break;
 case G1_TYPE:
 type();
 break;
 case G1_RELAX:
 neutral();
 group = GROUP_0;
 break;
 case G1_MOUSE:
 mouse();
 break;
 case G1_WAVE:
 wave();
 break;
 case G1_THUMBS_UP:
 thumbsup();
 break;
 case G1_PENCIL:
 pencil();
 break;
 }
 break;
 }
}

void test()
{
 Serial.println("HERE");
 drive(pinky, pbend);
 drive(pinky, pstraight);
 drive(ringF, bend);
 drive(ringF, straight);
 drive(middleF, bend);
 drive(middleF, straight);
 drive(indexF, bend);
 drive(indexF, straight);
 drive(thumb, bend);
 drive(thumb, straight);
}

void pinch()
{
 qDrive(thumb, bend);
 qDrive(ringF, straight);
 qDrive(middleF, straight);
 qDrive(pinky, pstraight);
 delay(200);
 qDrive(indexF, bend);
}

void grab()
{
 qDrive(indexF, bend);
 qDrive(thumb, bend);
 qDrive(ringF, bend);
 qDrive(middleF, bend);
 qDrive(pinky, pbend);
}

void rock()
{
 qDrive(middleF, bend);
 delay(200);
 qDrive(indexF, straight);
 qDrive(thumb, straight);
 qDrive(ringF, bend);
 qDrive(pinky, pstraight);
}

void gnarly()
{
 qDrive(middleF, bend);
 delay(200);
 qDrive(indexF, bend);
 qDrive(thumb, straight);
 qDrive(ringF, bend);
 qDrive(pinky, pstraight);
}

void point()
{
 qDrive(indexF, straight);
 qDrive(thumb, bend);
 qDrive(ringF, bend);
 qDrive(middleF, bend);
 qDrive(pinky, pbend);
}

void peace()
{
 qDrive(indexF, straight);
 qDrive(thumb, bend);
 qDrive(ringF, bend);
 qDrive(middleF, straight);
 qDrive(pinky, pbend);
}

void flick()
{
 qDrive(indexF, bend);
 qDrive(thumb, bend);
 qDrive(ringF, bend);
 qDrive(middleF, straight);
 qDrive(pinky, pbend);
}

void neutral()
{
 qDrive(indexF, straight);
 qDrive(thumb, straight);
 qDrive(ringF, straight);
 qDrive(middleF, straight);
 qDrive(pinky, pstraight);
}

void type()
{
 qDrive(thumb, half);
 qDrive(pinky, phalf);
 qDrive(indexF, half);
 qDrive(middleF, half);
 qDrive(ringF, half);
}

void mouse()
{
 qDrive(thumb, bend);
 qDrive(pinky, pbend);
 qDrive(indexF, bend);
 qDrive(middleF, straight);
 qDrive(ringF, straight);
}

void wave()
{
 for(int i = 0; i < 3; i++)
 {
 qDrive(middleF, bend);
 delay(200);
 qDrive(thumb, straight);
 qDrive(pinky, pbend);
 qDrive(indexF, bend);
 qDrive(ringF, bend);
 delay(500);
 neutral();
 delay(500);
 }
}

void thumbsup()
{
 qDrive(middleF, bend);
 delay(200);
 qDrive(thumb, straight);
 qDrive(pinky, pbend);
 qDrive(indexF, bend);
 qDrive(ringF, bend);
}

void pencil()
{
 qDrive(middleF, bend);
 delay(200);
 qDrive(thumb, bend);
 qDrive(pinky, pbend);
 qDrive(ringF, bend);
 delay(500);
 qDrive(indexF, bend);
}

void drive(Servo s, int pos)
{
 s.writeMicroseconds(pos);
 delay(wait);
}

void qDrive(Servo s, int pos)
{
 s.writeMicroseconds(pos);
}
[pre]
[pre][pre]
[pre][pre]

[b]Please let me know if anything else is needed.


Thanks[/b]

[/pre][/pre][/pre][/pre][/pre]

Yes - you need to use code & /code tags when posting. Easy way is to use the </> button on the menu, paste your code inside the box that opens.

Like so.

Thanks but I still don't know what went wrong and How I can resolve this issue.

void drive(Servo s, int pos)
{
 s.writeMicroseconds(pos);
 delay(wait);
}

void qDrive(Servo s, int pos)
{
 s.writeMicroseconds(pos);
}[color=#222222]

I’m guessing, as you did NOT post the error message, because its to long, that those 2 functions are the issue.

Of course one could use the internet and type “arduino pass servo object into a function” and get results; like https://forum.arduino.cc/index.php?topic=45697.0 and if that’s not the issue, POST THE DANG ERROR MESSGAE.

As I have mentioned previously, I am UNABLE to post the error messages because it exceeds the word count.

For one of the error codes it says 'EasyVRBridge' was not declared in this scope.

For the code that sets the motors at 90 degrees, the error that comes up says 'servo' was not declared in this scope.

C:\Users\ARYAND~1\AppData\Local\Temp\arduino_modified_sketch_953769\EasyVRBridge.ino: In function 'void loop()':

EasyVRBridge:150:1: error: 'EasyVRBridge' was not declared in this scope

EasyVRBridge bridge;

^~~~~~~~~~~~

C:\Users\ARYAND~1\AppData\Local\Temp\arduino_modified_sketch_953769\EasyVRBridge.ino:150:1: note: suggested alternative: 'EasyVR'

EasyVRBridge bridge;

^~~~~~~~~~~~

EasyVR

EasyVRBridge:155:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:191:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:246:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:314:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:329:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:339:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:348:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:358:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:368:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:377:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:386:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:395:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:404:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:413:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:422:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:438:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:448:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:459:1: error: a function-definition is not allowed here before '{' token

{

^

EasyVRBridge:465:1: error: a function-definition is not allowed here before '{' token

{

^

Multiple libraries were found for "Servo.h"

Used: C:\Users\Aryan Dahiya\Documents\Arduino\libraries\Servo

Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\libraries\Servo

Using library SoftwareSerial at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SoftwareSerial

Using library EasyVR at version 1.11.1 in folder: C:\Users\Aryan Dahiya\Documents\Arduino\libraries\EasyVR

Using library Servo at version 1.1.7 in folder: C:\Users\Aryan Dahiya\Documents\Arduino\libraries\Servo

exit status 1

'EasyVRBridge' was not declared in this scope

Of course the error message is telling you the issue.
Take this line of code that you wrote

EasyVR easyvr(port);

and this error message

'EasyVRBridge' was not declared in this scope

Can you see why the error message was produced?

easyvr and EasyVRBridge are not the same thing.

With that clue you should be able to hunt down the rest of the deceleration issues.

And for the other issue, properly FORMAT your code and you should be able to see why and where.

After you fix your code, post the fixed code here and the other error messages you receive or not.

Sorry,

I am new to this and don't quite understand.
How exactly do I fix this issue? what exact changes do I need to make? If its not worth too much trouble, could you please help me out here?

The first code you posted compiles with no errors. The second code has errors but none that I can see containing "servo".

If the full error messages are too big to post then all you need to do is ATTACH them to a message. And also post the exact code that the errors relate to and say what Arduino you are compiling the code for.

Steve

If there are no errors for the first one, why am I getting this error in the first place? For the second one, the only errors are regarding 'servo' so this makes no sense.

I am still as uncertain as I was before, what do I do to resolve this issue?

What is wrong with the code?

Let's take your sketches one at a time. Post the first code in code tags and attach a file with the errors you get. FWIW, the first one compiles fine for me too, so seeing the actual error messages is crucial.

Ok, the first code:

#include <Servo.h>

Servo servothumb;          // Define thumb servo
Servo servoindex;          // Define index servo
Servo servomajeure;
Servo servoringfinger;
Servo servopinky;
Servo servowrist;
Servo servobiceps;
Servo servorotate;
Servo servoshoulder;
Servo servoomoplat;
Servo servoneck;
Servo servorothead;

void setup() { 
  servothumb.attach(2);  // Set thumb servo to digital pin 2
  servoindex.attach(3);  // Set index servo to digital pin 3
  servomajeure.attach(4);
  servoringfinger.attach(5);
  servopinky.attach(6);
  servowrist.attach(7);
  servobiceps.attach(8);
  servorotate.attach(9);
  servoshoulder.attach(10);
  servoomoplat.attach(11);
  servoneck.attach(12);
  servorothead.attach(13);
  
} 

void loop() {            // Loop through motion tests
  alltovirtual();        // Example: alltovirtual
  delay(4000);           // Wait 4000 milliseconds (4 seconds)
//alltorest();           // Uncomment to use this
//delay(4000);           // Uncomment to use this
//alltomax();            // Uncomment to use this
//delay(2000);           // Uncomment to use this
 
  
 
}
// Motion to set the servo into "virtual" 0 position: alltovirtual
void alltovirtual() {         
  servothumb.write(0);
  servoindex.write(0);
  servomajeure.write(0);
  servoringfinger.write(0);
  servopinky.write(0);
  servowrist.write(0);
  servobiceps.write(0);  
  servorotate.write(20);    //Never less then (20 degree)
  servoshoulder.write(30);  //Never less then (30 degree)
  servoomoplat.write(10);   //Never less then (10 degree)
  servoneck.write(0);
  servorothead.write(0);
}
// Motion to set the servo into "rest" position: alltorest
void alltorest() {         
   servothumb.write(0);
  servoindex.write(0);
  servomajeure.write(0);
  servoringfinger.write(0);
  servopinky.write(0);
  servowrist.write(0);
  servobiceps.write(0);     
  servorotate.write(90);    //Never less then (20 degree)
  servoshoulder.write(30);  //Never less then (30 degree)
  servoomoplat.write(10);   //Never less then (10 degree)
  servoneck.write(90);
  servorothead.write(90);
}



// Motion to set the servo into "max" position: alltomax
void alltomax() {
  servothumb.write(180);
  servoindex.write(180);
  servomajeure.write(180);
  servoringfinger.write(180);
  servopinky.write(180);
  servowrist.write(180);
  servobiceps.write(85);      //Never more then (85 or 90degree)
  servorotate.write(110);     //Never more then (110 degree)
  servoshoulder.write(130);   //Never more then (130 degree)
  servoomoplat.write(70);     //Never more then (70 degree)
  servoneck.write(180);
  servorothead.write(180);
 
}

error message when compiling (cut down, full error message too long):

   C:\Users\ARYAND~1\AppData\Local\Temp\arduino_modified_sketch_700764\sketch_nov19b.ino: In function 'void setup()':
sketch_nov19b:18:14: error: a function-definition is not allowed here before '{' token
 void setup() {
              ^
sketch_nov19b:34:13: error: a function-definition is not allowed here before '{' token
 void loop() {            // Loop through motion tests
             ^
sketch_nov19b:46:21: error: a function-definition is not allowed here before '{' token
 void alltovirtual() {
                     ^
sketch_nov19b:61:18: error: a function-definition is not allowed here before '{' token
 void alltorest() {
                  ^
sketch_nov19b:79:17: error: a function-definition is not allowed here before '{' token
 void alltomax() {
                 ^
Multiple libraries were found for "Servo.h"
 Used: C:\Users\Aryan Dahiya\Documents\Arduino\libraries\Servo
 Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\libraries\Servo
Using library Servo at version 1.1.7 in folder: C:\Users\Aryan Dahiya\Documents\Arduino\libraries\Servo 
exit status 1
a function-definition is not allowed here before '{' token

There's nothing wrong with that code. It compiles fine for me.

Perhaps you've got some control characters in there that didn't make it to the web. Try attaching the ino file instead.

What version of the IDE are you using?

I am using version 1.8.13

May I know what exactly you did?
did you just copy/paste into a new sketch, and press verify/compile?

aryand323:
I am using version 1.8.13

May I know what exactly you did?
did you just copy/paste into a new sketch, and press verify/compile?

That's it. I probably did an auto format too - it's a reflex, so I'm not sure but it wouldn't impact compilation.

That is really strange. What should I do now?

You might try reinstalling the IDE - perhaps it's corrupted.

Before that, I would try and compile some of the examples that come with the IDE. If they're ok, maybe copy a subset of that latest code and try and compile that and keep removing stuff until you get a clean compile.