当前位置:网站首页>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);
}
边栏推荐
- 工作5年,测试用例都设计不好?来看看大厂的用例设计总结
- 辛普森悖论
- recursion: method calls itself
- solidity
- C语言七夕来袭!是时候展现专属于程序员的浪漫了!
- 企业防护墙管理,有什么防火墙管理工具?
- 【Leetcode】478. Generate Random Point in a Circle(配数学证明)
- 类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
- 【Leetcode】2360. Longest Cycle in a Graph
- 月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
猜你喜欢

【三子棋】C语言实现简易三子棋

一款简洁的文件传输工具

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

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

20220725 Information update

SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture

solidity

Get piggy homestay (short-term rental) data

cdh6 opens oozieWeb page, Oozie web console is disabled.

Architecture basic concept and nature of architecture
随机推荐
【Leetcode】478. Generate Random Point in a Circle(配数学证明)
security跨域配置
Unity—四元数、欧拉角API+坐标系统
程序员还差对象?new一个就行了
【MySQL系列】MySQL索引事务
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
使用Ganache、web3.js和remix在私有链上部署并调用合约
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
使用Jenkins做持续集成,这个知识点必须要掌握
【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
C language Qixi is coming!It's time to show the romance of programmers!
FAST-LIO2代码解析(二)
接地气讲解TCP协议和网络程序设计
认识USB、Type-C、闪电、雷电接口
async/await 原理及执行顺序分析
22.支持向量机—高斯核函数
ansible模块--copy模块
Architecture basic concept and nature of architecture
EasyExcel的简单读取操作