当前位置:网站首页>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);
}
边栏推荐
- 已知中序遍历数组和先序遍历数组,返回后序遗历数组
- Arduino 基础语法
- C language Qixi is coming!It's time to show the romance of programmers!
- Keepalived 高可用的三种路由方案
- 【Leetcode】479. Largest Palindrome Product
- 【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
- 22. The support vector machine (SVM), gaussian kernel function
- PHP从txt文件中读取数据的方法
- background-image使用
- 不就是个TCC分布式事务,有那么难吗?
猜你喜欢
随机推荐
06-SDRAM : SDRAM control module
els 方块变形
Quick solution for infix to suffix and prefix expressions
【Leetcode】479. Largest Palindrome Product
easy-excel 解决百万数据导入导出,性能很强
微软电脑管家V2.1公测版正式发布
security跨域配置
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
工业信息物理系统攻击检测增强模型
CRS 管理与维护
QML package management
LeetCode_322_零钱兑换
【Leetcode】478. Generate Random Point in a Circle(配数学证明)
短视频SEO搜索运营获客系统功能介绍
以交易为生是一种什么体验?
协作乐高 All In One:DAO工具大全
How to solve the error when mysql8 installs make
一文概览最实用的 DeFi 工具
【Leetcode】470. Implement Rand10() Using Rand7()
NFT工具合集