当前位置:网站首页>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);
}
边栏推荐
- CRS 管理与维护
- 2022/08/01 学习笔记 (day21) 泛型和枚举
- JSP Taglib指令具有什么功能呢?
- Statement执行update语句
- 短视频SEO优化教程 自媒体SEO优化技巧方法
- Double queue implementation stack?Dual stack implementation queue?
- easy-excel 解决百万数据导入导出,性能很强
- 如何发现新的潜力项目?工具推荐
- bgp 聚合 反射器 联邦实验
- Short video SEO search operation customer acquisition system function introduction
猜你喜欢
Win11如何获得最佳电源效率?
QML package management
Task execution control in Ansible
Short video SEO optimization tutorial Self-media SEO optimization skills and methods
Short video seo search optimization main content
QML包管理
[Headline] Written test questions - minimum stack
Keepalived 高可用的三种路由方案
background-image使用
When Netflix's NFTs Forget Web2 Business Security
随机推荐
146. LRU 缓存
【无标题】
单片机遥控开关系统设计(结构原理、电路、程序)
磁盘与文件系统管理
JSP out.print()和out.write()方法的不同之处
TCL:在Quartus中使用tcl脚本语言进行管脚约束
How to reinstall Win11?One-click method to reinstall Win11
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
JSP out.println()方法具有什么功能呢?
电机原理动图合集
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
【ACWing】406. 放置机器人
background-image使用
GIF making - very simple one-click animation tool
Unity—四元数、欧拉角API+坐标系统
JSP内置对象out对象的功能简介说明
Keepalived 高可用的三种路由方案
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
Multi-feature fusion face detection based on attention mechanism
Quick solution for infix to suffix and prefix expressions