|
本帖最后由 点点&木木 于 2019-4-12 09:31 编辑 项目介绍:采用stm32和Zerynth组合,实现LED多线程闪烁效果。当你在红外接近传感器上挥手时,会发生一种类似于乐器的改变音高的声光发生器。
这个项目中使用的东西 硬件组件 ST Nucleo F401RE或Arduino DUE或粒子核心。无论您使用哪种主板,Zerynth都是多板兼容的! × 1 夏普红外接近传感器(GP2Y0A21YK)或其他模拟距离传感器 × 1 电阻1k欧姆 × 2 低功率LED(红色和绿色) × 2 压电蜂鸣器 × 1 旋转电位器 × 1 面包板(通用) × 1 跳线包装 × 1 介绍
描述 在这篇介绍中,我们将看到如何制作简化的类似于Theremin的乐器,当您挥动手在红外接近传感器上时,该乐器会改变音量。此外,您可以轻松改变“节拍”的长度,同时驱动各种闪烁的LED(看似)。这是多线程的魔力......它是Zerynth的魔力! Zerynth是一个易于使用的开发套件,用于为物联网准备的交互式对象的高级设计。借助Zerynth的创意,设计师和专业人士可以使用Python开发适用于Arduino DUE,ST Nucleo,Particle Photon和Mikroe Flip&Click以及使用典型PC和移动编程的范例和类似板卡。 本特雷门琴是由俄罗斯物理学家列夫·谢尔盖耶维奇Termen在20年代发明了一种实验性的乐器。在没有物理接触的情况下,两个天线可以感知到玩家在太空中的手的位置,并用它来控制声音的频率和幅度。Theremin已被用于经典电影原声带。 虽然它看起来很复杂,但这个项目需要非常简单的电子设备,因为大多数的工作都是由Zerynth 完成的。 组装电路 每个Sharp IR接近传感器都有三个引脚。一个是电源输入,我们连接到3.3V。另一个是我们将连接到一个GND引脚的接地。最后,模拟输出从10cm处的3.1V变化到80cm处的0.4V。模拟输出引脚需要连接到模拟输入。在这里,我们使用了针脚A5。 压电蜂鸣器使用一种特殊的晶体,当电信号通过时,它会膨胀和收缩。这将产生我们可以听到的音调。压电蜂鸣器的一个引脚连接到GND连接,另一个引脚连接到数字引脚D7(如果使用Spark Core,则另一个引脚具有PWM功能,例如A4)。 旋转可调电位器有三个引脚。将3.3V连接到外部引脚,GND连接到另一个引脚,中心引脚的电压范围为0到3.3V,具体取决于电位器的旋转。将中心引脚连接到微控制器上的ADC,并从用户那里获得可变输入!在这里,我们使用了A2针。 要构建红色LED电路,请将电阻器的一端连接到引脚D8(或Spark Core上的D7)。将LED的长腿(正极,称为阳极)连接到电阻的另一端。将LED的短腿(负极,称为阴极)连接到GND。 使用Zerynth对电路板进行编程 使用Zerynth非常简单!让我们一步一步看看: 安装它并通过Zerynth快捷方式启动它。 通过专用按钮创建Zerynth用户。点击提供的链接,查看您的电子邮件并验证您的新帐户。 验证帐户后,Zerynth Studio会自动将您登录到ZerynthCloud (第一次创建用户帐户时可能需要重新启动。如果您有登录问题,请重新启动IDE)。 连接您的电路板并根据需要重命名,以便将来轻松识别它。 要使电路板可用,您需要对其进行虚拟化。虚拟化是在板上安装Zerynth VM(虚拟机)的过程。可以通过单击ZerynthStudio顶部栏上的专用按钮启动该过程。 创建一个新项目,在此hackster中发布的副本,编译脚本并将其上载到主板中。按照IDE消息,某些板需要在上载过程中手动重置。 享受您的Zeriesnth脚本! 为什么使用Zerynth 许多人在开始为微控制器编写代码时发现具有挑战性的概念之一是如何管理多个与硬件相关的任务,似乎都在同时运行。因此,设计者因为在微控制器中实现这些功能的困难而感到沮丧。 为了解决这些问题,Zerynth支持Python的所有最常用的高级功能,如模块,类,多线程,回调,定时器和异常,以及一些与中断,PWM,数字I / O等自定义硬件相关的功能。。 Zerynth中的每个线程都是一种独立且并行的进程,可以在您的主板上自主运行。线程需要将函数作为定义的输入来执行。相同的函数可以通过各种线程实例化,使您可以编写非常简洁和可读的代码。使用线程,您可以设计算法架构,假设并行性是典型的高级别。 在代码里面 该脚本使用4个并行运行的线程实现。一个线程用于获取和归一化通过电位计和IR接近传感器获取的模拟信号。其他三个线程用于实例化一个通用的blink()函数,该函数驱动不同频率的两个LED,以及一个通用的buzz()函数,用于驱动不同频率睡眠时的蜂鸣器(以创建“节拍”效果),根据采集的模拟信号计算。 delay()与sleep() 在Arduino /接线中使用delay()有副作用 -Arduino没有做任何事情。要使两个或多个“动作”彼此独立运行,您不能使用delay()函数。 在Zerynth中,sleep()函数暂停当前线程的time_units表示的时间,但所有其他线程都可以继续执行! Zerynth内置函数 zerynth VM通过内置函数扩展Python,以处理嵌入式设备的通用输入输出引脚。这些函数类似于Arduino使用的函数,但更灵活。 analogRead()与adc.read() analogRead()函数作为内置提供,以简化从Arduino/ Wiring到Zerynth的通道。但是,在Zerynth中读取模拟引脚的首选方法是: #import the adc driver 导入adc x = adc.read(pin,samples = 1) 从引脚读取模拟值,该引脚必须是Ax引脚之一。如果样本为1或未给出,则返回从pin读取的整数值。如果samples大于1,则返回大小样本的整数元组。 analogWrite()与pwm.write() Arduino的analogWrite()函数为硬件PWM提供了一个简单的接口,但不提供任何频率控制。analogWrite()函数作为内置提供,以简化从Arduino / Wiring到Zerynth的通道。但是在Zerynth中使用pwm的首选方法是: #import pwm driver import pwm pwm.write(pin,period,pulse,time_unit = MILLIS) 根据参数,引脚的状态在LOW和HIGH之间周期性地切换: - 周期是pwm方波的持续时间 - pulse是pwm方波保持在HIGH状态的时间 - time_unit是时间周期的单位,脉冲以time_unit表示 您可以从这个非常简单的示例代码开始,以开发您喜欢的东西。当然还有很大的改进空间,例如: 添加音量控制 利用smartSensors库而不是通过手动映射去模拟输入值范围 随意提出自己的想法,并用Zerynth提供的全新多线程闪烁游戏! 原理图 多线程闪烁Theremin以不同的速率
代码 ################################################################################ # Multithreaded Blinking Theremin - main.py # # Created by Zerynth Team 2015 CC # Authors: L.F. Cerfeda, L. Cominelli ################################################################################ import adc # import the adc driver import pwm # import the pwm module import helpers # import the helpers module # Define the pins where input sensors andoutput is attached to pot_pin = A2 # Analog input from a potentiometer. Tested values range from 0 to about4000 prox_pin = A5 # Analog Input from a proximity sensor. Tested values range from 250 toabout 3800 buzzer = D7.PWM # Use pin D7 of Arduino DUE or ST Nucleo F401RE as PWM. If you use SparkCore choose A4 led_green = D6 led_red = D8 # If you use Spark Core choose D7 # Set the LED pins as outputs pinMode(led_red,OUTPUT) pinMode(led_green,OUTPUT) # Create a dictionary for storing values ofthe input variables to be used in the threads input_val= {'prox_val':1, 'pot_val':1} # Define an analog sensor sampling functionthat acquires the raw data and # normalizes it through the function map_range()definited in the helpers.py # adc.read is the correct way to use adc inZerynth. It is similar at analogRead in Arduino Wiring, but sounds better def sampling(): global input_val while True: input_val['pot_val'] = helpers.map_range(adc.read(pot_pin),0,4000,1,1000) input_val['prox_val'] = helpers.map_range(adc.read(prox_pin),300,3800,1,1000) sleep(50) # Define the 'buzz' function def buzz(input_for_period,input_for_length,buzzer_pin): while True: # Typical piezoelectric buzzer frequenciesrange from 500-4000Hz, so period has to range from 250 us to 2000 us period = helpers.map_range(input_val[input_for_period],1,1000,250,2000) # Set the period of the buzzer and the dutyto 50% of the period through pwm.write # pwm.write is the correct way to use pwmin Zerynth. It is similar at analogWrite in Arduino Wiring, but sounds better # Note that in pwm.write we will use MICROSso every sec is 1000000 micros # // is the int division, pwm.write perioddoesn't accept floats pwm.write(buzzer_pin,period,period//2,MICROS) # Set the length of the sleep to create a"beat" effect (from 1 to 300 ms). The default time unit of sleepfunction is MILLIS length = helpers.map_range(input_val[input_for_length],1,1000,1,300) sleep(length) # Define the 'blink' function def blink(input_for_delay,led_pin): while True: # Set the delay from 1 to 500 ms delay = helpers.map_range(input_val[input_for_delay],1,1000,1,500) digitalWrite(led_pin,HIGH) # turn the LED ON by making the voltage HIGH sleep(delay) # wait for 'delay' ms digitalWrite(led_pin,LOW) # turn the LED OFF by making the voltage LOW sleep(delay) # wait for 'delay' ms # Create the threads that execute instancesof the functions with different parameters thread(sampling) thread(buzz,'prox_val','pot_val',buzzer) thread(blink,'prox_val',led_red) thread(blink,'pot_val',led_green) |
微信公众号
手机版