Go Down

Topic: Problem adding library (Read 699 times) previous topic - next topic

LaloHao

I'm having trouble adding libraries, it doesn't get recognized by the compiler when i add it like so:
IMG attached
Code: (sfdk.h) [Select]
#ifndef sfdk_h
#define sfdk_h
#include "Arduino.h"

#endif

Coding Badly

Quote
...doesn't get recognized...


Reading glasses.  Your compiler needs a pair.  Works for me every time.

LaloHao


Quote
...doesn't get recognized...


Reading glasses.  Your compiler needs a pair.  Works for me every time.


How so?

Coding Badly


The point is that "doesn't get recognized" tells us nothing about the actual problem.

Are you saying you are getting a compilation error?  That the program builds but clearly is not including the code you expect to be included?  That syntax highlighting is not working?  That the compiler is whining about its myopia?

LaloHao

#4
Mar 09, 2013, 02:31 am Last Edit: Mar 09, 2013, 02:35 am by LaloHao Reason: 1
The program doesn't include the libraries i created, i had tested it before on a DC motor i had to simplify some stuff, adding functions like motor.FWD(); but for some reason when i wanted to try again today it just doesn't do anything. It compiles with no errors

EDIT: sorry
Quote
rev1:11: error: 'Motor' does not name a type
rev1:12: error: 'Motor' does not name a type
rev1.ino: In function 'void loop()':
rev1:20: error: 'motorIzquierdo' was not declared in this scope
rev1:21: error: 'motorDerecho' was not declared in this scope


Code: [Select]
#include <motor.h>
Motor motorIzquierdo(0,1);
Motor motorDerecho(2,3);

void setup()
{
}

void loop()
{
  motorIzquierdo.Adelante();
  motorDerecho.Adelante();
  delay(3000);
  motorIzquierdo.Atras();
  motorDerecho.Atras();
  delay(3000);
  motorIzquierdo.Parar();
  motorDerecho.Parar();
  delay(3000);
}

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

LaloHao


Link to the library?


Code: (motor.h) [Select]
#ifndef motor_h
#define motor_h
#include "Arduino.h"

class Motor
{
  public:
    Motor(int pin1, int pin2);
void Adelante();
void Atras();
void Parar();
int Estado();
  private:
    int _pin1,_pin2,_flag;
};
#endif


Code: (motor.cpp) [Select]
#include "Arduino.h"
#include "Motor.h"

int _flag;

Motor::Motor(int _pin1, int _pin2)
{
pinMode(_pin1, OUTPUT);
pinMode(_pin2, OUTPUT);
Motor::Parar();
}

int Motor::Estado()
{
return _flag;
}

void Motor::Parar()
{
digitalWrite(_pin1, LOW);
digitalWrite(_pin2, LOW);
_flag = 0;
}

void Motor::Adelante()
{
  digitalWrite(_pin1, HIGH);
  digitalWrite(_pin2, LOW);
  _flag = 1;
}

void Motor::Atras()
{
  digitalWrite(_pin1, LOW);
  digitalWrite(_pin2, HIGH);
  _flag = 2;
}

michinyon

When you add a library,   you need to close and re-start the IDE.

PaulS

It's also a really good idea to make the file names EXACTLY match the class name. The files should be Motor.cpp and Motor,h, not motor.cpp and motor.h.

Quote
When you add a library,   you need to close and re-start the IDE.

That depends on where you add the library. Add it to the libraries folder, and yes the IDE needs to be restarted. Add it to the sketch folder, using the create tab option, and no, it does not need to be restarted.

LaloHao


It's also a really good idea to make the file names EXACTLY match the class name. The files should be Motor.cpp and Motor,h, not motor.cpp and motor.h.

Typo, they are actually named Motor.cpp and Motor.h and in ~/Documents/Arduino/Libraries folder


Quote
When you add a library,   you need to close and re-start the IDE.

That depends on where you add the library. Add it to the libraries folder, and yes the IDE needs to be restarted. Add it to the sketch folder, using the create tab option, and no, it does not need to be restarted.

I did restart every time i tried to add it. And for some reason i followed your advise of adding it to the sketch and it turns out it says i'm declaring classes twice which means it actually added the library after i added the file to the sketch, i just deleted the sketch and it seems to work again. Wierd?
Well, at least it works now, thanks  everyone!

Go Up