论文总字数:2672字
int Key0 = A0; //按键引脚
int Key1 = A1;
int Key2 = A2;
int Key3 = A3;
int Pin0 = 2; //电机驱动引脚
int Pin1 = 3;
int Pin2 = 4;
int Pin3 = 5;
int _step = 0;
boolean dir = false; //正反转
boolean isMotoRun = false; //上电时,电机不运行
int stepperSpeed = 10; //电机转速,1ms一步
void setup() {
pinMode(Pin0, OUTPUT); //电机引脚置为输出状态
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
pinMode(Key0, INPUT); //按键引脚置为输入状态
pinMode(Key1, INPUT);
pinMode(Key2, INPUT);
pinMode(Key3, INPUT);
digitalWrite(Pin0, LOW); //电机引脚模式低电平,不转动
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
}
void loop() {
if (analogRead(Key0) lt; 200) //检测按键引脚电压,低于200就认为有按键按下
{
delay(10);
if (analogRead(Key0) lt; 200)
{
while (analogRead(Key0) lt; 200);
dir = false; //正转
isMotoRun = true;
}
}
if (analogRead(Key1) lt; 200) //检测按键引脚电压,低于200就认为有按键按下
{
delay(10);
if (analogRead(Key1) lt; 200)
{
while (analogRead(Key1) lt; 200);
dir = true; //反转
isMotoRun = true;
}
}
if (analogRead(Key2) lt; 200) //检测按键引脚电压,低于200就认为有按键按下
{
delay(10);
if (analogRead(Key2) lt; 200)
{
while (analogRead(Key2) lt; 200);
isMotoRun = true;
stepperSpeed--; //加速
if (stepperSpeed lt;= 2)
{
stepperSpeed = 2;
}
}
}
if (analogRead(Key3) lt; 200) //检测按键引脚电压,低于200就认为有按键按下
{
delay(10);
if (analogRead(Key3) lt; 200)
{
while (analogRead(Key3) lt; 200);
isMotoRun = true;
stepperSpeed ; //减速
if (stepperSpeed gt;= 10)
{
stepperSpeed = 10;
}
}
}
switch (_step) {
case 0:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 1:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 2:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 3:
digitalWrite(Pin0, LOW);
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 (isMotoRun == true)
{
if (dir) {
_step ;
} else {
_step--;
}
if (_step gt; 3) {
_step = 0;
}
if (_step lt; 0) {
_step = 3;
}
}
delay(stepperSpeed);
}
剩余内容已隐藏,请支付后下载全文,论文总字数:2672字
该课题毕业论文、开题报告、外文翻译、程序设计、图纸设计等资料可联系客服协助查找;