当前位置:网站首页>EPP+DIS学习之路(2)——Blink!闪烁!
EPP+DIS学习之路(2)——Blink!闪烁!
2022-07-07 10:12:00 【MakeMaker】
上一篇搞定了编程软件和基本的程序上传,抄了一段程序实现了板载LED的控制,那这一篇开始就要学一学这个python语言了,先来看上一篇的程序:
import machine
pin = machine.Pin(2, machine.Pin.OUT)
pin.on()
第1句 import machine是加载machine模块,加载了后我们才能对ESP32的引脚进行控制。
第2句machine.Pin(2, machine.Pin.OUT)则是将2号引脚定义为输出引脚
第3句pin.on()就是让这个引脚输出高电平,也就是3.3V
这个对引脚的控制还可以用下面写法:
pin.value(1)
它们是相同的。
那这一篇就先让这个LED灯闪烁起来,先修改程序如下:
import machine,time
pin = machine.Pin(2, machine.Pin.OUT)
pin.on()
time.sleep(1)
pin.off()
time.sleep(1)
上面用了time.sleep(1),这个语句的意思也很明显,就是让ESP32睡1秒,其实就是延时1秒,有下面几种写法:
time.sleep(1) # 睡眠1秒
time.sleep_ms(500) # 睡眠500毫秒
time.sleep_us(10) # 睡眠10微妙
当然使用time函数之前先要加载time模块,可以直接跟在machine后面,加个逗号
点击绿色的运行按钮,如果接着上一篇的话应该就会迅速的上传完成,其实就是将ESP32里的main.py覆盖了一下,如果是刚打开软件的话,会出现下面的对话框
同样选择MicroPython设备
这时发现ESP32里已经有main.py了,就是我们上节课的程序,现在将其覆盖即可!
点击是后就会上传了。这时你会发现LED就亮了1秒,然后就熄灭了,没有闪烁。我们再来看看程序:
pin.on()
time.sleep(1)
pin.off()
time.sleep(1)
打开LED,停1秒,然后关掉再停1秒,然后......就没有然后了。可以看到micropython和Arduino的运行机制有些不同,Arduino会一直循环运行loop()里的语句,而micropython只会运行一遍主程序。为了让其一直循环运行,我们得自己写一个类似loop()的语句:
import machine,time
pin = machine.Pin(2, machine.Pin.OUT)
while True:
pin.on()
time.sleep(1)
pin.off()
time.sleep(1)
我们加了第3句while True:
这是个while循环语句,只要while后面的语句为真,函数内的语句就会循环运行,我们直接写了while True:那么就永远为真了,函数内的语句就会永远运行下去。注意函数包含的语句要与前面空四个空格,这是python的编程格式,在C里面是{ },在python里就是空四个空格。具体的自己去百度python的基础教程。
再次上传,这下ESP32上的LED就会闪烁了!
边栏推荐
- Present pod information to the container through environment variables
- MATLAB實現Huffman編碼譯碼含GUI界面
- Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
- 超标量处理器设计 姚永斌 第8章 指令发射 摘录
- Camera calibration (2): summary of monocular camera calibration
- Problem: the string and characters are typed successively, and the results conflict
- SwiftUI 教程之如何在 2 秒内实现自动滚动功能
- 【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
- C#中在路径前加@的作用
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
猜你喜欢
About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
Complete collection of common error handling in MySQL installation
5V串口接3.3V单片机串口怎么搞?
Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
Zero shot, one shot and few shot
Hi3516 full system type burning tutorial
College entrance examination composition, high-frequency mention of science and Technology
Poor math students who once dropped out of school won the fields award this year
随机推荐
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
千人规模互联网公司研发效能成功之路
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
《通信软件开发与应用》课程结业报告
Introduction to three methods of anti red domain name generation
When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
<No. 8> 1816. 截断句子 (简单)
《通信软件开发与应用》课程结业报告
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
2022年在启牛开华泰的账户安全吗?
源代码防泄密中的技术区别再哪里
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
How to write test cases for test coupons?
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
Poor math students who once dropped out of school won the fields award this year
Software design - "high cohesion and low coupling"
Detailed explanation of debezium architecture of debezium synchronization