def test_Buzzer(): log.basicConfig(level=log.DEBUG) for i in range(10): # 建议输出2000~5000HZ 的PWM波形 # 范围可以自己选择, 0~1 duty_cycle = random.uniform(0.1, 0.8) HZ = random.randint(2000, 5000) outputpwm(HZ, duty_cycle, 500) time.sleep_ms(1500) pass
def setAll(red, green, blue): for i in range(num_leds): pixels[i] = (red, green, blue) pixels.show() colorsList = [RED,YELLOW,ORANGE,GREEN,TEAL,CYAN,BLUE,PURPLE,MAGENTA,WHITE] lastColor = 0 colorNow = 0 lastMode = 0 mode = 5 lastSpd = 0 spd = 0 while True: while spd == lastSpd: spd = random.uniform(0.01, 0.08) lastSpd = spd while colorNow == lastColor: colorNow = random.randint(0, len(colorsList)-1) lastColor = colorNow #while mode == lastMode: #mode = random.randint(0,4) #lastMode = mode # for i in range(len(colorsList)): colorDemos = colorsList[colorNow] if mode == 5: loop_swirl(100, spd) if mode == 4: theaterDemo(colorDemos,spd,11)
pressed = False led = machine.Pin(15, machine.Pin.OUT) button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN) def button_handler(pin): global pressed if not pressed: pressed = True timer_reaction = utime.ticks_diff(utime.ticks_ms(), timer_start) print("Your reaction time was " + str(timer_reaction) + " milliseconds!") led.value(1) utime.sleep(urandom.uniform(5, 10)) led.value(0) timer_start = utime.ticks_ms() button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler) import machine import utime import urandom pressed = False led = machine.Pin(15, machine.Pin.OUT) left_button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN) right_button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN) def button_handler(pin):
【本例程可以屏蔽下面这一行!】 ''' # checknet.wait_network_connected() # urandom.randint(start, end) # 随机1 ~ 4之间 num = random.randint(1, 4) random_log.info(num) # random between 0~1 num = random.random() random_log.info(num) # urandom.unifrom(start, end) # 在开始和结束之间生成浮点数 num = random.uniform(2, 4) random_log.info(num) # urandom.randrange(start, end, step) # 2-bit binary,the range is [00~11] (0~3) num = random.getrandbits(2) random_log.info(num) # 8-bit binary,the range is [0000 0000~1111 11111] (0~255) num = random.getrandbits(8) random_log.info(num) # urandom.randrange(start, end, step) # 从开始到结束随机生成递增的正整数 num = random.randrange(2, 8, 2) random_log.info(num)
assert 0 <= random.randint(0, 4) <= 4 assert 2 <= random.randint(2, 6) <= 6 assert -2 <= random.randint(-2, 2) <= 2 # empty range try: random.randint(2, 1) except ValueError: print('ValueError') print('choice') lst = [1, 2, 5, 6] for i in range(50): assert random.choice(lst) in lst # empty sequence try: random.choice([]) except IndexError: print('IndexError') print('random') for i in range(50): assert 0 <= random.random() < 1 print('uniform') for i in range(50): assert 0 <= random.uniform(0, 4) <= 4 assert 2 <= random.uniform(2, 6) <= 6 assert -2 <= random.uniform(-2, 2) <= 2
left_button = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN) right_button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN) fastest_button = None timer_reaction = 0 pressed = False def button_handler(pin): global pressed if not pressed: pressed=True global timer_reaction timer_reaction = utime.ticks_diff(utime.ticks_ms(), timer_start) global fastest_button fastest_button = pin led.value(1) utime.sleep(urandom.uniform(2, 5)) led.value(0) timer_start = utime.ticks_ms() left_button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler) right_button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler) while fastest_button is None: utime.sleep(1) if fastest_button is left_button: print("Left Player wins!") elif fastest_button is right_button: print("Right Player wins!") print("Winner reaction time was " + str(timer_reaction) + " milliseconds!")
def measure(self): self._temperature = urandom.uniform(*self._temperature_range) self._humidity = urandom.uniform(*self._humidity_range)
#data logging mas datos... import machine import utime import urandom datos = open('log01.txt', 'w') datos.write('Numeros random ' + '\n') datos.close() utime.sleep(1) datos = open('log01.txt', 'a') for eventos in range(1, 101): azar = urandom.uniform(1, 100) datos.write(str(eventos) + '; ' + str(azar) + '\n') datos.flush() datos.close()
from machine import Pin, Timer import urandom #Define onboard LED led = Pin(25, Pin.OUT) LED_state = True tim = Timer() #Select random frequency and print it random_number = urandom.uniform(1, 35) print (random_number) #Set the timer def tick(timer): global led, LED_state LED_state = not LED_state led.value(LED_state) tim.init(freq=random_number, mode=Timer.PERIODIC, callback=tick)
for i in range(leds): pi[i] = color pi.show() time.sleep(spd) clrLst = [RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE, PURPLE, MAGENTA, WHITE] lstclr = 0 clrNow = 0 lstMode = 0 mode = 0 lstSpd = 0 spd = 0 while True: while spd == lstSpd: spd = random.uniform(0.02, 0.08) lstSpd = spd while clrNow == lstclr: clrNow = random.randint(0, len(clrLst) - 1) lstclr = clrNow while mode == lstMode: mode = random.randint(0, 8) lstMode = mode mode = 3 # for i in range(len(clrLst)): clrDs = clrLst[clrNow] if mode == 0: for i in range(5): colorWiped(CYAN, spd, 1) colorWiped(BLACK, spd, 1)