from machine import RTC,LED,delay ledy =LED('ledy') rtc=RTC() def tick_cb(): ledy.toggle() rtc.datetime ((2021,7,21,2,4,12,3,1)) rtc.tickcallback(tick_cb) while True: delay (100) machine.stop()
from machine import UART,delay,LED # Lite BLE on UART port 1, baudrate is 115200) ble=UART(1,115200,timeout=200) ledy = LED('ledy') ledr = LED('ledr') #確保 BLE 回到 CMD mode ble.write('!CCMD@') delay(150) ble.write('!CCMD@') delay(150) # enable BLE System MSG ble.write('AT+EN_SYSMSG=1\r\n') delay(50) while True: msg = ble.readline() print (msg) recv_data = str(msg,'utf-8') # 200ms will return a data print (recv_data) if recv_data == 'A' : ledy.toggle() if recv_data == 'B' : ledr.toggle() if recv_data == 'b' : ledr.toggle()
# need update ePy-Lite micropython image to V1.5 from machine import LED, Pin, ADC, Switch, UART import utime, micropython, sys, gc # setting 接收 ADVERT information ADV_NAME = '08167319' myID = '01' ledy = LED('ledy') ledr = LED('ledr') ledg = LED('ledg') ledrgb = LED(LED.RGB) ledy.off() ledr.off() ledg.off() keya = Switch('keya') #使用 UART1 連接 BLE,並增加接收 Buffer BLE_uart = UART(1, 115200, read_buf_len=1024) # 確認切到 command mode BLE_uart.write('!CCMD@') utime.sleep_ms(200) BLE_uart.write('!CCMD@') utime.sleep_ms(200) ledy.toggle()
from machine import RTC from machine import LED rtc = RTC() #创建RTC对象 print(rtc.now()) #获取当前日期时间 rtc.alarm(time=10, repeat=True) #设置RTC 10秒警报, 並且重复设置 l = LED(1) #创建LED1为输出对象 irq_count = 0 def alarm_handler(t): #时钟警报回调函数 global irq_count if (irq_count % 2) == 0: l.on() else: l.off() irq_count += 1 rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler) #创建由系统时钟警报触发的中断对象
''' CMC GMFS02EVB pin1 -- AIN0 (up) pin4 -- AIN1 (down) pin3 -- GND pin2 -- 3.3V ''' from machine import UART, ADC, Pin, LED import utime rled = LED('ledr') gled = LED('ledg') rgbled = LED(LED.RGB) rled.off() gled.off() adc0 = ADC(Pin.board.AIN0) adc1 = ADC(Pin.board.AIN1) count = 0 while True: adc_0 = adc0.read() adc_1 = adc1.read() report0 = adc_0 * 3.3 / (2**12) report1 = adc_1 * 3.3 / (2**12) if (report0 - report1 >= 1.2): if count == 3: count = 1 else: count = count + 1 if count == 1: rgbled.rgb_write(1, 255, 0, 0) rgbled.rgb_write(2, 0, 255, 0)
from machine import LED l = LED(LED.RGB) #创建RGB LED print('show render') for i in range(0, 256, 1): #RGB渐亮渐灭变化 for j in range(1, 6, 1): l.rgb_write(j, i, 0, 0) for i in range(0, 256, 1): for j in range(1, 6, 1): l.rgb_write(j, 255, i, 0) for i in range(0, 256, 1): for j in range(1, 6, 1): l.rgb_write(j, 255, 255, i) times = 0 while times < 10: for i in range(255, 1, -1): for j in range(1, 6, 1): l.rgb_write(j, i, i, i) for i in range(0, 256, 1): for j in range(1, 6, 1): l.rgb_write(j, i, i, i) times = times + 1 for i in range(255, 1, -1): for j in range(1, 6, 1): l.rgb_write(j, i, i, i)
from machine import LED, delay ledrgb = LED(LED.RGB) ledY = LED('ledy') RgbLedArray1 =[\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] RgbLedArray2 =[\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] RgbLedArray3 =[\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],[0,100,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] RgbLedArray4 =[\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\ [0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],\
i2c_bus.send(bytearray([GMP102_REG_CMD, T_Forced_mode]), GMP102_ADDR) while GP102_DATA_Ready(i2c_bus) != True: pass i2c_bus.send(GMP102_REG_TEMPH, GMP102_ADDR) i2c_bus.recv(temp_data, GMP102_ADDR) # print("".join("\\x%02x" % i for i in temp_data)) data = struct.unpack('>h', temp_data) return (data[0]) # Start Function if __name__ == '__main__': p = [0] * 9 KeyA = Switch('keya') #Create button A led = LED('ledg') led.off() GMP102_i2c = I2C( 0, I2C.MASTER, baudrate=100000) #Create I2C0 Master Mode, Baudrate=100kHz p = GMP102_init(GMP102_i2c) while True: led.on() press_raw = GMP102_PRESSURE_READ(GMP102_i2c) temp_raw = GMP102_TEMP_READ(GMP102_i2c) led.off() temp = temp_raw / GMP102_TEMPERATURE_SENSITIVITY
['01','0A','1F','00','00'],\ ['01','0A','00','00','00']] # 定義廣播封包前識別碼 '7319', type '16' '08' 是廣播封包長度 8byte # len = 16 ,73, 19 ,ID, ,lednumber , Rcolor, Gcolor , Bcolor ; 最大可達30 byte ADV_NAME = '08167319' ID = '01' ledNumber = '01' Rcolor = '8F' Gcolor = '00' Bcolor = '00' intev_time = 1000 counter = 0 ledy = LED('ledy') ledr = LED('ledr') ledg = LED('ledg') ledrgb = LED(LED.RGB) ledy.off() ledr.off() ledg.off() # 使用 KeyA 去做 templet 切換 keya = Switch('keya') # 設定 0.5s 觸發時鐘 定時更改廣播訊息 tim_3 = Timer(3, freq=2)
from machine import KEY from machine import PIN from machine import LED led = LED(LED.LED1) #创建红灯LED1 led.on() #红灯开启 def toggle(t): #中断回调函数 led.toggle() #LED状态改变 k = KEY(KEY.KEYA) #创建按键A k.value() #读取按键值 k.irq(trigger=PIN.IRQ_BOTH, handler=toggle) #按键上升缘或下降缘产生中断
''' Touch Key SIG --> P17 VCC --> 3.3V GND --->GND when Touch the LED(Y) will off P17 is High) release touch LED(Y) will on ( P17 is Low) ''' from machine import Pin, LED import utime ledy = LED('ledy') ledg = LED('ledg') p17 = Pin(Pin.board.P17, Pin.IN) ledg.off() while True: print(p17.value()) if p17.value() == 0: ledy.on() else: ledy.off() utime.sleep_ms(500)
from machine import Timer from machine import LED tim = Timer(1, mode=Timer.PERIODIC) #创建Timer 1为一个频率定期运行对象 tim_a = tim.channel(Timer.CH_0, freq=1) #创建Timer A其频率为1Hz l = LED(1) led_off = 2 led_on = 3 irq_count = 0 def toggle(t): #中断回调函数 global irq_count if irq_count < led_off: l.off() else: l.on() if irq_count < (led_off + led_on - 1): irq_count = irq_count + 1 else: irq_count = 0 tim_a.irq(trigger=Timer.TIMEOUT, handler=toggle) #回调函数将使用设置的频率定期执行中断
''' BLE send 'A' CMD to open Lite's Y LED send 'B' to Close Y LED ''' from machine import I2C, UART, LED import utime HTU21D_ADDR = 0x40 temp = bytearray(2) led = LED('ledy') i2c0 = I2C(0, I2C.MASTER, baudrate=400000) ble = UART(1, 115200) ble.write('AT+MODE_DATA\r\n') def Read_BLE(): msg = ble.read(ble.any()) if msg != '': return (msg) else: return (None) while True: m = str(Read_BLE(), 'utf-8') print(m) if m == 'A': led.off() elif m == 'B': led.on() i2c0.send(0xE3, HTU21D_ADDR)
from machine import LED, delay ledR = LED('ledr') ledY = LED('ledy') ledG = LED('ledg') ledR.off() ledY.off() ledG.off() delay(1000) ledR.on() ledY.on() ledG.on() while True: ledG.toggle() delay(1000)
from machine import LED total_pin = 4 pin_num=1 while pin_num < (total_pin+1): led_test=LED(int(pin_num)) #LED1~4循序亮灭 print(led_test) led_test.on() for j in range(0,5000,1): continue led_test.off() for j in range(0,5000,1): continue led_test.toggle() for j in range(0,5000,1): continue led_test.toggle() for j in range(0,5000,1): continue pin_num=pin_num+1
# main.py -- put your code here! from machine import LED,Pin,ADC,Switch import utime import uos as os import micropython,sys ledy = LED('ledy') ledr = LED('ledr') ledg = LED('ledg') ledy.off() ledr.off() ledg.off() micropython.kbd_intr(-1) while True: ch = sys.stdin.read(1) if ch == '/r': break count =0 while True: ch = sys.stdin.read(2) if ch == 'Q0': break if ch == 'R0': ledr.toggle() if ch == 'G0': ledg.toggle() if ch == 'Y0': ledy.toggle()
''' 溫度 LM35 Out -- AIN0 GND -- GND VCC -- 3.3V ''' from machine import UART, ADC, Pin, LED import utime rled = LED('ledr') gled = LED('ledg') rgbled = LED(LED.RGB) rled.off() gled.off() adc0 = ADC(Pin.board.AIN0) count = 0 while True: ''' Lite ADC have 12bit ''' adc_0 = adc0.read() >> 2 # temp = (adc_val /1024(10bit) *ADC_VCC ) *0.01 oC temp = (adc_0 / 1024 * 3.3) / 0.01 print('{:3.2f} oC'.format(temp)) utime.sleep_ms(1000)
from machine import LED from machine import Switch ledG = LED('ledg') ledR = LED('ledr') ledY = LED('ledy') keyA = Switch('keya') def KeyA_Function(): print('KeyA be pressed') ledG.toggle() keyA.callback(KeyA_Function) while True: pyb.delay(3000) pass
from machine import PIN from machine import LED total_pin = 35 pin_title = 'P' pin_num = 0 failNum = 0 led = LED(1) led.on() led.off() irq_count = 0 def count(t): global irq_count if irq_count < 5: irq_count = irq_count + 1 else: irq_count = 0 while pin_num < total_pin: if pin_num == 17 or pin_num == 18: pin_num += 1 continue gpio_set = pin_title + str(pin_num) p_test = PIN(gpio_set) #创建I/O p_test.mode(PIN.OUT) #设置引脚模式为输出 p_test.drive(PIN.LOW_POWER) #设置引脚驱动强度 p_test.value(1) #设置引脚的值 print(p_test) cmp_str = "Pin('" + gpio_set + "', mode=Pin.OUT, pull=Pin.INACTIVE, drive=Pin.LOW_POWER, alt=0)"
from machine import LED, delay ledrgb = LED(LED.RGB) while True: for i in range(1, 61, 3): for color in range(10, 101, 10): ledrgb.rgb_write(i, color, 0, 0) delay(20) for i in range(2, 61, 3): for color in range(10, 101, 10): ledrgb.rgb_write(i, 0, color, 0) delay(20) for i in range(3, 61, 3): for color in range(10, 101, 10): ledrgb.rgb_write(i, 0, 0, color) delay(20)