|
本帖最后由 BlackCat 于 2012-6-26 23:19 编辑
今天刚刚到了L298电机驱动板套件,用了午休时间焊接完成。
之前购买的二手步进电机,虽然简易方法测试过好坏,但是心里还是不踏实,准备上电测试。
测试方法是:将两个电机相应导线连接,转动其中一个电机,另一个电机也会随之转动。
拿出2560,L298N,杜邦线,电机一系列问题出来了,不会接线,没有实例代码。
搜了论坛的帖子,在百度文库中有一些教程,还是看不明白。
折腾了半天,最后在arduino ide 的例子中找到代码;
读懂一半,另一半还在捉摸,不过好歹电机动了。
学习嘛,就得做做笔记啥的,另一个原因,发现论坛中也没有相关的例子,就上来献丑了。
因为Fritzing中没找到L298n的模块,就用电机驱动扩展板来做例子,接线方式一样。
另外,我只用主板供电,电机转动时稍微给点阻力,电机就空转,不知道是不是此原因,回头研究。
代码: - /*
- Stepper Motor Control - one revolution
-
- This program drives a unipolar or bipolar stepper motor.
- The motor is attached to digital pins 8 - 11 of the Arduino.
-
- The motor should revolve one revolution in one direction, then
- one revolution in the other direction.
-
-
- Created 11 Mar. 2007
- Modified 30 Nov. 2009
- by Tom Igoe
-
- */
- #include <Stepper.h>
- const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
- // for your motor
- // initialize the stepper library on pins 8 through 11:
- Stepper myStepper(stepsPerRevolution, 50,51,52,53);
- int xpotPin = 2; //X的信号输入端口
- int xval=0; //设置变量 上面两段代码是我自己加的,将PS2模块X轴接入2口。
- void setup() {
- // set the speed at 60 rpm: 转速,但是我调到80以上,电机就空转
- myStepper.setSpeed(60);
- // initialize the serial port:
- Serial.begin(9600);
- }
- void loop() {
- xval = analogRead(xpotPin); //xval变量为从2信号口读取到的数值
- if (xval<250) /*这个数在论坛中PS2实验中有提到,是一个阻值。下面两个判断是PS2摇杆模块X轴,阻值小于250正转,大于650翻转,介于250-650之间不转 ,并发送字符串到串口*/
- {
- // step one revolution in one direction:
- Serial.println("clockwise");
- myStepper.step(stepsPerRevolution);
- }
- if (xval>650){
- // step one revolution in the other direction:
- Serial.println("counterclockwise");
- myStepper.step(-stepsPerRevolution);
- }
- }
复制代码 希望会的人给出点指导,不会的人我们也可以一起讨论研究。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|