How do I fix my stepper motor library that I made for Arduino?

I have an Arduino Mega and I am using an L298N and my goal is to make a successful Arduino library to make music with the stepper motor. I am aware that this approach has already been made before, but I am trying to do it myself. I can’t use the Moppy library and quite frankly, other’s code is quite complex. So, what’s my problem? My problem is I get this error when I use the library that I have made (as a test):

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega" "-IC:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary" "C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp" -o "C:\Users\austin\AppData\Local\Temp\arduino_build_519425\libraries\stepperTestLibrary\stepperTestLibrary.cpp.o"
C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.cpp:13:1: error: prototype for 'stepperTest::stepperTest(int, int, int, int)' does not match any in class 'stepperTest'
 stepperTest::stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4){
 ^~~~~~~~~~~
In file included from C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.cpp:11:0:
C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.h:15:7: error: candidates are: constexpr stepperTest::stepperTest(stepperTest&&)
 class stepperTest{
       ^~~~~~~~~~~
C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.h:15:7: error:                 constexpr stepperTest::stepperTest(const stepperTest&)
C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.h:19:1: error:                 stepperTest::stepperTest(uint8_t, uint8_t, uint8_t, uint8_t)
 stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4);
 ^~~~~~~~~~~
C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.cpp:29:6: error: prototype for 'void stepperTest::runStepper(int, int)' does not match any in class 'stepperTest'
 void stepperTest::runStepper(int frequency, int duration){
      ^~~~~~~~~~~
In file included from C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.cpp:11:0:
C:UsersaustinDocumentsArduinolibrariesstepperTestLibrarystepperTestLibrary.h:21:6: error: candidate is: void stepperTest::runStepper(uint8_t, uint8_t)
 void runStepper(uint8_t frequency, uint8_t duration);
      ^~~~~~~~~~

and this as well:

exit status 1
Error compiling for board Arduino Mega or Mega 2560.

I expected the library to run smoothly because I was following some other libraries for stepper motors, but I got those errors. I have tried revising the code several times, but I get similar problems, so I am not sure how to fix my own problems. I have even tried copying and pasting some code to just make the library work. Nothing. I am working with the Arduino Stepper, Accelstepper, and the bipolar stepper library someone else made, no results. https://create.arduino.cc/projecthub/ambhatt/bipolar-stepper-motor-library-b9d5e0

Here is the .h file:

/*
This library is designed for testing purposing only. CC Public Domain
Austin Harris
3/4/2021

This library is designed to run a stepper motor at a certain frequency to
make musical notes and for a certain duration
*/

#ifndef stepperTestLibrary_h
#define stepperTestLibrary_h

#include "Arduino.h"

class stepperTest{

public:

stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4);

void runStepper(int frequency, int duration);

private: 

  int direction;

  int SMPin1, intSMPin2, intSMPin3, intSMPin4;
  
};

#endif

Here is the .cpp file:

/*
This library is designed for testing purposing only. CC Public Domain
Austin Harris
3/4/2021

This library is designed to run a stepper motor at a certain frequency to
make musical notes and for a cetain duration
*/

#include "Arduino.h"
#include "stepperTestLibrary.h"

stepperTest::stepperTest(int SMPin1, int SMPin2, int SMPin3, int SMPin4){

pinMode(SMPin1, OUTPUT);
pinMode(SMPin2, OUTPUT);
pinMode(SMPin3, OUTPUT);
pinMode(SMPin4, OUTPUT);

_SMPin1 = SMPin1;
_SMPin2 = SMPin2;
_SMPin3 = SMPin3;
_SMPin4 = SMPin4;

int direction = 0;      // motor direction

}

void stepperTest::runStepper(int frequency, int duration){

int rpmCalculation = abs(1000.0 / frequency);

// determine direction based on whether steps_to_mode is + or -:
  if (frequency > 0) { direction = 1; }
  if (frequency < 0) { direction = 0; }

    digitalWrite(SMPin1, HIGH);
    digitalWrite(SMPin2, LOW);
    digitalWrite(SMPin3, HIGH);
    digitalWrite(SMPin4, LOW);

    delay(1);

    digitalWrite(SMPin1, LOW);
    digitalWrite(SMPin2, HIGH);
    digitalWrite(SMPin3, HIGH);
    digitalWrite(SMPin4, LOW);

    delay(1);

    digitalWrite(SMPin1, LOW);
    digitalWrite(SMPin2, HIGH);
    digitalWrite(SMPin3, LOW);
    digitalWrite(SMPin4, HIGH);

    delay(1);

    digitalWrite(SMPin1, HIGH);
    digitalWrite(SMPin2, LOW);
    digitalWrite(SMPin3, LOW);
    digitalWrite(SMPin4, HIGH);

    delay(duration);

}

This is the keywords.txt:

stepperTest KEYWORD1
runStepper  KEYWORD2

And this is the .ino example:

#include <stepperTestLibrary.h>

stepperTest myStepper(22, 23, 24, 25);


void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

myStepper.runStepper(440, 500);

}

Here is the wiring:

This is the wiring image

Here is the link for the datasheet of the stepper:

This is the datasheet link

-Thanks,

Austin

Answer

Reading something about classes in C++ would be a good start. And maybe how variable names works. And how shadowing variables works too. It’s quite a difference between SMPin2, _SMPin2 and third variant is intSMPin2 (and others are messed up too)

Anyway, if you want to use the same parameter name as for member in constructor, you have to use constructors initializer list.

#ifndef stepperTestLibrary_h
#define stepperTestLibrary_h

#include "Arduino.h"

class stepperTest {
public:
  stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4);

  void runStepper(int frequency, int duration);

private: 
  // just give some space to it:
  uint8_t SMPin1;
  uint8_t SMPin2;
  uint8_t SMPin3;
  uint8_t SMPin4;

  int8_t direction;

  /// or you'll mess it up like this:
  ///  int SMPin1, --->>>>>  intSMPin2, intSMPin3, intSMPin4  <<<<<---;
};

#endif

And the main part:

#include "Arduino.h"
#include "stepperTestLibrary.h"

stepperTest::stepperTest(uint8_t SMPin1, uint8_t SMPin2, uint8_t SMPin3, uint8_t SMPin4)
// constructor's initializer list:
: SMPin1{ SMPin1 }
, SMPin2{ SMPin2 }
, SMPin3{ SMPin3 }
, SMPin4{ SMPin4 }
, direction{ 0 }
{
    pinMode(SMPin1, OUTPUT);
    pinMode(SMPin2, OUTPUT);
    pinMode(SMPin3, OUTPUT);
    pinMode(SMPin4, OUTPUT);
}

void stepperTest::runStepper(int frequency, int duration) {

    // // Never used anywhere:  
    // int rpmCalculation = abs(1000.0 / frequency); 
    // // determine direction based on whether steps_to_mode is + or -:
    // if (frequency > 0) { direction = 1; }
    // if (frequency < 0) { direction = 0; }

    digitalWrite(SMPin1, HIGH);
    digitalWrite(SMPin2, LOW);
    digitalWrite(SMPin3, HIGH);
    digitalWrite(SMPin4, LOW);

    delay(1);

    digitalWrite(SMPin1, LOW);
    digitalWrite(SMPin2, HIGH);
    digitalWrite(SMPin3, HIGH);
    digitalWrite(SMPin4, LOW);

    delay(1);

    digitalWrite(SMPin1, LOW);
    digitalWrite(SMPin2, HIGH);
    digitalWrite(SMPin3, LOW);
    digitalWrite(SMPin4, HIGH);

    delay(1);

    digitalWrite(SMPin1, HIGH);
    digitalWrite(SMPin2, LOW);
    digitalWrite(SMPin3, LOW);
    digitalWrite(SMPin4, HIGH);

    delay(duration); // what? why???
}

Leave a Reply

Your email address will not be published. Required fields are marked *