42工业2相4线步进电机,想用一个电位器来随动控制电机转动:电位器正转一点,电机就正转一点,电位器反转一点,电机就反转一点,想了半天写了这个,结果电机不转,知识有限,不知道要怎么实现,代码:
int dir = 8;//步进电机方向端口
int pul = 9;//步进电机速度端口
int val;//电位器变量
int newval;
void setup() {
pinMode(dir, OUTPUT);//步进电机端口输出模式
pinMode(pul, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = analogRead(A0);//定义电位器中线接口A0并赋值给变量
newval=val;
if (newval > val) {
digitalWrite(dir, LOW);//步进电机正转
digitalWrite(pul, HIGH);
delayMicroseconds(500);
digitalWrite(pul, LOW);
delayMicroseconds(500);
}
if (newval < val) {
digitalWrite(dir, HIGH);//步进电机反转
digitalWrite(pul, HIGH);
delayMicroseconds(500);
digitalWrite(pul, LOW);
delayMicroseconds(500);
}
}
|