[英]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.我有一个 Arduino Mega,我正在使用 L298N,我的目标是制作一个成功的 Arduino 库,用步进电机制作音乐。 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.
我不能使用 Moppy 库,坦率地说,其他的代码非常复杂。 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:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.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:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:11:0:
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:15:7: error: candidates are: constexpr stepperTest::stepperTest(stepperTest&&)
class stepperTest{
^~~~~~~~~~~
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.h:15:7: error: constexpr stepperTest::stepperTest(const stepperTest&)
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.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:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.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:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.cpp:11:0:
C:\Users\austin\Documents\Arduino\libraries\stepperTestLibrary\stepperTestLibrary.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.
我正在使用 Arduino 步进器、Accelstepper 和其他人制作的双极步进器库,但没有结果。 https://create.arduino.cc/projecthub/ambhatt/bipolar-stepper-motor-library-b9d5e0
https://create.arduino.cc/projecthub/ambhatt/bipolar-stepper-motor-library-b9d5e0
Here is the.h file:这是.h文件:
/*
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:这是.cpp文件:
/*
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:这是keywords.txt:
stepperTest KEYWORD1
runStepper KEYWORD2
And this is the.ino example:这是.ino 的例子:
#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:这是接线:
Here is the link for the datasheet of the stepper:这是步进器数据表的链接:
This is the datasheet link这是数据表链接
-Thanks, -谢谢,
Austin奥斯汀
Reading something about classes in C++ would be a good start.在 C++ 中阅读有关类的内容将是一个好的开始。 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)
SMPin2、_SMPin2 和第三种变体是 intSMPin2 之间有很大的不同(其他变体也搞砸了)
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???
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.