I was trying to write a library of my own (nothing big just for a small project of mine) . However when I try to compile my sketch with the library included I only get the error:
expected initializer before 'stepper'
This error doesn't say much about what is wrong and from what I can tell this error mostly comes when someone has forgotten a semicolon or a swirly bracket. But I've looked through my code and I can't find what's causing this. I haven't written a library before so I just followed the arduino tutorial on the topic. Here is my code:
Hedder file
#ifndef MusicStepper_h
#define MusicStepper_h
#include "Arduino.h"
class MusicStepper {
public:
MusicStepper(int stepPin, int dirPin);
void setFrequency(float freq);
void play();
private:
int _stepPin;
int _dirPin;
long _lastPlay;
float _freq;
float _period;
bool _step;
}
#endif
MusicStepper::MusicStepper(int stepPin, int dirPin) {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
You cannot be sure that the hardware is properly initialized before entering the "setup()" method. You should implement a "MusicStepper::begin()" method which initializes the pins and call that method in "setup()".
You cannot be sure that the hardware is properly initialized before entering the "setup()" method. You should implement a "MusicStepper::begin()" method which initializes the pins and call that method in "setup()".
Okay thanks. Didn't know it worked that way. I was just following the tutorial were they used pinMode as well in their constructor.
Mm, the "Hacking" part of the Arduino reference doesn't seem to be part of the reference on git... But indeed, what the Morse Library tutorial shows is bad practice. That's why a lot of libraries come with a .begin() method