我为下面的问题找了一个月的答案,但是我找不到它,所以这里有我的问题: 我为Arduino编写了一个库,以下是我的代码,编译器总是显示
error: 'iniziaScheda1' was not declared in this scope
和其他功能相同的错误,我该怎么办?
CNC.h
#ifndef CNC
#define CNC
#include <Arduino.h>
class CNCa
{
private:
int latchPin1;
int clockPin1;
int dataPin1;
int latchPin2;
int clockPin2;
int dataPin2;
byte scheda[7];
int count;
public:
void iniziaScheda1(int latch,int clock,int data);
void iniziaScheda2 (int latch,int clock,int data);
void motoreZ(int nStep);
void motoreX(int nStep);
void motoreY(int nStep);
void motore4(int nStep);
int velocita;
void registro(int nScheda, byte motore);
};
#endif
CNC.cpp
#include <CNC.h>
#include <Arduino.h>
void CNCa:: iniziaScheda1(int latch,int clock,int data){
latchPin1=latch;
clockPin1=clock;
dataPin1=data;
pinMode(latchPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
scheda= { B01100000, // = 0
B01010000, // = 1
B00011000, // = 2
B00101000, // = 3
B10000100, // = 4
B00000110, // = 5
B00000011, // = 6
B10000001, // = 7
};
}
void CNCa::iniziaScheda2(int latch,int clock,int data){
latchPin2=latch;
clockPin2=clock;
dataPin2=data;
pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
}
//n Scheda indica la scheda che stiamo usando
void CNCa::registro(int nScheda, byte motore){
if(nScheda=1){
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,scheda[motore]);
digitalWrite(latchPin1, HIGH);
}else{(digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, LSBFIRST,scheda[motore]);
digitalWrite(latchPin2, HIGH);
}}
//i motori sono nominati come Z(scheda1 motore1) x(scheda1 motore2) y(scheda2 motore1) 4(scheda2 motore2)
void CNCa:: motoreZ (int nStep){if(nStep>0){ count=0; for(int i=0;i<nStep;i++){delay(velocita);
switch(count){
case 0 : registro(1,3);count++;break;
case 1 : registro(1,2);count++;break;
case 2 : registro(1,1);count++;break;
case 3 : registro(1,0);count=0;break;
} ;
}
}else if(nStep<0){count=0;for(int i=0;i>nStep;--i){delay(velocita);
switch(count){
case 0 : registro(1,0);count++;break;
case 1 : registro(1,1);count++;break;
case 2 : registro(1,2);count++;break;
case 3 : registro(1,3);count=0;break;
} ;
}
}
}
void CNCa:: motoreX(int nStep){if(nStep>0){ count=0; for(int i=0;i<nStep;i++){delay(velocita);
switch(count){
case 0 : registro(1,7);count++;break;
case 1 : registro(1,6);count++;break;
case 2 : registro(1,5);count++;break;
case 3 : registro(1,4);count=0;break;
} ;
}
}else if(nStep<0){count=0;for(int i=0;i>nStep;--i){delay(velocita);
switch(count){
case 0 : registro(1,4);count++;break;
case 1 : registro(1,5);count++;break;
case 2 : registro(1,6);count++;break;
case 3 : registro(1,7);count=0;break;
} ;
} }
}
void CNCa:: motoreY(int nStep){if(nStep>0){ count=0; for(int i=0;i<nStep;i++){delay(velocita);
switch(count){
case 0 : registro(1,3);count++;break;
case 1 : registro(1,2);count++;break;
case 2 : registro(1,1);count++;break;
case 3 : registro(1,0);count=0;break;
} ;
}
}else if(nStep<0){count=0;for(int i=0;i>nStep;--i){delay(velocita);
switch(count){
case 0 : registro(2,0);count++;break;
case 1 : registro(2,1);count++;break;
case 2 : registro(2,2);count++;break;
case 3 : registro(2,3);count=0;break;
} ;
}
}
}
void CNCa:: motore4(int nStep){if(nStep>0){ count=0; for(int i=0;i<nStep;i++){delay(velocita);
switch(count){
case 0 : registro(2,7);count++;break;
case 1 : registro(2,6);count++;break;
case 2 : registro(2,5);count++;break;
case 3 : registro(2,4);count=0;break;
} ;
}
}else if(nStep<0){count=0;for(int i=0;i>nStep;--i){delay(velocita);
switch(count){
case 0 : registro(2,4);count++;break;
case 1 : registro(2,5);count++;break;
case 2 : registro(2,6);count++;break;
case 3 : registro(2,7);count=0;break;
} ;
} }
}
arduino文件
#include <CNC.h>
void setup() {
iniziaScheda1(1,2,3);// put your setup code here, to run once:
velocita=20;
}
void loop() {
motoreZ(50);
delay(1000);
motoreZ(-50);
delay(1000);// put your main code here, to run repeatedly:
}
答案 0 :(得分:0)
您的函数是类成员,因此您应该创建类实例,然后在其上调用成员,即:。
CNCa tmp;
tmp.iniziaScheda1(1,2,3);
要保留类成员变量的值,您的类实例应具有全局范围。