I am trying to create a library Mitch (following the tutorial??).
Here is Mitch.h
/*
Mitch.h - Library for stepper motor function.
Created by John Mitchell 23 Mar 2023_1205.
Released into the mitch domain.
*/
#ifndef Mitch_h
#define Mitch_h
#include "Arduino.h"
class Mitch
{
public:
Mitch(String motorType);
void setAttributes(String motorType);
// int no_of_steps;
private:
String _motorType;
};
#endif
Here is Mitch.cpp:
`
/*
Mitch.cpp - Library for mitch stepper motors.
Created by John Mitchell 23 Mar 2023_1228.
Released into the mitch domain.
*/
#include "Arduino.h"
#include "Mitch.h"
Mitch::Mitch()
{
_motorType = motorType;
}
void Mitch::setAttributes(String _motorType) {
int no_of_steps = 0;
if (_motorType == "2T35")
{
// step = 3.75 deg
no_of_steps = 360 / 3.75;
} else if (_motorType == "2T15") {
// step = 18 deg
no_of_steps = 360 / 18;
} else if (_motorType == "WA42") {
// step = 0.9 deg
no_of_steps = 360 / 0.9;
}
return no_of_steps;
}
And here is the Sketch
#include <Mitch.h>
String motor = "WA42";
Mitch mitch;
void setup() {
// put your setup code here, to run once:
int nSteps;
nSteps = mitch.setAttributes(motor);
}
void loop() {
// put your main code here, to run repeatedly:
}
I get this error
Compilation error: no matching function for call to 'Mitch::Mitch()'
I suggest before you make a library - make a working sketch with the class in your sketch. Show that sketch too.
a) The error is obvious, you try to create your instance not in the form you have declared the constructor.
b) Why do you need a setter? Is there any need to change motorType during runtime? why call it it set when you return a calculated number?!?
c) Btw: there are for sure better variants to hand over a motor type than to use a String Object.
edit:
just a short POC:
class Mitch {
public:
enum Motortype {M2T35, M2T15, MWA42 };
protected:
Motortype motortype;
int steps = 0;
public:
Mitch(Motortype motortype) : motortype{motortype} {
steps = calcNoOfSteps(motortype);
}
// an independent mapping of a motortype to steps
// if re-usage in usersketch would really be necessary
int calcNoOfSteps(Motortype motortype) {
int no_of_steps = 0;
switch (motortype) {
case M2T35 :
no_of_steps = 360 / 3.75;
break;
case M2T15 :
// step = 18 deg
no_of_steps = 360 / 18;
break;
case MWA42 :
// step = 0.9 deg
no_of_steps = 360 / 0.9;
break;
}
return no_of_steps;
}
// just return the internal
int getNoOfSteps() {
return steps;
}
};
Mitch mitchA(Mitch::M2T35);
Mitch mitchB(Mitch::M2T15);
Mitch mitchC(Mitch::MWA42);
void setup() {
Serial.begin(115200);
Serial.println(mitchA.getNoOfSteps());
Serial.println(mitchB.getNoOfSteps());
Serial.println(mitchC.getNoOfSteps());
}
void loop() {
// put your main code here, to run repeatedly:
}
gives:
96
20
400
If you explain a little bit more about what you want to achieve, and what you want the user let to configure at which point, we might come up with a better solution. This could be a base class with the main functionality and inherited children for the motortypes. Or something completely different depending of what you are aiming for...
My guessing is it is a dry exercise to learn how it works to write a library that contains a class.
Of course it is very very common to have objects defined as classes in a library.
If my assumption is true (which might be not) the TO is writing a library which has a class as the very first exercise does two things at once which make it more complicated to understand what is what.
And - like it or hate it - requires a lot of explanation to make it easy to understand.
Without the explanations it is very likely that a newcomer will work this way:
the class that I use as my blue-print has a name in first letter Capital followed by the same charactersequence lowercase in this pattern
This means it is a optical modification without understanding why.
You should add explanations to make it easy to understand the underlying pattern or as an alternative just paste a link to a good tutorial that does explain all details step by step.
best regards Stefan
P.S. After quogling for it Will do exactly this in my own thread
to all replies:
I followed the example from Learn "Writing a Library for Arduino". I attempted to transpose exactly my specifics using the the example Morse dash, dot. @StefanL38 captures my predicament. I am a new comer to Arduino although have some reasonable coding experience. I have several scripts all understanding how to control stepper motors taking various examples in the Web and this forum, and wanted to have a library instead of tweaking multiple times.
So I will look at the example in circuit basic.