当前位置:网站首页>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);
}
边栏推荐
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- 基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
- 利用“栈”快速计算——逆波兰表达式
- How to get the best power efficiency in Windows 11?
- 微软电脑管家V2.1公测版正式发布
- els block deformation judgment.
- Multi-feature fusion face detection based on attention mechanism
- TCP 可靠吗?为什么?
- 22. The support vector machine (SVM), gaussian kernel function
- SphereEx苗立尧:云原生架构下的Database Mesh研发实践
猜你喜欢

GIF making - very simple one-click animation tool

ES中SQL查询详解

Excel导入和导出

An overview of the most useful DeFi tools

Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation

PHP从txt文件中读取数据的方法

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

接地气讲解TCP协议和网络程序设计

DOM 基础操作

How to reinstall Win11?One-click method to reinstall Win11
随机推荐
【Leetcode】2360. Longest Cycle in a Graph
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
玩转NFT夏季:这份工具宝典值得收藏
contentEditable属性
ES中SQL查询详解
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
06-SDRAM : SDRAM control module
Axure tutorial - the new base (small white strongly recommended!!!)
els strip deformation
【ACWing】406. 放置机器人
JSP page指令errorPage属性起什么作用呢?
When Netflix's NFTs Forget Web2 Business Security
Ansible中的任务执行控制
电机原理动图合集
Deliver cloud-native microservices applications with Zadig
一文概览最实用的 DeFi 工具
【Leetcode】470. Implement Rand10() Using Rand7()
els 长条变形
基于数据驱动的变电站巡检机器人自抗扰控制
An interesting project--Folder comparison tool (1)