当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
随机推荐
信息系统项目管理师必背核心考点(五十七)知识管理工具
很多人喜欢用多御安全浏览器,竟是因为这些原因
An interview question about iota in golang
DVWA靶场环境搭建
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
[Three sons] C language implements simple three sons
@WebServlet注解(Servlet注解)
GetHashCode与Equals
CDH6 Hue to open a "ASCII" codec can 't encode characters
Flink Yarn Per Job - 提交流程一
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
如何用Redis实现分布式锁?
Use Jenkins for continuous integration, this knowledge point must be mastered
1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?
Bean的生命周期
【Leetcode】470. Implement Rand10() Using Rand7()
一道golang中关于iota的面试题
为什么要使用MQ消息中间件?这几个问题必须拿下
Win11如何获得最佳电源效率?
QML包管理









