Hi All,
I thought that I would have a go at developing my own library, so that a large section of code is hidden away in a library.
But I am getting an error messages when I try and compile it:-
motor.h:16:24: warning: ISO C++ forbids declaration of 'init' with no type [-fpermissive]
init(int, int, int);
motor.cpp:1:0:
H:\Arduino\libraries\Motor\motor.h:16:24: warning: ISO C++ forbids declaration of 'init' with no type [-fpermissive]
init(int, int, int);
motor.cpp:5:1: error: expected initializer before 'void'
void motor::init(int pwm, int in1, int in2) {
motor.cpp:29:23: error: no 'void motor::forward()' member function declared in class 'motor'
void motor::forward() {
^
motor.cpp:33:20: error: no 'void motor::back()' member function declared in class 'motor'
void motor::back() {
motor.cpp:37:1: error: expected declaration before '}' token
};
^
Here is the code that I have in the motor.h file
#ifndef MOTOR_H
#define MOTOR_H
#include <Arduino.h>
struct motor{
int pwmPin;
int inPin1;
int inPin2;
public:
motor( int, int, int);
void setDirection(boolean, boolean);
void stop();
void speed(int);
private:
init(int, int, int);
};
#endif
And here is the code for the motor.cpp file
#include "motor.h"
motor:: motor(int pwmPin, int inPin1, int inPin2)
void motor::init(int pwm, int in1, int in2) {
pwmPin = pwm;
inPin1 = in1;
inPin2 = in2;
pinMode(inPin1, OUTPUT);
pinMode(inPin2, OUTPUT);
stop();
}
void motor::setDirection(boolean in1, boolean in2) {
digitalWrite(inPin1, in1);
digitalWrite(inPin2, in2);
}
void motor::stop() {
digitalWrite(inPin1, LOW);
digitalWrite(inPin1, LOW);
speed(0);
}
void motor::speed(int aSpeed) {
if (aSpeed >= 0) {
analogWrite(pwmPin, aSpeed);
}
}
// The following functions are not used here
// but could be useful in future
void motor::forward() {
digitalWrite(inPin1, HIGH);
digitalWrite(inPin1, LOW);
}
void motor::back() {
digitalWrite(inPin1, LOW);
digitalWrite(inPin1, HIGH);
}
};
I have looked at several different posts with the same error, but I can't seem to see why I am getting this error. I know it is the syntax of the motor definition, but more than that, I'm not 100% sure.
Never written a library before......