| Ligne 277 : |
Ligne 277 : |
| | Voici le script [https://github.com/mchobby/pico-2-explorer/blob/main/projects/theremin/theremin2.py theremin2.py], également disponible dans le [https://github.com/mchobby/pico-2-explorer dépôt dédié au Pico-2-Explorer]. | | Voici le script [https://github.com/mchobby/pico-2-explorer/blob/main/projects/theremin/theremin2.py theremin2.py], également disponible dans le [https://github.com/mchobby/pico-2-explorer dépôt dédié au Pico-2-Explorer]. |
| | | | |
| − | Cette version du script Théremin divise l'espace des 30cm au dessus de la photo résistance en 13 espaces 523 Hz (DO4) à 1046 Hz (DO5). | + | Cette seconde version du script Théremin divise l'espace des 30cm au dessus de la photo résistance en 12 espacements identiques correspondant au 13 notes présentes entre DO4 et DO5. |
| | | | |
| | + | <syntaxhighlight lang="python"> |
| | + | # This version of the Theremin divide the 30cm space into |
| | + | # Stepped notes from DO4 at 523 Hz to DO5 at 1046 Hz (13 notes in total). |
| | + | # |
| | + | from machine import Pin, ADC, PWM |
| | + | from math import exp |
| | + | from maps import ranking |
| | + | import micropython, time |
| | + | micropython.alloc_emergency_exception_buf(100) |
| | + | |
| | + | C = 79.6778221 |
| | + | D = 499 |
| | + | |
| | + | DO4 = 523 |
| | + | DO4diese = 554 |
| | + | RE4 = 587 |
| | + | RE4diese = 622 |
| | + | MI4 = 659 |
| | + | FA4 = 698 |
| | + | FA4diese = 740 |
| | + | SOL4 = 784 |
| | + | SOL4diese = 831 |
| | + | LA4 = 880 |
| | + | LA4diese = 932 |
| | + | SI4 = 988 |
| | + | DO5 = 1046 |
| | + | |
| | + | NOTES = [ DO4, DO4diese, RE4, RE4diese, MI4, FA4, \ |
| | + | FA4diese, SOL4, SOL4diese, LA4, LA4diese, SI4, DO5 ] |
| | + | |
| | + | # CM between notes (ranges_of_cm / intervalles) |
| | + | cm_spacing = (30-1) / (len(NOTES)-1) |
| | + | distances = [] |
| | + | for i in range( len(NOTES) ): |
| | + | distances.append( 1+i*cm_spacing ) |
| | + | |
| | + | 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 ) # No sound |
| | + | |
| | + | a1 = ADC( Pin( Pin.board.GP27 ) ) |
| | + | while True: |
| | + | # turn on/off the buzzer |
| | + | 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 |
| | + | |
| | + | val = 0 |
| | + | for i in range( 10 ): |
| | + | val += (a1.read_u16()>>6) |
| | + | val = val/10 |
| | + | |
| | + | cm = exp( (val-D)/C ) |
| | + | |
| | + | freq = ranking( cm, distances, NOTES ) # Notes are int. |
| | + | if freq!=None: |
| | + | buzzer.freq( freq ) |
| | + | print( 'adc_10bits=%5i , cm=%2.2f, freq=%i Hz' % (val,cm,freq) ) |
| | + | time.sleep_ms( 20 ) |
| | + | </syntaxhighlight> |
| | | | |
| | == Ressources == | | == Ressources == |