Error code in setting up

Hi
I'm new to this, so please bear with me. i have been trying to calibrate some servos from program i copied and put into sketch. however every time I verify the program i keep getting the following error message Compilation error: redefinition of 'void setup()

hope any one can help

thanks

Welcome to the forum.

It sounds like you have two setup() functions.

You need to post the full code for people to be able to see what's going on.
See the instructions in the 'How to get the best out of this forum' post for how to post code.

Also post the full compile output - use the same method as for code.

1 Like

The messages are telling you have the "void setup()" function specified two or more times in your program. Change one to "void setup1();" it will probably compile but not work. You need to combine the two functions into one.

This could also mean two .INO files in the same sketch folder.

1 Like

The full message would include the file name(s) and line numbers at which the two (or more) definitions were found

good morning
many thanks for the quick replies, just a bit of bit of back ground, this code is used for calibrating servos to control model railway point / switches.

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates

uint8_t servonum = 0; // address of servo to be calibrated
String readString;
int pos;

void setup() {
Serial.begin(9600);
pos=1500; // Initial position
pwm.begin();
pwm.setOscillatorFrequency(25000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates

Serial.println("Servo calibration");
Serial.println("Use this to calibrate your servo to find the range of movement required");
Serial.println("The servo should start close to the centre of the range");
Serial.println("Type a value followed by a + to move in one direction or a valu followed by a - to move in the other direction");
Serial.println("For example 100+ to 200-");
Serial.println("To move to a specific location use strings like 900x or 1800x for new servo position");
Serial.println("Move the servo to find the required range for whatever you're operating.");
Serial.println("Servos min and max can vary, try the 1000 - 2000 range to start with.");
Serial.println("WARNING: Exceeding the max range could damage the servo.");
Serial.println();
pwm.writeMicroseconds(servonum, pos);
Serial.println("Centre point:");
Serial.println(pos);
delay(10);
}

void loop()
{
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {

if(readString.indexOf('x') >0) {
  pos = readString.toInt();
}

if(readString.indexOf('+') >0) {
  pos = pos + readString.toInt();
}

if(readString.indexOf('-') >0) {
  pos = pos - readString.toInt();
}

pwm.writeMicroseconds(servonum, pos);
Serial.println(pos);
readString=""; //empty for next input

}
}

i copied this from a guys site who was instructing our to make these work.

error message
C:\Users\peter\AppData\Local\Temp.arduinoIDE-unsaved2023813-10404-1p4xyca.tkzk\sketch_sep13a\sketch_sep13a.ino: In function 'void setup()':
C:\Users\peter\AppData\Local\Temp.arduinoIDE-unsaved2023813-10404-1p4xyca.tkzk\sketch_sep13a\sketch_sep13a.ino:22:6: error: redefinition of 'void setup()'
void setup() {
^~~~~
C:\Users\peter\AppData\Local\Temp.arduinoIDE-unsaved2023813-10404-1p4xyca.tkzk\sketch_sep13a\sketch_sep13a.ino:1:6: note: 'void setup()' previously defined here
void setup() {
^~~~~
C:\Users\peter\AppData\Local\Temp.arduinoIDE-unsaved2023813-10404-1p4xyca.tkzk\sketch_sep13a\sketch_sep13a.ino: In function 'void loop()':
C:\Users\peter\AppData\Local\Temp.arduinoIDE-unsaved2023813-10404-1p4xyca.tkzk\sketch_sep13a\sketch_sep13a.ino:45:6: error: redefinition of 'void loop()'
void loop()
^~~~
C:\Users\peter\AppData\Local\Temp.arduinoIDE-unsaved2023813-10404-1p4xyca.tkzk\sketch_sep13a\sketch_sep13a.ino:6:6: note: 'void loop()' previously defined here
void loop() {
^~~~

exit status 1

Compilation error: redefinition of 'void setup()'

hope this all makes sense, as im at a loss

thanks again

You seem to have missed the instructions in the 'How to get the best out of this forum' post for how to post code?

You need to use the <CODE/> button.

The error messages tell you where you two definitions of void setup() are:

The bits in bold gives you the file name, line number, and position within the line.

So take a look at those two places, and decide which one to keep. Or merge the two into one.

Hi, @shuntdaddy
Welcome to the forum.

To add code please click this link;

It will show you how to put your code in the post with a scrolling window.

Tom... :grinning: :+1: :coffee: :australia:

hi again
firstly many apologies, for the code coming out wrong. i went into the arduino setting and used tools and copied and paste, i think i may have missed something.
awneil. many thanks for looking at this, and i can see on the output message what you mean, however on the progam list it just says void setup (){ in line 20. maybe im reading it wrong, i will persevere with it though. the question i have is how do you merge or remove it.

i thank you all for your time
thanks

The error message also points to one in line 1.

Are you sure you copied & pasted the entire sketch?

Please give a link to that site & what you copied.

Blindly copying from random internet sites may not be the greatest approach...

Thanks awneil. hope this helps. :slight_smile:

Hi, @shuntdaddy

I put the code in post #6 into the IDE and compiled it for the UNO.
No compilation errors.
What controller are you compiling for?

This is your code with [ CTRL ] [ T ] format used to ident the lines.

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates

uint8_t servonum = 0; // address of servo to be calibrated
String readString;
int pos;

void setup()
{
  Serial.begin(9600);
  pos = 1500; // Initial position
  pwm.begin();
  pwm.setOscillatorFrequency(25000000);
  pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
  Serial.println("Servo calibration");
  Serial.println("Use this to calibrate your servo to find the range of movement required");
  Serial.println("The servo should start close to the centre of the range");
  Serial.println("Type a value followed by a + to move in one direction or a valu followed by a - to move in the other direction");
  Serial.println("For example 100+ to 200-");
  Serial.println("To move to a specific location use strings like 900x or 1800x for new servo position");
  Serial.println("Move the servo to find the required range for whatever you're operating.");
  Serial.println("Servos min and max can vary, try the 1000 - 2000 range to start with.");
  Serial.println("WARNING: Exceeding the max range could damage the servo.");
  Serial.println();
  pwm.writeMicroseconds(servonum, pos);
  Serial.println("Centre point:");
  Serial.println(pos);
  delay(10);
}

void loop()
{
  while (Serial.available())
  {
    char c = Serial.read(); //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2); //slow looping to allow buffer to fill with next character
  }
  if (readString.length() > 0)
  {
    if (readString.indexOf('x') > 0)
    {
      pos = readString.toInt();
    }
    if (readString.indexOf('+') > 0)
    {
      pos = pos + readString.toInt();
    }
    if (readString.indexOf('-') > 0)
    {
      pos = pos - readString.toInt();
    }
    pwm.writeMicroseconds(servonum, pos);
    Serial.println(pos);
    readString = ""; //empty for next input
  }
}

Is this the code you are having problems with?

What version IDE are you using?
What OS?

I used 1.6.19, Win 10.

Tom... :grinning: :+1: :coffee: :australia:
PS, I did "wash" the code through a text editor before copying it to the IDE.

That's a video - what code did you copy?

In the Comments, there's a link to

in there is two sketches:

  1. Calibration.ino;
  2. Servos.ino.

Each one is a complete sketch in its own right - with its own pair of setup() and loop() functions.

Did you try to load the two sketches into one ... ?

Hi Tom
Yes thats the code.
the version i am using is IDE 2.2.1 on windows 10, could this be the problem as the guy who wrote the program did it way back.

the controller it working with is a ELEGOO UNO R3
I also have an arduino.cc/uno-r3 board which i have not used.

Im ok with computers but when it comes to programming i'm in the stone age..lol

Thanks for your help

I only copied the calibrtion sketch

Open that folder. Tell us what files are present; there should only be one file with the .ino extension.

Hi,

2.2.1 should be able to compile it, its getting most of its bugs out and should be okay.
I'll try it later today.

Tom... :grinning: :+1: :coffee: :australia:

Good afternoon all, well after 5 days working on the calibration code. today i finally cracked it and found out where i was going wrong.
Now hopefully it will all come together. I tried deleting the coding when you open up the soft wear (the code you use for clearing the board memory) and then pasting the new code to a blank sketch. and it worked .

I would like to thank you all for your help

Many thanks

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