Cannot Understan Why This Code doesn't Work : "Error Compiling" !

Hello everybody. I have just written this program. Don't mind the annotation, they're in french and I don't think they have much importance :). So this program controls 6 servos. To make things a bit more organized, I have created a .h file (header) where I put the prototype of the functions and a .cpp in which I define them. However, when I try to compile it, I just get a message error which says :"Error compiling". Here are the files :

the main one :

#include "Servo_command.h"
#include <Servo.h>
#include "Arduino.h"

Servo myservo1, myservo2, myservo3, myservo4, myservo5, myservo6;

                
 
int pos = 0;   
int pos_max = 90;  
int pos_min = 56;                         // position la plus basse du servo
int ND = 4;                               // Nombre de notes différentes
int SRT = (pos_max-pos_min)*10*2;         // temps que met le servo pour monter puis redescendre
int k = 0; 
int l = 0;


void setup()
{ 
  myservo1.attach(7);  
  myservo2.attach(8);
  myservo3.attach(9);
  myservo4.attach(10);
  myservo5.attach(11);
  myservo6.attach(12);  
}
 
  
 
void loop() 
{  
  toutMonter();
  toutDescendre();
  k = k+1;                    
  l = (k % ND) +1;                             // A chaque fois que le servo atteindra cette position, une valeur "l" sera stocké, allant de 1 à ND : il représente à chaque itération les notes les unes à la suite des autres.
  if ( l == 1 || l == 2 ) {delay(1000-SRT);}   // Si la valeur de "l" est de 1 ou 2, le servo reste en bas pendant 1000-SRT(=le temps que met le servo pour son aller-retout) ms.
  if ( l == 3 || l == 4 ) {delay(2000-SRT);}   // Si la valeur de "l" est de 3 ou 4, le servo reste en bas pendant 2000-SRT(=le temps que met le servo pour son aller-retout) ms.
}

The .cpp :

#include "Servo_command.h"
#include <Servo.h>
#include "Arduino.h"

Servo myservo1, myservo2, myservo3, myservo4, myservo5, myservo6;

int pos = 0;   
int pos_max = 90;  
int pos_min = 56;                          // position la plus basse du servo
int ND = 4;                                // Nombre de notes différentes
int SRT = (pos_max-pos_min)*10*2;          // temps que met le servo pour monter puis redescendre
int k = 0; 
int l = 0;


void monter1(Servo myservo1)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    delay(10);
  }
}

void monter2(Servo myservo1, Servo myservo2)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    delay(10);
  }
}

void monter3(Servo myservo1, Servo myservo2, Servo myservo3)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    delay(10);
  }
}

void monter4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    delay(10);
  }
}

void monter5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    delay(10);
  }
}

void toutMonter()
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    myservo6.write(pos);
    delay(10);
  }
}

void descendre1(Servo myservo1)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    delay(10);
  }
}

void descendre2(Servo myservo1, Servo myservo2)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    delay(10);
  }
}

void descendre3(Servo myservo1, Servo myservo2, Servo myservo3)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    delay(10);
  }
}

void descendre4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    delay(10);
  }
}

void descendre5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    delay(10);
  }
}

void toutDescendre()
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    myservo6.write(pos);
    delay(10);
  }
}

And finally the .h :

#ifndef Servo_command.h 
#define Servo_command.h 
#include "Arduino.h"

#include <Servo.h>


void monter1(Servo myservo1);
void monter2(Servo myservo1, Servo myservo2);
void monter3(Servo myservo1, Servo myservo2, Servo myservo3);
void monter4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4);
void monter5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5);
void toutMonter();
void descendre1(Servo myservo1);
void descendre2(Servo myservo1, Servo myservo2);
void descendre3(Servo myservo1, Servo myservo2, Servo myservo3);
void descendre4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4);
void descendre5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5);
void toutDescendre();

#endif

Any help is welcomed :slight_smile: !

I think it didn't work because the port is incorrect, or not chosen. You need to look at the top bar>>tools>>port. Make sure your arduino is set for the port. As well, make sure you actually put the correct model of the arduino. The error compiling basically means that the software can't apply the code to the arduino, if you don't have any error messages, this will work!

jeff_ekaka:
I just get a message error which says :"Error compiling". Here are the files :

There is likely to be some additional information in the output window. Try clicking the Copy Error Message button and pasting it into a text editor.

If this is a compiling problem the port won't matter. The port is only relevant when a program compiles successfully and tries to upload.

...R

Maybe a bit lacking in "extern"?

Here, I have put the error message in a .txt file in the attachment.

Error Message.txt (26.8 KB)

There are lots of messages about multiple definitions and I think that is because you have defined servo objects in all three files.

...R

Well finally I figured out what was the problem : it seems like I had to remove some variables from the main file and the .cpp one. As Robin2 said, I shouldn't have defined servo objects in my .cpp file. Moreover, I had to put arguments in my toutMonter() function. So it went from :

toutMonter();

to :

toutMonter(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5, Servo myservo6);

(And of course all the changements it implied).

Yeah so here is the new code which works :
the main .ino file :

#include "Servo_command.h"
#include <Servo.h>


Servo myservo1, myservo2, myservo3, myservo4, myservo5, myservo6;

                
int ND = 4;                               // Nombre de notes différentes
// int SRT = (pos_max-pos_min)*10*2;         // temps que met le servo pour monter puis redescendre
int k = 0; 
int l = 0;


void setup()
{ 
  myservo1.attach(7);  
  myservo2.attach(8);
  myservo3.attach(9);
  myservo4.attach(10);
  myservo5.attach(11);
  myservo6.attach(12);  
}
 
  
 
void loop() 
{  
  toutMonter(myservo1, myservo2, myservo3, myservo4, myservo5, myservo6);
  toutDescendre(myservo1, myservo2, myservo3, myservo4, myservo5, myservo6);
  k = k+1;                    
  l = (k % ND) +1;                             // A chaque fois que le servo atteindra cette position, une valeur "l" sera stocké, allant de 1 à ND : il représente à chaque itération les notes les unes à la suite des autres.
  if ( l == 1 || l == 2 ) {delay(1000-680);}   // Si la valeur de "l" est de 1 ou 2, le servo reste en bas pendant 1000-SRT(=le temps que met le servo pour son aller-retout) ms.
  if ( l == 3 || l == 4 ) {delay(2000-680);}   // Si la valeur de "l" est de 3 ou 4, le servo reste en bas pendant 2000-SRT(=le temps que met le servo pour son aller-retout) ms.
}

the .cpp file :

#include "Servo_command.h"
#include "Arduino.h"

#include <Servo.h>

int pos = 0;   
int pos_max = 90;  
int pos_min = 56;                          // position la plus basse du servo



void monter1(Servo myservo1)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    delay(10);
  }
}

void monter2(Servo myservo1, Servo myservo2)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    delay(10);
  }
}

void monter3(Servo myservo1, Servo myservo2, Servo myservo3)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    delay(10);
  }
}

void monter4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    delay(10);
  }
}

void monter5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    delay(10);
  }
}

void toutMonter(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5, Servo myservo6)
{
  for (pos = pos_min; pos <= pos_max; pos++)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    myservo6.write(pos);
    delay(10);
  }
}

void descendre1(Servo myservo1)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    delay(10);
  }
}

void descendre2(Servo myservo1, Servo myservo2)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    delay(10);
  }
}

void descendre3(Servo myservo1, Servo myservo2, Servo myservo3)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    delay(10);
  }
}

void descendre4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    delay(10);
  }
}

void descendre5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    delay(10);
  }
}

void toutDescendre(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5, Servo myservo6)
{
  for (pos = pos_max; pos >= pos_min; pos--)
  {
    myservo1.write(pos);
    myservo2.write(pos);
    myservo3.write(pos);
    myservo4.write(pos);
    myservo5.write(pos);
    myservo6.write(pos);
    delay(10);
  }
}

and the header file .h :

#ifndef Servo_command.h 
#define Servo_command.h 
#include "Arduino.h"

#include <Servo.h>


void monter1(Servo myservo1);
void monter2(Servo myservo1, Servo myservo2);
void monter3(Servo myservo1, Servo myservo2, Servo myservo3);
void monter4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4);
void monter5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5);
void toutMonter(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5, Servo myservo6);
void descendre1(Servo myservo1);
void descendre2(Servo myservo1, Servo myservo2);
void descendre3(Servo myservo1, Servo myservo2, Servo myservo3);
void descendre4(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4);
void descendre5(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5);
void toutDescendre(Servo myservo1, Servo myservo2, Servo myservo3, Servo myservo4, Servo myservo5, Servo myservo6);

#endif

Thank you everyone !