写了个电位器控制步进电机位移的程序 但一直抖动 加了防抖还是抖动 增加防抖值也不行 不知道怎么解决 还有这个没法同时控制两个电机 如果想要同时控制需要怎么写呢 用多线程?还是定时器啥的。
int X=0;
void setup() { // put your setup code here, to run once:
pinMode(4,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);}
void loop() { // put your main code here, to run repeatedly:
delayMicroseconds(100) ;
digitalWrite(4,1);
delayMicroseconds(100) ;
X= analogRead(A0);
int step=0;
while(1){
int a = analogRead(A0);
step=a-X;
X=a;if (step<3&step>-3) continue;//防抖效果不明显,增到10也抖动,增多了缓慢旋转电位器就不动
digitalWrite(7,(step>0?1:0));
delayMicroseconds(6) ;
for(int l=0;l<(step>0?2*step:2*(-1*step));l++) {
digitalWrite(8,0);
delayMicroseconds(600);
digitalWrite(8,1);
delayMicroseconds(800);
}
}
}
int X=0;
void setup() { // put your setup code here, to run once:
pinMode(4,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);}
void loop() { // put your main code here, to run repeatedly:
delayMicroseconds(100) ;
digitalWrite(4,1);
delayMicroseconds(100) ;
X= analogRead(A0);
int step=0;
while(1){
int a = analogRead(A0);
step=a-X;
X=a;if (step<3&step>-3) continue;//防抖效果不明显,增到10也抖动,增多了缓慢旋转电位器就不动
digitalWrite(7,(step>0?1:0));
delayMicroseconds(6) ;
for(int l=0;l<(step>0?2*step:2*(-1*step));l++) {
digitalWrite(8,0);
delayMicroseconds(600);
digitalWrite(8,1);
delayMicroseconds(800);
}
}
}