Modifications

Sauter à la navigation Sauter à la recherche
1 296 octets ajoutés ,  4 avril 2025 à 16:18
Ligne 192 : Ligne 192 :     
<syntaxhighlight lang="python" line>
 
<syntaxhighlight lang="python" line>
 +
import micropython, time
 +
from random import randint
 +
from machine import Pin
   −
xxx
+
micropython.alloc_emergency_exception_buf(100)
 +
 
 +
counter = 0
 +
 
 +
def bouton_cb( obj ):
 +
    global counter
 +
    counter += 1
 +
   
 +
# https://docs.micropython.org/en/latest/library/machine.Pin.html
 +
# https://docs.micropython.org/en/latest/reference/isr_rules.html
 +
btn = Pin( Pin.board.GP10, Pin.IN, Pin.PULL_UP )
 +
btn.irq( handler=bouton_cb, trigger=Pin.IRQ_FALLING )
 +
 
 +
LEDs = [ Pin(Pin.board.GP27, Pin.OUT), Pin(Pin.board.GP26, Pin.OUT),
 +
        Pin(Pin.board.GP22, Pin.OUT) ]
 +
for led in LEDs:
 +
    led.value(0)
 +
 
 +
old_value = 0
 +
while True:
 +
    if counter>0: # Button pressed?     
 +
        # Random number
 +
        new_value = randint(1,6)
 +
        while new_value == old_value:
 +
            new_value = randint(1,6)
 +
       
 +
        # show the number
 +
        old_value = new_value
 +
        print( 'Random = %i = %s' % (new_value,bin(new_value)) )
 +
        # Show the LED value
 +
        s = bin(new_value).replace( '0b','' )
 +
        s = '%3s' % s
 +
        s = s.replace(' ', '0')
 +
        # print( bin(new_value), '->', s )
 +
       
 +
        for i in range(3): # 0..2
 +
            LEDs[i].value( int(s[i]) )
 +
       
 +
        time.sleep_ms( 500 )
 +
        # Reset button detection
 +
        counter=0
 +
       
 +
    else:
 +
        time.sleep_ms(200)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
32 920

modifications

Menu de navigation