当前位置:网站首页>Arduino 基础语法
Arduino 基础语法
2022-08-02 00:00:00 【2021 Nqq】
文章目录
点亮LED灯 basic01
// 初始化函数
void setup() {
//将LED灯引脚(引脚值为13,被封装为了LED_BUTLIN)设置为输出模式
pinMode(LED_BUILTIN, OUTPUT);
}
// 循环执行函数
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // 打开LED灯
delay(1000); // 休眠1000毫秒
digitalWrite(LED_BUILTIN, LOW); // 关闭LED灯
delay(1000); // 休眠1000毫秒
}
通信
上下通信

引脚操作
处理器在引脚写数据是输出output
处理器从引脚读数据是输入input
时间函数

串行通信
下位机到上位机 basic02
下位机向上位机发送数据的话,TX引脚会闪烁
/* * 需求: 向 arduino 向PC 发送数据: hello world * 实现: * 1. 设置波特率 * 2. Serial.print()或println()发送数据 * */
void setup() {
Serial.begin(57600);
}
void loop() {
delay(3000);
Serial.print("hello ");
Serial.println("world");
}
上位机到下位机 basic03
上位机向下位机发送数据的话,RX引脚会闪烁
/* * 需求: 由上位机向 arduino 发送数据 * 实现: * 1. 设置波特率 * 2. 在 loop 中读数据 * */
void setup() {
Serial.begin(57600);
}
void loop() {
if(Serial.available() > 0){
char num = Serial.read();
// 输出到上位机
Serial.print("I accept: ");
Serial.println(num);
}
}
数字IO操作(已演示)
模拟IO操作 basic04
控制LED灯亮度
PWM:脉冲宽度调制技术,设置占空比为LED间歇性供电,PWM的取值范围是[0,255]
0——low,255——high
假设是PWM = 50,high比例是 50/255 约等于1/5,low比例约是4/5
假设是PWM = 100,high比例是 100/255 约等于2/5,low比例约是3/5
亮度多少就是通过设置的数字与255相除,然后计算出来百分比
/* * 需求: 控制LED亮度 * 使用的知识点: PWM + analogWrite * * 1. 封装变量: led 引脚,不同亮度级别 * 2. 设置LED操作模式 OUTPUT * 3. loop 中隔某个时间设置不同的PWM值 * */
int led = 13;
int l1 = 255;
int l2 = 127;// 半亮
int l3 = 0;
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
}
void loop() {
delay(2000);
analogWrite(led,l1);
delay(2000);
analogWrite(led,l2);
delay(2000);
analogWrite(led,l3);
}
时间函数 basic05
/* * 演示时间函数: millis()与delay() * 需求: 每休眠 2000 ms, 调用一次 millis() 获取时刻,并打印 * */
void setup() {
// put your setup code here, to run once:
// 需要打印到PC端,需要设置波特率
Serial.begin(57600);
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
unsigned long t = millis();
Serial.print("time = ");
Serial.println(t);
}
边栏推荐
猜你喜欢

Get piggy homestay (short-term rental) data

一个有些意思的项目--文件夹对比工具(一)

【Leetcode】1206. Design Skiplist

Architecture basic concept and nature of architecture

20220725 Information update

使用Ganache、web3.js和remix在私有链上部署并调用合约

How to get the best power efficiency in Windows 11?

Win11内存管理错误怎么办?

security CSRF漏洞保护

GetHashCode方法与=
随机推荐
How to get the best power efficiency in Windows 11?
GIF制作-灰常简单的一键动图工具
ES中SQL查询详解
Quartus 使用 tcl 文件快速配置管脚
如何优雅的消除系统重复代码
根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
尚硅谷MySQL学习笔记
Various Joins of Sql
OpenCV DNN blogFromImage()详解
Enterprise firewall management, what firewall management tools are there?
机器学习文本分类
Use Jenkins for continuous integration, this knowledge point must be mastered
cdh6 opens oozieWeb page, Oozie web console is disabled.
@Scheduled注解详解
好好活就是做有意义的事,有意义的事就是好好活
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
async和await用法介绍
windows sql server 如何卸载干净?
架构基本概念和架构本质
经典文献阅读之--DLO