Пример #1
0
import time
import board
from adafruit_hcsr04 import HCSR04
import Adafruit_BBIO.GPIO as GPIO
import display
import threading


BUTTON0 = "P2_3"

GPIO.setup(BUTTON0, GPIO.IN)


#HCSR04(trigger pin, echo pin)
sonar0 = HCSR04(board.P2_2, board.P2_4)
sonar1 = HCSR04(board.P2_6, board.P2_8)

# ------------------------------------------------------------------------
# Global variables
# ------------------------------------------------------------------------

left_sonar = 0
right_sonar = 0
passed = True
#Value that is the max distance for the device.
running_average = 0
distance_buffer = 10
reset_distance = 3

#Initializing display values
Пример #2
0
import neopixel
import time
from digitalio import DigitalInOut, Direction, Pull
'''from lcd.lcd import LCD
from lcd.i2c_pcf8574_interface import I2CPCF8574Interface

from lcd.lcd import CursorMode'''

# importing the sensor library
from adafruit_hcsr04 import HCSR04

#declaring dot and sensor (not lcd apparently because there isn't enough memory. Apparently.)

dot = neopixel.NeoPixel(board.NEOPIXEL, 1)
# lcd = LCD(I2CPCF8574Interface(0x27), num_rows=2, num_cols=16)
sensor = HCSR04(board.D6, board.D5)

# not useless variables this time for r, g, b values for neopixel
r = 255
g = 0
b = 0

# distance variable
dist = 0

while True:
    #filling the neopixel with r, g, b variables
    dot.fill((r, g, b))

    # COLOR CODE
Пример #3
0
# setup imports
import time, board, pulseio, busio, adafruit_gps, adafruit_lsm9ds1
from adafruit_motor import servo
from adafruit_hcsr04 import HCSR04

# other imports
import math as m

# ==========================================================================
# -------------------------------- INIT ------------------------------------
# ==========================================================================

# ------------ set up hcsr04 ultrasonic sensor --------------
sonar = HCSR04(trig, echo)
try:
    while True:
    print(sonar.dist_cm())
    sleep(2)
except KeyboardInterrupt:
    pass
sonar.deinit()

# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)

#SPI connection:
# from digitalio import DigitalInOut, Direction
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# csag = DigitalInOut(board.D5)
Пример #4
0
"""
FILE: main.py
DESC: Working example of Trinket M0 PING sensor
"""

import board
import busio
from time import sleep
from adafruit_hcsr04 import HCSR04

threshold = 10.0
delay = 0.59

uart = busio.UART(board.TX, board.RX, baudrate=9600)

with HCSR04(board.D2, board.D1) as sonar:
    while True:
        try:
            print(sonar.distance)
            if(sonar.distance < threshold):
                uart.write("X")
            else:
                uart.write("*")
            sleep(delay)
        except Exception:
            pass


Пример #5
0
 def __init__(self, trigger_pin, echo_pin):
     super().__init__()
     self._sensor = HCSR04(trigger_pin, echo_pin)
Пример #6
0
import pulseio
import servo

echo = board.A2
trig = board.A0

# create a PWMOut object on Pin A2 
right = pulseio.PWMOut(board.D10, duty_cycle=2 ** 15, frequency=50) 
left = pulseio.PWMOut(board.D9, duty_cycle=2 ** 15, frequency=50)

LR = servo.Servo(right)
LL = servo.Servo(left)

#sonar = HCSR04(trig, echo)

with HCSR04(trig, echo) as sonar:
    while True:
        dist = sonar.distance
        if (dist > 10):
        # Shuffle right foot
            for angle in range(90, 45, -3):  
                LR.angle = angle
                time.sleep(0.05)
            for angle in range(45, 90, 3):
                LR.angle = angle
                time.sleep(0.05)
        else:
            # Shuffle right foot
            for angle in range(90, 45, -3):  
                LR.angle = angle
                time.sleep(0.05)
Пример #7
0
 def __init__(self, trigger_pin, receive_pin, name='unknown'):
     self.__sensor = HCSR04(trigger_pin, receive_pin, timeout=2)
     self.__last_distance = 0
     self.__name = name
Пример #8
0
#Imports
from adafruit_circuitplayground.express import cpx
from adafruit_hcsr04 import HCSR04
import board
import math
import time
import digitalio
from time import sleep

#Assigning variables
sonar = HCSR04(trigger_pin=board.A7, echo_pin=board.A6)
ledLeft = digitalio.DigitalInOut(board.A1)
ledLeft.direction = digitalio.Direction.OUTPUT
ledRight = digitalio.DigitalInOut(board.A3)
ledRight.direction = digitalio.Direction.OUTPUT
cpx.pixels.brightness = 0.3

#Indefinite loop
while True:
    try:

        #Retrieving values from accelerometer
        x_float, y_float, z_float = cpx.acceleration

        #If the board flexes left, then turn on left LED
        if (x_float < -.5):
            ledLeft.value = True
            ledRight.value = False

        #If the board flexes right, then turn on right LED
        if (x_float > 0.5):