Need help with project code

Hello! I am super new to coding and am trying to use an Arduino to move two servo motors. I have an error message:

"Compilation error: 'servo1Pin' does not name a type"
/private/var/folders/kk/phc3ywzj0bj30s9b3c4gh8xh0000gn/T/.arduinoIDE-unsaved202628-99380-vnrfmz.4x06/sketch_mar8a/sketch_mar8a.ino:13:1: error: 'servo1Pin' does not name a type"
 
servo1Pin.write(pos1);
 ^~~~~~~~~
/private/var/folders/kk/phc3ywzj0bj30s9b3c4gh8xh0000gn/T/.arduinoIDE-unsaved202628-99380-vnrfmz.4x06/sketch_mar8a/sketch_mar8a.ino:14:1: error: 'servo2Pin' does not name a type
 servo2Pin.write(pos2);
 ^~~~~~~~~
/private/var/folders/kk/phc3ywzj0bj30s9b3c4gh8xh0000gn/T/.arduinoIDE-unsaved202628-99380-vnrfmz.4x06/sketch_mar8a/sketch_mar8a.ino:17:1: error: 'pos1' does not name a type
 pos1 += 1;
 ^~~~
/private/var/folders/kk/phc3ywzj0bj30s9b3c4gh8xh0000gn/T/.arduinoIDE-unsaved202628-99380-vnrfmz.4x06/sketch_mar8a/sketch_mar8a.ino:18:1: error: expected unqualified-id before 'if'
 if (pos1 > 180) {
 ^~
/private/var/folders/kk/phc3ywzj0bj30s9b3c4gh8xh0000gn/T/.arduinoIDE-unsaved202628-99380-vnrfmz.4x06/sketch_mar8a/sketch_mar8a.ino:21:1: error: expected unqualified-id before '{' token
 {
 ^
exit status 1

Compilation error: 'servo1Pin' does not name a type

here is my code:

void loop() {
#include <Servo.h>

  Servo Servo1;

  int servo1Pin = 9;
  int servo2Pin = 8;
}
int pos1 = 0;
int pos2 = 180;

//set servo position
servo1Pin.write(pos1);
servo2Pin.write(pos2);

//calculate next position
pos1 += 1;
if (pos1 > 180) {
  pos1 = 0;
}
{
  pos2 -= 1;
  if (pos2 < 0) {
    pos2 = 180;
  }
  delay(15);
}

#include inside a function.

What are presumable global declarations (Servo, servo1pin, servo2pin) inside a function.

Code outside a function (everything from //set servo position onwards).

No setup() function.

No servo.attach().

Treating int variables like they're an instance of the Servo type.


The Servo library comes with a couple of perfectly serviceable, simple examples of how to use it. I suggest you start there.

Back in the old days when source code was on 80 col cards, I would think you either dropped the deck or shuffled the deck. Pure nonsense.

Start here

FYI the compile error log also goes in code tags.

sorry i fixed it lol

this was actually super helpful thank you so much

Describe how you fixed this to further enforce what you have learned.