|
这段时间研究步进电机,买了42步的电机和L298N的驱动板,利用别人的代码改写了步进电机的代码,要实现的功能是,通过判断变量的值来实现步进电机的正转或反转N圈,写完第一个判断后,程序可以编译,但写完第二的判断后就报错,那位高手帮我看看代码由什么问题,或有更好编写请不吝啬赐教!!
int Pin0 = 8;int Pin1 = 9;int Pin2 = 10;int Pin3 = 11;int val;
int step = 0;//step=脚步
int stepperSpeed = 1;//电机转速,1ms一步// stepperSpeed(步进速度)
void setup(){ pinMode(Pin0, OUTPUT); pinMode(Pin1, OUTPUT); pinMode(Pin2, OUTPUT); pinMode(Pin3, OUTPUT);}
void loop()
{
switch(step)
{
case 0: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH);
break; case 1: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH);
break; case 2: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW);
break; case 3: digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW);
break; case 4: digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW);
break; case 5: digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW);
break; case 6: digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW);
break; case 7: digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH);
break; default: digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW);
break;
}
if (val='z') //Z/F为正反转判断
{
for (int i=0;i>=300;i++)
{
if(step>7)
{step=0;step++;}
}
if (val='f')
{
for (int i=300;i<=0;i--)
{
if(step<0)
{setp=7;step--;}//问题出现在这行
}
}
}
|
|
|