2019秋季学期Arduino 第一课
秋季
学期
2019年9月20日Arduino 第一课
0
第一部分
基础知识回顾
1.Arduino 基本操作

首选项菜单栏 ↑

不同库
↓ 库管理器菜单栏

启用代码折叠
步骤:文件-首选项-启用代码折叠&显示行号
优点:使程序更为清晰,在程序较长时方便整理。
库管理器
步骤:项目-加载库-管理库
特点:库是arduino作为开源编程软件的主要特点,在这里可以下载国内外社区的封装好的不同库来编写自己的程序

Arduino IDE 程序块

定义区域:用来完成加载库,创建对象,定义变量等。存在于void setup()和void loop()之外,通常在程序开头。
初始化区域:存在于void setup()中,对相关引脚、终端操作等进行初始化,只运行一次。
主程序:存在于void loop()中,是程序主体。在loop()中的程序启动后会循环执行。


2.arduino语法回顾(C语言)

详情可见ppt~~


第二部分
实践(舵机)
接线部分

红线接5V;棕线接GND;
黄线为信号线,接PWM数字口
PWM数字口:Arduino UNO板中数字口数字标号旁有~标记的端口,可发送高频PWM信号。

程序部分:
将舵机设定成某一角度
打开 文件-示例-Servo-knob




主要语句
1.
myservo.attach(9)
myservo指自己创建的Servo类对象, 9指黄线所接的PWM数字口,作用是发出信号控制舵机的旋转角度。
2.
myservo.write(val)
语句作用是设置舵机的角度,角度为绝对值而非相对转动值,范围是0-180,在实际操作中由于硬件的限制,不建议数字写满。




程序部分:舵机连续旋转
打开
文件-示例-Servo-sweep

主要语句
1.
For循环部分
用法与C语言一致作用是使舵机每次以一度的公差顺时针旋转到180度再逆时针旋转回0度。
--end--
声明:本文章由网友投稿作为教育分享用途,如有侵权原作者可通过邮件及时和我们联系删除:freemanzk@qq.com