当前位置:网站首页>EPP+DIS学习之路(2)——Blink!闪烁!
EPP+DIS学习之路(2)——Blink!闪烁!
2022-07-07 10:12:00 【MakeMaker】
上一篇搞定了编程软件和基本的程序上传,抄了一段程序实现了板载LED的控制,那这一篇开始就要学一学这个python语言了,先来看上一篇的程序:
import machinepin = 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,timepin = 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,timepin = 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就会闪烁了!
边栏推荐
- Steps of redis installation and self startup configuration under CentOS system
- 全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
- 【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
- Summed up 200 Classic machine learning interview questions (with reference answers)
- Flet tutorial 17 basic introduction to card components (tutorial includes source code)
- Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
- Fleet tutorial 15 introduction to GridView Basics (tutorial includes source code)
- [filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
- 108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
- Up meta - Web3.0 world innovative meta universe financial agreement
猜你喜欢

Common locking table processing methods in Oracle

MySQL安装常见报错处理大全

STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动

MATLAB實現Huffman編碼譯碼含GUI界面

Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB

Rationaldmis2022 advanced programming macro program
![110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]](/img/62/1ec8885aaa2d4dca0e764b73a1e2df.png)
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]

Mastering the new functions of swiftui 4 weatherkit and swift charts

Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt

Matlab implementation of Huffman coding and decoding with GUI interface
随机推荐
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Present pod information to the container through environment variables
相机标定(2): 单目相机标定总结
Rationaldmis2022 array workpiece measurement
Problem: the string and characters are typed successively, and the results conflict
Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
Summed up 200 Classic machine learning interview questions (with reference answers)
Software design - "high cohesion and low coupling"
Rationaldmis2022 advanced programming macro program
2022年在启牛开华泰的账户安全吗?
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Sonar:cognitive complexity
Mastering the new functions of swiftui 4 weatherkit and swift charts
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
清华姚班程序员,网上征婚被骂?
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
MATLAB實現Huffman編碼譯碼含GUI界面