小车控制
LED灯控制¶
方法
led灯亮
led.open()
led灯灭
led.close()
led灯切换状态
led.toggle()
实现
控制LED灯亮灭交替
from genki import led
import ohos
if __name__ == '__main__':
while True:
led.close()
ohos.sleep(0.3)
led.open()
ohos.sleep(0.3)
控制LED灯切换状态
from genki import led
import ohos
if __name__ == '__main__':
while True:
led.toggle()
ohos.sleep(0.5)
呼吸灯
from genki import led
import ohos
if __name__ == '__main__':
while True:
for i in range(50):
led.pwm_start(i, 10000)
ohos.sleep(0.05)
for i in range(50, 0, -1):
led.pwm_start(i, 10000)
ohos.sleep(0.05)
马达转动控制¶
方法
马达转动
mator.run(50,50)
实现
马达转动方法
motor.run(50, 50)
控制马达转动
from genki.car import motor
import ohos
if __name__ == '__main__':
motor.run(50, 50)
注意:run中传递左右轮子的pwm占空比百分值
红外巡线传感器¶
方法
巡线传感器是否可用
linetrack.isEnable()
巡线传感器数据
linetrack.getState()
实现
from genki.car import linetrack
import ohos
if __name__ == '__main__':
enable = linetrack.isEnable()
print(enable)
while enable:
state = linetrack.getState()
print(state)
print("{}{}{}{} {}{}{}".format((state >> 0) & 0x01,
(state >> 1) & 0x01,
(state >> 2) & 0x01,
(state >> 3) & 0x01,
(state >> 4) & 0x01,
(state >> 5) & 0x01,
(state >> 6) & 0x01))
ohos.sleep(1)
超声波传感器¶
方法
超声波传感器是否可用
ultrasound.isEnable()
巡线传感器数据
ultrasound.getDistance()
实现
from genki.car import ultrasound
import ohos
if __name__ == '__main__':
enable = ultrasound.isEnable()
print(enable)
while enable:
distance = ultrasound.getDistance()
print(distance)
ohos.sleep(0.5)
OLED屏幕¶
方法
OLED屏幕是否可用
oled.isEnable()
清理屏幕显示
oled.clear()
展示文本
oled.showText('hello',(10,10,1))
实现
from genki.car import oled
import ohos
def main():
enable = oled.isEnable()
if not enable:
return
oled.clear()
oled.showText('hello',(10,10,1))
ohos.sleep(2.0)
oled.clear()
oled.showText('world', (10, 10, 1))
if __name__ == '__main__':
main()
线程¶
方法
ohos.run(方法名, 参数)
实现
import ohos
def my_task(arg):
while True:
print('1')
ohos.sleep(0.5)
if __name__ == '__main__':
ohos.run(my_task, 10)
while True:
print('2')
ohos.sleep(1)