示例#1
0
import network
import ESP8266WebServer  # 匯入網站模組
import wemotor
from machine import I2C, Pin

motor = wemotor.Motor()


# 左轉
def left():
    motor.move(0, 50)


# 右轉
def right():
    motor.move(50, 0)


# 處理 /Race 指令的函式
def handleCmd(socket, args):
    # 檢查是否有 output 參數
    if 'output' in args:
        if args['output'] == 'L':  # 若 output 參數值為 'L'
            print("左轉")
            left()  # 左轉
        elif args['output'] == 'R':  # 若 output 參數值為 'R'
            print("右轉")
            right()  # 右轉
        # 回應 OK 給瀏覽器
        ESP8266WebServer.ok(socket, "200", "OK")
    else:
示例#2
0
from machine import I2C, Pin
from hcsr04 import HCSR04
import wemotor
import time

dir = 0  # 記錄行進狀態,0代表「前進」,1代表「右轉」。
limitDist = 10  # 距離上限10公分
sr04 = HCSR04()  # 建立超音波物件

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
motorA = wemotor.Motor(wemotor.A, i2c)  # 建立馬達A物件
motorB = wemotor.Motor(wemotor.B, i2c)  # 建立馬達B物件


def stop():  # 停止
    motorA.setMotor(wemotor.STOP)
    motorB.setMotor(wemotor.STOP)


def forward():  # 前進
    motorA.setMotor(wemotor.CW)
    motorB.setMotor(wemotor.CW)


def turnRight():  # 右轉
    motorA.setMotor(wemotor.CW)
    motorB.setMotor(wemotor.CCW)


while True:
    try: