“步进电机控制”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(Created page with "{| style="width: 800px;" |- | ==目的== 本教程将教大家如何用processing来控制步进电机,向左转向右转。 ==设备== *'''Microduino-Core''' *'''Mi...")
 
Pkj讨论 | 贡献
程序
第29行: 第29行:
 
==程序==
 
==程序==
  
MicroduinoStepperControl
+
https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/MicroduinoStepperControl
  
ProcessingStepperControl
+
https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/ProcessingStepperControl
  
 
==调试==
 
==调试==

2014年6月3日 (二) 08:27的版本

目的

本教程将教大家如何用processing来控制步进电机,向左转向右转。

设备

Microduino-A4982

  • 其他硬件设备
    • USB数据连接线 一根
    • 洞洞板
    • 铜柱、螺丝
  • 焊接工具
    • 电烙铁
    • 焊锡
    • 导线
    • 镊子


原理图

Stepper motor.jpg

程序

https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/MicroduinoStepperControl

https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/ProcessingStepperControl

调试

硬件搭建部分与进阶教程第三十八课步进电机驱动类似: http://www.microduino.cc/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E5%8D%81%E5%85%AB%E8%AF%BE--Microduino_%E6%AD%A5%E8%BF%9B%E7%94%B5%E6%9C%BA%E9%A9%B1%E5%8A%A8/zh

  • 步骤一:按规则大小裁剪洞洞板;
  • 步骤二:确定好Microduino底座位置,按原理图焊接电路;
洞洞板焊法
正面
反面
  • 搭建电路
ok

步骤三:解释一下代码:

本例需要两端的代码,Processing端和Microduino端

Microduino:

//读取串口数据

 void loop() {
   if(Serial.available())
   {
     command=Serial.read();
     Serial.println(command);
     if(command=='l') {//如果processing端发来的数据是’l’,步进电机向左转
       stepper.setSpeed(motorSpeed);
       stepper.runSpeed();
     } 
     else if(command=='r') {//如果processing端发来的数据是’r’,步进电机向右转
       stepper.setSpeed(-motorSpeed);
       stepper.runSpeed();
     }
     else {
       stepper.stop();//否则停止转动
     }
   }
 }


Processing:

//在绘制函数中,做一下鼠标选择的相应。

 void draw() 
 {
   background(0);
   image(img,0,0);
   noFill();
   if("left".equals(turning)) {
     rect(30,80,150,210);
     fill(0);        //Specify font color
     text ( "turn left" ,250,20); 
     port.write("l");
   }else if("right".equals(turning)) {
     rect(320,90,150,200);
     fill(0);        //Specify font color
     text ( "turn right" ,250,20); 
     port.write("r");
   }
 }

//鼠标移动事件响应,判断鼠标是否在选择区内

 // When the mouse is moved, the state of the turning is toggled.
 void mouseMoved() {
   if (mouseX > 30 && mouseX < 180 && mouseY > 80 && mouseY < 290) {
     turning = "left";
     cursor(HAND);
   }
   else if (mouseX > 320 && mouseX < 470 && mouseY > 90 && mouseY < 290) {
     turning = "right";
     cursor(HAND);
   }
   else {
     turning = "turning";
     cursor(ARROW);
   }
 }

步骤四:下载代码并编译通过。

步骤五:运行后,鼠标分别放到向左走的人和向右走的人,看看步进电机有啥反应。

结果

鼠标放到向左走的人:

鼠标放到向右走的人:

步进电机会朝着相应的方向转动


视频