Getting the error "not declared in this scope"

Hi all,
I am new to Arduino coding and I'm having trouble getting this code to compile.
I 3D printed an animatronic headstone and the provided code was broken into 3 tabs so I'm not sure If it is in the right place.

The code "functions tab"
void lid_pos(int tu) {
bool tup = tu > lid_p;
int tus = lid_p;
int wait = 40;
int tui = abs(tu - lid_p);

int top = tui;

for (int i = 0; i < top; i++) {
if (tup) {
if (i + tus < tu) {
lid.write(i + tus);
lid_p = tus + i;
}
} else {
if (tus - i > tu) {
lid.write(tus - i);
lid_p = tus - i;
}
}
if (i < 10) {
wait = wait - 1;
}
if (top - i == 20) {
wait = wait + 1;
}
if (top - i <= 10) {
wait = wait + 2;
}
// Serial.println(lid_p);
delay(wait);
}
}

void lidOpen() {
lid_pos(lid_u);
}
void lidClose() {
lid_pos(lid_d);
}

void eye_pos(int tu) {
bool tup = tu > eye_p;
int tus = eye_p;
int wait = 40;
int tui = abs(tu - eye_p);

int top = tui;

for (int i = 0; i < top; i++) {
if (tup) {
if (i + tus < tu) {
eyel.write(i + tus);
eyer.write(i + tus);
eye_p = tus + i;
}
} else {
if (tus - i > tu) {
eyel.write(tus - i);
eyer.write(tus - i);
eye_p = tus - i;
}
}
if (i < 10) {
wait = wait - 1;
}
if (top - i == 20) {
wait = wait + 1;
}
if (top - i <= 10) {
wait = wait + 2;
}
// Serial.println(eye_p);
delay(wait);
}
}

void gendelay() {
int randwait = random(2, 5);
delay(randwait * 60000);
}

Code part 2 "Leota tab"
#include <Servo.h>
#include<SPI.h>

Servo lid;
Servo eyel;
Servo eyer;

int wait;
float v;
float X;
int eye_m = 70;
int eye_r = eye_m - 20;
int eye_l = eye_m + 30;
int lid_m = 30;
int lid_u = lid_m - 30;
int lid_d = lid_m + 45;
float lid_p;
float eye_p;
int head;
int eye_w = 10;
int choice = 0;
int lid_p = lid_m;
int eye_p = eye_m;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("OK!");
eyel.attach(2);
eyer.attach(3);
lid.attach(4);
lid.write(lid_m);

eyel.write(eye_m);
eyer.write(eye_m);

}

void loop() {
choice = random(0, 5);
switch (choice) {
case (0):
eye1();
break;
case (1):
eye2();
break;
case (2):
eye3();
break;
case (3):
eye4();
break;
case (4):
eye5();
break;
case (5):
eye6();
break;
}
gendelay();
}

The code part 3 "movements tab"
void eye1() {
lid_pos(lid_u);
delay(1000);
eye_pos(eye_r);
delay(1000);
eye_pos(eye_l);
delay(1000);
eye_pos(eye_m);
delay(1000);
lid_pos(lid_d);
}

void eye2() {
lidOpen();
delay(1000);
eye_pos(eye_r);
delay(500);
eye_pos(eye_m);
delay(500);
eye_pos(eye_r);
delay(2000);
lidClose();
eye_pos(eye_m);
}

void eye3() {
eye_pos(eye_l);
lidOpen();
eye_pos(eye_r);
eye_pos(eye_l);
lidClose();
eye_pos(eye_m);
delay(1000);
lidOpen();
delay(500);
lidClose();
}

void eye4() {
lidOpen();
eye_pos(eye_l);
delay(3000);
lidClose();
eye_pos(eye_m);
}

void eye5() {
lidOpen();
eye_pos(eye_r);
delay(3000);
lidClose();
eye_pos(eye_l);
lidOpen();
delay(500);
lidClose();
eye_pos(eye_r);
lidOpen();
delay(500);
lidClose();
eye_pos(eye_m);
lidOpen();
delay(500);
lidClose();
}

void eye6() {
lidOpen();
lidClose();
lidOpen();
lidClose();
lidOpen();
lidClose();
lidOpen();
delay(1000);
lidClose();
}

All three were supposed to be placed in one folder,which I did but I keep getting this message:

Arduino: 1.8.19 (Windows 7), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

C:\Users\G2\Documents\Arduino\Leota\functions.ino: In function 'void lid_pos(int)':

functions:3:19: error: 'lid_p' was not declared in this scope

bool tup = tu > lid_p;

               ^~~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:3:19: note: suggested alternative: 'lid_pos'

bool tup = tu > lid_p;

               ^~~~~

               lid_pos

functions:13:9: error: 'lid' was not declared in this scope

     lid.write(i + tus);

     ^~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:13:9: note: suggested alternative: 'ldiv'

     lid.write(i + tus);

     ^~~

     ldiv

functions:18:9: error: 'lid' was not declared in this scope

     lid.write(tus - i);

     ^~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:18:9: note: suggested alternative: 'ldiv'

     lid.write(tus - i);

     ^~~

     ldiv

C:\Users\G2\Documents\Arduino\Leota\functions.ino: In function 'void lidOpen()':

functions:37:11: error: 'lid_u' was not declared in this scope

lid_pos(lid_u);

       ^~~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:37:11: note: suggested alternative: 'lid_pos'

lid_pos(lid_u);

       ^~~~~

       lid_pos

C:\Users\G2\Documents\Arduino\Leota\functions.ino: In function 'void lidClose()':

functions:40:11: error: 'lid_d' was not declared in this scope

lid_pos(lid_d);

       ^~~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:40:11: note: suggested alternative: 'lid_pos'

lid_pos(lid_d);

       ^~~~~

       lid_pos

C:\Users\G2\Documents\Arduino\Leota\functions.ino: In function 'void eye_pos(int)':

functions:45:19: error: 'eye_p' was not declared in this scope

bool tup = tu > eye_p;

               ^~~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:45:19: note: suggested alternative: 'eye6'

bool tup = tu > eye_p;

               ^~~~~

               eye6

functions:55:9: error: 'eyel' was not declared in this scope

     eyel.write(i + tus);

     ^~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:55:9: note: suggested alternative: 'eye6'

     eyel.write(i + tus);

     ^~~~

     eye6

functions:56:9: error: 'eyer' was not declared in this scope

     eyer.write(i + tus);

     ^~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:56:9: note: suggested alternative: 'eye6'

     eyer.write(i + tus);

     ^~~~

     eye6

functions:61:9: error: 'eyel' was not declared in this scope

     eyel.write(tus - i);

     ^~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:61:9: note: suggested alternative: 'eye6'

     eyel.write(tus - i);

     ^~~~

     eye6

functions:62:9: error: 'eyer' was not declared in this scope

     eyer.write(tus - i);

     ^~~~

C:\Users\G2\Documents\Arduino\Leota\functions.ino:62:9: note: suggested alternative: 'eye6'

     eyer.write(tus - i);

     ^~~~

     eye6

C:\Users\G2\Documents\Arduino\Leota\leota.ino: At global scope:

leota:24:5: error: conflicting declaration 'int lid_p'

int lid_p = lid_m;

 ^~~~~

C:\Users\G2\Documents\Arduino\Leota\leota.ino:19:7: note: previous declaration as 'float lid_p'

float lid_p;

   ^~~~~

leota:25:5: error: conflicting declaration 'int eye_p'

int eye_p = eye_m;

 ^~~~~

C:\Users\G2\Documents\Arduino\Leota\leota.ino:20:7: note: previous declaration as 'float eye_p'

float eye_p;

   ^~~~~

Multiple libraries were found for "Servo.h"

Used: C:\Users\G2\Documents\Arduino\libraries\Servo

Not used: C:\Program Files (x86)\Arduino\libraries\Servo

exit status 1

'lid_p' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Where did you find the original code? There are a few problems.

I found it on "Thingiverse" with the 3D print files here:

You must have all three of those .INO files in one folder. They refer to each other through function calls and variable definitions.

Or... concatenate all three... try compiling this...


#include <Servo.h>
#include<SPI.h>

Servo lid;
Servo eyel;
Servo eyer;


int wait;
float v;
float X;
int eye_m = 70;
int eye_r = eye_m - 20;
int eye_l = eye_m + 30;
int lid_m = 30;
int lid_u = lid_m - 30;
int lid_d = lid_m + 45;
float lid_p;
float eye_p;
int head;
int eye_w = 10;
int choice = 0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("OK!");
  eyel.attach(2);
  eyer.attach(3);
  lid.attach(4);
  lid.write(lid_m);
  lid_p = lid_m;
  eyel.write(eye_m);
  eyer.write(eye_m);
  eye_p = eye_m;
}

void loop() {
  choice = random(0, 5);
  switch (choice) {
    case (0):
      eye1();
      break;
    case (1):
      eye2();
      break;
    case (2):
      eye3();
      break;
    case (3):
      eye4();
      break;
    case (4):
      eye5();
      break;
    case (5):
      eye6();
      break;
  }
  gendelay();
}

void eye1() {
  lid_pos(lid_u);
  delay(1000);
  eye_pos(eye_r);
  delay(1000);
  eye_pos(eye_l);
  delay(1000);
  eye_pos(eye_m);
  delay(1000);
  lid_pos(lid_d);
}

void eye2() {
  lidOpen();
  delay(1000);
  eye_pos(eye_r);
  delay(500);
  eye_pos(eye_m);
  delay(500);
  eye_pos(eye_r);
  delay(2000);
  lidClose();
  eye_pos(eye_m);
}

void eye3() {
  eye_pos(eye_l);
  lidOpen();
  eye_pos(eye_r);
  eye_pos(eye_l);
  lidClose();
  eye_pos(eye_m);
  delay(1000);
  lidOpen();
  delay(500);
  lidClose();  
}

void eye4() {
  lidOpen();
  eye_pos(eye_l);
  delay(3000);
  lidClose();
  eye_pos(eye_m);
}

void eye5() {
  lidOpen();
  eye_pos(eye_r);
  delay(3000);
  lidClose();
  eye_pos(eye_l);
  lidOpen();
  delay(500);
  lidClose();
  eye_pos(eye_r);
  lidOpen();
  delay(500);
  lidClose();
  eye_pos(eye_m);
  lidOpen();
  delay(500);
  lidClose();
}

void eye6() {
  lidOpen();
  lidClose();
  lidOpen();
  lidClose();
  lidOpen();
  lidClose();
  lidOpen();
  delay(1000);
  lidClose();
}


void lid_pos(int tu) {
  bool tup = tu > lid_p;
  int tus = lid_p;
  int wait = 40;
  int tui = abs(tu - lid_p);

  int top = tui;

  for (int i = 0; i < top; i++) {
    if (tup) {
      if (i + tus < tu) {
        lid.write(i + tus);
        lid_p = tus + i;
      }
    } else {
      if (tus - i > tu) {
        lid.write(tus - i);
        lid_p = tus - i;
      }
    }
    if (i < 10) {
      wait = wait - 1;
    }
    if (top - i == 20) {
      wait = wait + 1;
    }
    if (top - i <= 10) {
      wait = wait + 2;
    }
//    Serial.println(lid_p);
    delay(wait);
  }
}

void lidOpen() {
  lid_pos(lid_u);
}
void lidClose() {
  lid_pos(lid_d);
}


void eye_pos(int tu) {
  bool tup = tu > eye_p;
  int tus = eye_p;
  int wait = 40;
  int tui = abs(tu - eye_p);

  int top = tui;

  for (int i = 0; i < top; i++) {
    if (tup) {
      if (i + tus < tu) {
        eyel.write(i + tus);
        eyer.write(i + tus);
        eye_p = tus + i;
      }
    } else {
      if (tus - i > tu) {
        eyel.write(tus - i);
        eyer.write(tus - i);
        eye_p = tus - i;
      }
    }
    if (i < 10) {
      wait = wait - 1;
    }
    if (top - i == 20) {
      wait = wait + 1;
    }
    if (top - i <= 10) {
      wait = wait + 2;
    }
//    Serial.println(eye_p);
    delay(wait);
  }
}

void gendelay() {
  int randwait = random(2, 5);
  delay(randwait * 60000);
}
1 Like

That compiled for me
And uploaded!
Thanks for you excellent help! I have an arduino controlled graveyard and I'm hoping to add the Disney headstone this year, Graveyard video link:

I tried putting it all into one, but I didn't get it right. I've done some amazing things with Unos, but I really need to learn coding. It's hard to find the "right" tutorial.

One big overview:

Programming Arduino: