|
步进电机测试代码 L293D驱动板 控制正转暂停反转时间 /* 作者:tom 时间:2013年04月30日 IDE版本号:1.01 作用: */ /*操作系统环境winxp Arduino IDE Ver 1.01 硬件:2012年的Mango控制器(Arduino兼容) 作者:tom */ //硬件:Mango控制器、自制的传感器扩展板、自制的L293D电机驱动板(光耦隔离)、5伏的步进电机型号28BYJ-48 备注:为了方便观察步进电机是否停下来了,间隙时间变量设置为interval=1300(13秒) 实现功能 步进电机正转13秒 停止12秒 反转13秒 停止12秒 /*执行Stepper.step(0)子程序时,只有第一个和第三个指示灯亮。 */ #include <Stepper.h> // change this to the number of steps on your motor //改变这个数字适应你的步进电机 #define STEPS 100 // create an instance of the stepper class, specifying // the number of steps of the motor and the pins it's // attached to //创建步进电机类 //将Arduino控制器上数字端口8,9,10,11,定义为步进电机的端口 //Stepper stepper(STEPS, 8, 9, 10, 11); //为了适应变化,定义变量stepPin1,stepPin2,stepPin3,stepPin4作为控制步进电机的端口变量 int stepPin1=8; int stepPin2=9; int stepPin3=10; int stepPin4=11; Stepper stepper(STEPS,stepPin1,stepPin2,stepPin3,stepPin4); long previousMillis = 0; // 定义储存上次的时间变量 int intstep=10;//设置步进电机的步数,请自行调整 // 下列的变量是长整型数,因为时间单位是毫秒 //将迅速变成一个很大的数字超过整型数字的存储 long interval = 13000; // 间隙(毫秒) void setup() { stepper.setSpeed(30); Serial.begin(9600); } void loop() { stepper.step(intstep);//步进电机正转 //判断是否到了 间隔时间 if (millis() - previousMillis > interval) { Serial.println("time is out"); //暂停13秒 stepper.step(0); delay(13000); //步进电机反转 intstep=-intstep; stepper.step(intstep); //储存上次时间 previousMillis = millis(); } } 后记:如果仅用delay()函数想实现步进的电机的正转多少时间,反转多少时间行不通 void loop() { stepper.step(intstep);//步进电机正转 delay(13000);//此法错误,步进电机不转,端口8与10高电平,9与1`低电平保持13秒 //暂停13秒 stepper.step(0); delay(13000); Serial.println("Now is pause ."); //步进电机反转 intstep=-intstep; stepper.step(intstep); delay(13000); //暂停13秒 stepper.step(0); delay(13000); Serial.println("Now is pause ."); |