查看“第四十一课--Microduino 直流电机正反转/zh”的源代码
←
第四十一课--Microduino 直流电机正反转/zh
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{{Language|Lesson_41--Microduino_Dc_Motor_Rotation_Control}} {| style="width: 800px;" |- | ==目的== 本教程将教大家用一种简单的方法使一个直流电机正转与反转 ==设备== *'''[[Microduino-Core]]''' *'''[[Microduino-FT232R]]''' *其他硬件设备 **面包板跳线 一盒 **面包板 一块 **直流电机 一个 **USB数据连接线 一根 ==原理图== [[File:第四十一课-Microduino直流电机正反转原理图.jpg|600px|center|thumb]] 注意:这不是一种最安全控制电机的方法,因为每个I/O pin只能承受40mA的电流。如果作为实际应用的话,还是建议大家使用类似于H-Bridge的电机驱动板 ==程序== <source lang="cpp"> int motorPin1 = 5; // 一条线连接 digital pin 5 int motorPin2 = 6; // 另一条线连接 digital pin 6 void setup() { // 初始化,设置两个引脚为输出 pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); } void loop() { rotateLeft(150, 500);//左转速率150,持续500ms rotateRight(50, 1000);//右转速率50,持续1000ms rotateRight(150, 1000);//右转速率150,持续1000ms rotateRight(200, 1000);//右转速率200,持续1000ms rotateLeft(255, 500);//左转速率255,持续500ms rotateRight(10, 1500);//右转速率10,持续1500ms } //向左转 void rotateLeft(int speedOfRotate, int length){ analogWrite(motorPin1, speedOfRotate); //用pwm设置电流大小 digitalWrite(motorPin2, LOW); // 设置 Pin motorPin2为LOW delay(length); //waits digitalWrite(motorPin1, LOW); // 设置 Pin motorPin1为LOW } //向右转 void rotateRight(int speedOfRotate, int length){ analogWrite(motorPin2, speedOfRotate); //用pwm设置电流大小 digitalWrite(motorPin1, LOW); // 设置 Pin motorPin1为LOW delay(length); //waits digitalWrite(motorPin2, LOW); // 设置 Pin motorPin2为LOW } //全力向左转 void rotateLeftFull(int length){ digitalWrite(motorPin1, HIGH); //设置最大电流 digitalWrite(motorPin2, LOW); // 设置 Pin motorPin2为LOW delay(length); //waits digitalWrite(motorPin1, LOW); // 设置 Pin motorPin1为LOW } //全力向右转 void rotateRightFull(int length){ digitalWrite(motorPin2, HIGH); //设置最大电流 digitalWrite(motorPin1, LOW); // 设置 Pin motorPin1为LOW delay(length); //waits digitalWrite(motorPin2, LOW); // 设置 Pin motorPin2为LOW } </source> ==调试== 步骤一:把代码复制到IDE中,编译 步骤二:电路连接如下图: [[File:第四十一课-Microduino直流电机正反转连接图.jpg|600px|center|thumb]] 步骤三:运行代码 步骤四:查看电机转动情况 ==结果== 电机会以不同的速率正转,反转,一直循环下去 ==视频== http://v.youku.com/v_show/id_XNjgyNDgzNzM2.html
该页面使用的模板:
模板:Language
(
查看源代码
)
模板:Nmbox
(
查看源代码
)
返回至
第四十一课--Microduino 直流电机正反转/zh
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息