Compilation - error: redefinition of 'Servo myservo' Servo myservo;

Trying to compile this sketch, I get the errors shown. I have other sketches with the same code for using a servo and they compile correctly. Please advise what is wrong here.

Dale Gloer

type or paste code here
```cpp

#include <Servo.h>
 Servo myservo;    // create servo object to control a servo

int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 90);     // scale it for use with the servo (value between 0 and 90)
  Serial.println(val);
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(100);                           // waits for the servo to get there
}

+++++++++++++ error messages +++++++++++
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino:3:8: error: redefinition of 'Servo myservo'
Servo myservo; // create servo object to control a servo
^~~~~~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch.ino:4:7: note: 'Servo myservo' previously declared here
Servo myservo; // create servo object to control a servo
^~~~~~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino:5:5: error: redefinition of 'int potpin'
int potpin = A0; // analog pin used to connect the potentiometer
^~~~~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch.ino:6:5: note: 'int potpin' previously defined here
int potpin = A0; // analog pin used to connect the potentiometer
^~~~~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino:6:5: error: redefinition of 'int val'
int val; // variable to read the value from the analog pin
^~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch.ino:7:5: note: 'int val' previously declared here
int val; // variable to read the value from the analog pin
^~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino: In function 'void setup()':
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino:8:6: error: redefinition of 'void setup()'
void setup() {
^~~~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch.ino:9:6: note: 'void setup()' previously defined here
void setup() {
^~~~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino: In function 'void loop()':
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino:13:6: error: redefinition of 'void loop()'
void loop() {
^~~~
D:\Files\3D prints\Road Chain\starter kit\arduino\rcs_arduino_sketch\rcs_arduino_sketch.ino:14:6: note: 'void loop()' previously defined here
void loop() {
^~~~

exit status 1

Compilation error: redefinition of 'Servo myservo'

D:\Files\...\arduino\rcs_arduino_sketch\rcs_arduino_sketch_reversable.ino
D:\Files\...\arduino\rcs_arduino_sketch\rcs_arduino_sketch.ino

Two .ino files, both with setup(), loop() and Servo servo; in the same directory will do that.

Thanks for catching that. I think this is the first time I have ever had more than one sketch in a directory (still a sort of newbie) and I never expected or even noticed that there were two tabs open. I moved each sketch to a unique directory and all is well.

Thanks you for your quick reply. Dale Gloer

You have misunderstood what a sketch is. Because many sketches consist of only a single .ino file, it is easy to get the misconception that the .ino file is the sketch. But the sketch is actually the directory, and all source files in that directory are compiled into a single program. This is useful because it allows you to organize complex programs into multiple files.

You are correct about my misunderstanding - and thank you for explaining what I didn't know.

Closing this issue.
Dale /Gloer

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.