我收到此错误:
在函数'void loop()'中:headers_stepper_test:12:错误:预期 '。'之前的不合格ID令牌期望在'。'之前的非限定标识。令牌
在此代码中:
#include "StepperMotor.h"
void setup() {
// put your setup code here, to run once:
StepperMotor(8,9);
}
void loop() {
// put your main code here, to run repeatedly:
void StepperMotor.moveDegrees(-180);
delay(1000);
}
cpp库文件:
#include "Arduino.h"
StepperMotor::StepperMotor(int pin1, int pin2)
{
dirPin=pin1;
pinMode(dirPin,OUTPUT);
stepperPin=pin2;
pinMode(stepperPin,OUTPUT);
}
void StepperMotor::stepDegrees(bool dir, int steps);
{
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++){
digitalWrite(stepperPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepperPin, LOW);
delayMicroseconds(800);
}
void StepperMotor::moveDegrees(int degreeNumber);
{
if (degrees > 0){
userAbs = (degreeNumber);
stepNumber = (userAbs * 200/360);
step(true,stepNumber);
}
if (degrees < 0){
userAbs = (-1*degreesNumber);
stepNumber = (userAbs * 200/360);
step(false,stepNumber);
}
}
.h头文件:
#ifndef StepperMotor_h
#define StepperMotor_h
#include "Arduino.h"
class StepperMotor
{
public:
StepperMotor(int pin1, int pin2);
void moveDegrees(int degreeNumber);
private:
void stepDegrees(bool dir, int steps);
int dirPin;
int stepPin;
float userAbs;
float stepNumber;
};
#endif
当我尝试实现自己的库文件时。我不确定在这种情况下是否需要“空白”,但无论如何都会出现同样的错误。造成这种情况的原因是什么?
答案 0 :(得分:0)
让我们回顾一下你的代码:
void loop() {
// put your main code here, to run repeatedly:
void StepperMotor.moveDegrees(-180);
delay(1000);
}
首先要做的是:不要将void
放入moveDegress()
的调用中。
第二:
方法moveDegrees
不是静态的,因此,您需要类StepperMotor
的实例才能调用它:
// note: you can initialize the variable here
// but I'll do it in setup
StepperMotor stepperMotorInstance; // your variable
void setup() {
stepperMotorInstance = StepperMotor(8,9); // option 2: initialize variable in setup
}
void loop() {
// put your main code here, to run repeatedly:
stepperMotorInstance.moveDegrees(-180);
delay(1000);
}
希望有所帮助