This repository has been archived on 2024-03-22. You can view files and clone it, but cannot push or open issues or pull requests.

59 lines
1.2 KiB
Python

import time
from machine import Pin, SPI
from machine import I2C
import VL53L0X
from ssd1306 import Ssd1306Spi
onboard_led = Pin(25, Pin.OUT)
# OLED
oled_spi = SPI(0, 1000000, mosi=Pin(19), sck=Pin(18))
oled = Ssd1306Spi(width=128, height=64, spi=oled_spi, dc=Pin(21), res=Pin(20), cs=None)
# бут лого
oled.fill(0)
oled.text("Lazer", 0, 0)
oled.text("rangefinder", 0, 15)
oled.text("By bigboy_2010", 0, 52)
oled.show()
time.sleep_ms(2000)
i2c = I2C(0, sda=Pin(16), scl=Pin(17))
# Create a VL53L0X object
tof = VL53L0X.VL53L0X(i2c)
tof.set_Vcsel_pulse_period(tof.vcsel_period_type[0], 18)
tof.set_Vcsel_pulse_period(tof.vcsel_period_type[1], 14)
while True:
# Start ranging
ms = []
for _ in range(10):
tof.start()
distance = tof.read()
tof.stop()
ms.append(distance)
distance = 0
for m in ms:
distance += m
distance = int(distance / 10) / 10 # сразу получаем сантиметры
if distance >= 200:
distance = '<overflow>'
if onboard_led.value() != 0:
onboard_led.off()
else:
onboard_led.on()
oled.fill(0)
oled.text("Distance:", 0, 0)
oled.text(f"{distance} cm", 0, 25)
oled.show()
# time.sleep_ms(100)