def brake(self): """ Brake the motor by sending power both directions. """ self._forward.high() self._backward.high() self._speedControl.pulse_width_percent = 100 delay(1000) self.speed = 0
def test_main(): """Test function for verifying basic functionality.""" print("Running test_main") ''' use I2C0 , and must change baudrate to 50kHz ''' i2c = I2C(0, I2C.MASTER, baudrate=50000) lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) lcd.putstr("It Works!\nSecond Line") delay(3000) lcd.clear() count = 0 while True: lcd.move_to(0, 0) lcd.putstr("%7d" % (millis() // 1000)) delay(1000) count += 1 if count % 10 == 3: print("Turning backlight off") lcd.backlight_off() if count % 10 == 4: print("Turning backlight on") lcd.backlight_on() if count % 10 == 5: print("Turning display off") lcd.display_off() if count % 10 == 6: print("Turning display on") lcd.display_on() if count % 10 == 7: print("Turning display & backlight off") lcd.backlight_off() lcd.display_off() if count % 10 == 8: print("Turning display & backlight on") lcd.backlight_on() lcd.display_on()
def turnGreen(light) : machine.delay(fiveSecondsInMS) light[0].value(0); #red off light[1].value(1); #green on light[2].value(0); #yellow off greenLightTimer.start() startTime = time.time()
def turnRed(light) : light[0].value(0); #red off light[1].value(0); #green off light[2].value(1); #yellow on machine.delay(twentySecondsInMS) light[0].value(1); #red on light[1].value(0); #green off light[2].value(0); #yellow off
def demo(): # This example requires a servo on X1 and a signal (from a radio) on X4. micropython.alloc_emergency_exception_buf(100) in_ppm = Ppm(pyb.Pin(3)) while True: # wait forever pyb.delay(200) print("%d%% %d deg %d speed (%s) %d us" % (in_ppm.percent(), in_ppm.angle(), in_ppm.speed(), "True" if in_ppm.switch() else "False", in_ppm.pulse_width))
def hal_write_command(self, cmd): """Writes a command to the LCD. Data is latched on the falling edge of E. """ byte = ((self.backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA)) self.i2c.send(byte | MASK_E, self.i2c_addr) self.i2c.send(byte, self.i2c_addr) byte = ((self.backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA)) self.i2c.send( byte | MASK_E, self.i2c_addr, ) self.i2c.send(byte, self.i2c_addr) if cmd <= 3: # The home and clear commands require a worst # case delay of 4.1 msec delay(5)
def __init__(self, i2c, i2c_addr, num_lines, num_columns): self.i2c = i2c self.i2c_addr = i2c_addr self.i2c.send(0, self.i2c_addr) delay(20) # Allow LCD time to powerup # Send reset 3 times self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) delay(5) # need to delay at least 4.1 msec self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) delay(1) self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) delay(1) # Put LCD into 4 bit mode self.hal_write_init_nibble(self.LCD_FUNCTION) delay(1) LcdApi.__init__(self, num_lines, num_columns) cmd = self.LCD_FUNCTION if num_lines > 1: cmd |= self.LCD_FUNCTION_2LINES self.hal_write_command(cmd)
def GMP102_init(i2c_bus): calib_data = bytearray(GMP102_CALIBRATION_REGISTER_COUNT) fCalibParam = [0] * GMP102_CALIBRATION_PARAMETER_COUNT i2c_bus.send(bytearray([GMP102_REG_RESET, SW_RST_SET_VALUE]), GMP102_ADDR) delay(100) # Wait 100ms for reset complete # GMP102 get the pressure calibration parameters i2c_bus.send(GMP102_REG_CALIB00, GMP102_ADDR) i2c_bus.recv(calib_data, GMP102_ADDR) # print("".join("\\x%02x" % i for i in calib_data)) # GMP102 get the pressure calibration parameters for i in range(0, 9): fCalibParam[i] = ( (struct.unpack('>h', calib_data[i * 2:i * 2 + 2]))[0] / 4) * math.pow( 10, calib_data[(i * 2) + 1] & 3) * GMP102_CALIB_SCALE_FACTOR[i] i2c_bus.send(bytearray([GMP102_REG_CALIB00, 0x00, 0x00, 0x00, 0x00]), GMP102_ADDR) i2c_bus.send(bytearray([GMP102_REG_CONFIG2, 0x18 | OSR_8192]), GMP102_ADDR) return fCalibParam
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()
import tm1637,math from machine import Pin,delay from machine import USonic sonic=USonic(0) tm = tm1637.TM1637(clk=Pin.board.P0, dio=Pin.board.P1) while True: dis = math.floor(sonic.distance()) #取整數 tm.number(dis) delay(200)
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()
from machine import Servo, delay s1 = Servo(1) # serve channel 1 is PWM0 , 2 is PWM1....4 is PWM3 s2 = Servo(2) s3 = Servo(3) s4 = Servo(4) s_all = Servo(Servo.ALL_SERVO) s1.calibration(1500, 7000, 1600, 4100, 3000) #(min,max,angle(0),angle(90),xxxx) s2.calibration(1500, 7000, 1600, 4100, 3000) s3.calibration(1500, 7000, 1600, 4100, 3000) s4.calibration(1500, 7000, 1600, 4100, 3000) s1.angle(90) # change to angle 90 delay(1000) #wait servo s1.angle(180, 1000) # change to angle 180 use 1000ms delay(1000) s1.angle(0, 1000) # change to angle 0 use 1000ms delay(1000) s1.angle(180, 1000) # change to angle 180 use 1000ms delay(1000) s1.angle(90) for i in range(1, 10): s_all.angle([180, 180, 180, 180, 2000]) # [s1 angle,s2 angle, s3 angle,s4 angle , active time] delay(2000) s_all.angle([0, 0, 0, 0, 5000]) delay(2000)
def sleep_ms(self, mseconds): try: time.sleep_ms(mseconds) except AttributeError: machine.delay(mseconds)
import ssd1306 from machine import I2C, delay, RTC i2c = I2C(0, I2C.MASTER, baudrate=400000) disp = ssd1306.SSD1306_I2C(128, 64, i2c) today = (2021, 3, 16, 3, 9, 0, 0, 0) rtc = RTC() rtc.datetime(today) while True: disp.fill(0) time = rtc.datetime() disp.text('Date:{yy}/{mm}/{dd}'.format(yy=time[0], mm=time[1], dd=time[2]), 0, 16, 1) disp.text('time:{hh}:{mm}:{ss}'.format(hh=time[4], mm=time[5], ss=time[6]), 0, 36, 1) disp.show() delay(900) #clr screen disp.fill(0) disp.show() disp.pixel(0, 0, 1) disp.text('SSD1306', 0, 8, 1) disp.invert(1) disp.invert(0)
import ssd1306 from machine import I2C, delay import math x = xh = None y = yh = None i2c = I2C(0, I2C.MASTER, baudrate=400000) oled = ssd1306.SSD1306_I2C(128, 64, i2c) oled.fill(0) oled.pixel(64, 32, 1) for _E8_A7_92_E5_BA_A6 in range(361): x = 30 * math.cos(_E8_A7_92_E5_BA_A6 / 180.0 * math.pi) y = 30 * math.sin(_E8_A7_92_E5_BA_A6 / 180.0 * math.pi) oled.pixel(int(64 - x), int(32 - y), 1) for i in range(0, 361, 30): xh = 12 * math.cos(i / 180.0 * math.pi) yh = 12 * math.sin(i / 180.0 * math.pi) for j in range(0, 361, 10): x = 18 * math.cos(j / 180.0 * math.pi) y = 18 * math.sin(j / 180.0 * math.pi) oled.line(64, 32, int(64 - x), int(32 - y), 1) oled.line(64, 32, int(64 - xh), int(32 - yh), 1) oled.show() delay(10) oled.line(64, 32, int(64 - x), int(32 - y), 0) oled.show() oled.line(64, 32, int(64 - xh), int(32 - yh), 0) oled.show()
import ssd1306 from machine import I2C,delay import math _E8_A7_92_E5_BA_A6 = None x = None y = None i2c = I2C(0,I2C.MASTER) oled=ssd1306.SSD1306_I2C(128, 64, i2c) oled.fill(0) #清除畫布 oled.pixel(64,32,1) # 圓心 (64,32)一點 ''' 使用三角函數劃出一個圓''' for i range(361): x = 30 * math.cos(i / 180.0 * math.pi) y = 30 * math.sin(i / 180.0 * math.pi) oled.pixel(int(64-x),int(32-y),1) # show 秒針,每秒計算6度 , 一圈 360/6 = 60秒 for j in range (0,361,6) : x = 18 * math.cos(j/ 180.0 * math.pi) y = 18 * math.sin(j/ 180.0 * math.pi) oled.line(64,32,int(64-x),int(32-y),1) #畫秒針在畫布上 oled.show() #顯示 delay(1000) #等1 sec oled.line(64,32,int(64-x),int(32-y),0) # 清除秒針在畫布上 oled.show()
from machine import Pin, ADC, delay, Servo Ax = ADC(Pin.epy.AIN2) Ay = ADC(Pin.epy.AIN1) SW = ADC(Pin.epy.AIN0) s1 = Servo(3) #PWM2 s2 = Servo(4) #PWM3 while True: print('XVR = {} , YVR = {} , SW = {}'.format(Ax.read(), Ay.read(), SW.read())) s1.angle(int((180 / 4096) * Ax.read())) s2.angle(int((180 / 4096) * Ay.read())) delay(50)
from machine import LED, delay, Pin p0 = Pin(Pin.board.P0, Pin.OUT) # P0 Output GPIO p1 = Pin(Pin.board.P1, Pin.IN) #P1 Input GPIO while True: p0.value(1) # Output High print('P0 output High') print('P1 input is {}'.format(p1.value())) delay(500) p0.value(0) # Output Low print('P0 output Low') print('P1 input is {}'.format(p1.value())) delay(500)
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)