当前位置:网站首页>Arduino Basic Syntax
Arduino Basic Syntax
2022-08-02 00:17: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毫秒
}
通信
Communicate up and down

引脚操作
The processor writes data on the pin as an outputoutput
The processor reads data from the pins as inputsinput
时间函数

串行通信
下位机到上位机 basic02
If the lower computer sends data to the upper computer,TXThe pin will blink
/* * 需求: 向 arduino 向PC 发送数据: hello world * 实现: * 1. 设置波特率 * 2. Serial.print()或println()发送数据 * */
void setup() {
Serial.begin(57600);
}
void loop() {
delay(3000);
Serial.print("hello ");
Serial.println("world");
}
From the upper computer to the lower computer basic03
If the upper computer sends data to the lower computer,RXThe pin will blink
/* * 需求: From the host computer arduino 发送数据 * 实现: * 1. 设置波特率 * 2. 在 loop 中读数据 * */
void setup() {
Serial.begin(57600);
}
void loop() {
if(Serial.available() > 0){
char num = Serial.read();
// output to the host computer
Serial.print("I accept: ");
Serial.println(num);
}
}
数字IO操作(已演示)
模拟IO操作 basic04
控制LED灯亮度
PWM:脉冲宽度调制技术,设置占空比为LEDIntermittent power supply,PWM的取值范围是[0,255]
0——low,255——high
假设是PWM = 50,high比例是 50/255 约等于1/5,lowThe ratio is approximately4/5
假设是PWM = 100,high比例是 100/255 约等于2/5,lowThe ratio is approximately3/5
How much brightness is passedSet numbers with255相除,Then calculate the percentage
/* * 需求: 控制LED亮度 * 使用的知识点: PWM + analogWrite * * 1. 封装变量: led 引脚,Different brightness levels * 2. 设置LED操作模式 OUTPUT * 3. loop Set a different time intervalPWM值 * */
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:
// Need to print toPC端,The baud rate needs to be set
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);
}
边栏推荐
- 链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
- JSP out.write()方法具有什么功能呢?
- PHP从txt文件中读取数据的方法
- Study Notes: The Return of Machine Learning
- 【Leetcode】1206. Design Skiplist
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
- C language Qixi is coming!It's time to show the romance of programmers!
- 中缀转后缀、前缀表达式快速解决办法
- CRS 管理与维护
- 2022/08/01 学习笔记 (day21) 泛型和枚举
猜你喜欢

【21天学习挑战赛】顺序查找和二分查找的小总结

当奈飞的NFT忘记了Web2的业务安全

短视频seo搜索优化主要内容

Short video SEO search operation customer acquisition system function introduction

【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件

An interesting project--Folder comparison tool (1)

Using the "stack" fast computing -- reverse polish expression

REST会消失吗?事件驱动架构如何搭建?

IP Core: FIFO

Collection of NFT tools
随机推荐
Double queue implementation stack?Dual stack implementation queue?
DOM 事件及事件委托
Zadig 面向开发者的自测联调子环境技术方案详解
JSP out.write()方法具有什么功能呢?
els strip deformation
为什么要使用MQ消息中间件?这几个问题必须拿下
短视频seo搜索优化主要内容
bgp 聚合 反射器 联邦实验
信息系统项目管理师必背核心考点(五十七)知识管理工具
How does JSP use request to get the real IP of the current visitor?
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
Short video SEO search operation customer acquisition system function introduction
【ACWing】230. 排列计数
双队列实现栈?双栈实现队列?
图解LeetCode——1161. 最大层内元素和(难度:中等)
如何重装Win11?一键重装Win11方法
【Leetcode】475. Heaters
Excel表格数据导入MySQL数据库
【Leetcode】1206. Design Skiplist
Cash Ⅱ LeetCode_518_ change