Modifications

Sauter à la navigation Sauter à la recherche
1 191 octets ajoutés ,  9 avril 2025 à 20:34
Ligne 180 : Ligne 180 :     
Le script [https://github.com/mchobby/pico-2-explorer/blob/main/projects/theremin/theremin.py theremin.py] est disponible dans le [https://github.com/mchobby/pico-2-explorer dépôt dédié au Pico-2-Explorer].
 
Le script [https://github.com/mchobby/pico-2-explorer/blob/main/projects/theremin/theremin.py theremin.py] est disponible dans le [https://github.com/mchobby/pico-2-explorer dépôt dédié au Pico-2-Explorer].
 +
 +
Cette version du Théremin divise l'espace des 30cm au dessus de la photo résistance en une progression linéaire de la fréquence entre 523 Hz (DO4) à 1046 Hz (DO5).
    
<syntaxhighlight lang="python" line>
 
<syntaxhighlight lang="python" line>
 +
from machine import Pin, ADC, PWM
 +
from math import exp
 +
from maps import map
 
import micropython, time
 
import micropython, time
−
from random import randint
+
micropython.alloc_emergency_exception_buf(100)
 +
 
 +
C = 79.6778221
 +
D = 499
 +
 
 +
DO4 = 523
 +
DO5 = 1046
 +
 
 +
Pin( 23, Pin.OUT, value=True )
 +
 
 +
counter = 0
 +
def bouton_cb( obj ):
 +
    global counter
 +
    counter += 1
 +
 
 +
btn = Pin( Pin.board.GP16, Pin.IN, Pin.PULL_UP )
 +
btn.irq( handler=bouton_cb, trigger=Pin.IRQ_FALLING )
 +
 
 +
buzzer = PWM( Pin( Pin.board.GP13 ) )
 +
buzzer.freq( 500 )
 +
buzzer.duty_u16( 0 ) # silence
 +
 
 +
a1 = ADC( Pin( Pin.board.GP27 ) )
 +
while True:
 +
    if counter>0:
 +
        if buzzer.duty_u16() == 0:
 +
            buzzer.duty_u16( 65535//3 )
 +
        else:
 +
            buzzer.duty_u16( 0 )
 +
        time.sleep_ms(300)
 +
        counter=0
 +
        continue
 +
    # lecture analogique sur 10 bits
 +
    val = 0
 +
    for i in range( 10 ):
 +
        val += (a1.read_u16()>>6)
 +
    val = val/10 # Moyenne
 +
   
 +
    cm = exp( (val-D)/C )
 +
   
 +
    freq = int( map( cm, 1, 30, DO4, DO5 ) )
 +
    buzzer.freq( freq )
 +
    print( 'adc_10bits=%5i , cm=%2.2f, freq=%i Hz' % (val,cm,freq) )
 +
    time.sleep_ms( 20 )
 
</syntaxhighlight>
 
</syntaxhighlight>
−
      
== Ressources ==
 
== Ressources ==
32 920

modifications

Menu de navigation