|
| 1 | +# SPDX-FileCopyrightText: 2024 John Park for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +''' |
| 5 | +Air Blaster |
| 6 | +Feather RP2040 Prop-Maker with Power Relay FeatherWing and VL53L1X distance sensor |
| 7 | +''' |
| 8 | + |
| 9 | +import time |
| 10 | +import board |
| 11 | +import digitalio |
| 12 | +import adafruit_vl53l1x |
| 13 | + |
| 14 | +TRIGGER_DISTANCE = 50.0 |
| 15 | +triggered = False |
| 16 | + |
| 17 | +i2c = board.STEMMA_I2C() |
| 18 | +vl53 = adafruit_vl53l1x.VL53L1X(i2c) |
| 19 | +vl53.distance_mode = 2 |
| 20 | +vl53.timing_budget = 100 |
| 21 | + |
| 22 | +print("VL53L1X Simple Test.") |
| 23 | +print("--------------------") |
| 24 | +model_id, module_type, mask_rev = vl53.model_info |
| 25 | +print("Model ID: 0x{:0X}".format(model_id)) |
| 26 | +print("Module Type: 0x{:0X}".format(module_type)) |
| 27 | +print("Mask Revision: 0x{:0X}".format(mask_rev)) |
| 28 | +print("Distance Mode: ", end="") |
| 29 | +if vl53.distance_mode == 1: |
| 30 | + print("SHORT") |
| 31 | +elif vl53.distance_mode == 2: |
| 32 | + print("LONG") |
| 33 | +else: |
| 34 | + print("UNKNOWN") |
| 35 | +print("Timing Budget: {}".format(vl53.timing_budget)) |
| 36 | +print("--------------------") |
| 37 | + |
| 38 | +vl53.start_ranging() |
| 39 | + |
| 40 | +relay_pin = digitalio.DigitalInOut(board.D10) |
| 41 | +relay_pin.direction = digitalio.Direction.OUTPUT |
| 42 | +relay_pin.value = False |
| 43 | + |
| 44 | +def blast(repeat, duration, rate): |
| 45 | + for _ in range(repeat): |
| 46 | + relay_pin.value = True |
| 47 | + print("bang") |
| 48 | + time.sleep(duration) |
| 49 | + relay_pin.value = False |
| 50 | + time.sleep(rate) |
| 51 | + |
| 52 | +distance = None |
| 53 | + |
| 54 | +while True: |
| 55 | + if vl53.data_ready: |
| 56 | + distance = vl53.distance |
| 57 | + print("Distance: {} cm".format(vl53.distance)) |
| 58 | + vl53.clear_interrupt() |
| 59 | + time.sleep(0.1) |
| 60 | + |
| 61 | + if distance: |
| 62 | + if distance <= TRIGGER_DISTANCE: |
| 63 | + if not triggered : |
| 64 | + blast(3, 0.01, 0.1) # adjust repeat, duration, rate here |
| 65 | + time.sleep(0.4) |
| 66 | + blast(2, 0.01, 0.2) # adjust repeat, duration, rate here |
| 67 | + triggered = True |
| 68 | + |
| 69 | + else: |
| 70 | + triggered = False |
0 commit comments