Last active
November 16, 2022 15:36
-
-
Save roxberry/b346a4df5ec806afbe832092a0115bb6 to your computer and use it in GitHub Desktop.
MakerPi Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, UART | |
from time import sleep | |
import utime | |
import uos | |
# Baud rate setting for bluetooth modules | |
# AT+BAUD<number> | |
# 1 set to 1200bps | |
# 2 set to 2400bps | |
# 3 set to 4800bps | |
# 4 set to 9600bps (Default) | |
# 5 set to 19200bps | |
# 6 set to 38400bps | |
# 7 set to 57600bps | |
# 8 set to 115200bps | |
def help(): | |
print("AT commands (prefix 0, 1, or 2 to write to port 0, 1, or both") | |
print("-"*50) | |
print("AT - Attention, should return 'OK'") | |
print("AT+HELP - Returns valid AT commands for device") | |
print("AT+VERSION - returns version number") | |
print("AT+NAME - returns current device name") | |
print("AT+NAME<name> - sets the name of the device") | |
print("AT+PIN<pin> - will set the pin e.g. 'AT+PIN4321 will set the pin to 4321") | |
def readUartBytes(uart, delay=1000): | |
print("reading data") | |
prvMills = utime.ticks_ms() | |
resp = b"" | |
while (utime.ticks_ms()-prvMills)<delay: | |
if uart.any(): | |
resp = b"".join([resp, uart.read(1)]) | |
print(resp.decode()) | |
def readUart(uart, delay): | |
response = "" | |
print("reading data") | |
while uart.any() > 0: | |
response = uart.readline() | |
print(str(response)) | |
sleep(delay) | |
# Create the UARTs | |
uart0 = UART(i0, baudrate=115200, tx=Pin(0), rx=Pin(1)) | |
uart1 = UART(i1, baudrate=9600, tx=Pin(4), rx=Pin(5)) | |
# Print a command line | |
print("-"*50) | |
print("PicoTerm") | |
print(uos.uname()) | |
print("type 'quit' to exit, read<n> to read from UART<n>, or 'help' for AT commands") | |
# Loop | |
command = "" | |
while True and command != 'quit': | |
command = input("PicoTerm>") | |
if command == 'help': | |
help() | |
elif command == 'read0': | |
print("reading bytes from UART0") | |
readUartBytes(uart=uart0) | |
elif command == 'read1': | |
print("reading bytes from UART1") | |
readUartBytes(uart=uart1) | |
elif command == 'quit': | |
print("-"*50) | |
print("Bye") | |
elif command[0:1] == '0' or command[0:1] == '1' or command[0:1] == '2': | |
print("Command sent:",command[1:]) | |
uart = command[0:1] | |
commandNL = command[1:]+'\r'+'\n' | |
if uart != '1': | |
print("writing to UART0") | |
uart0.write(commandNL.encode('utf-8')) | |
delay = 2500 | |
if command[1:] == "AT+HELP": | |
delay = 7000 | |
readUartBytes(uart0, delay) | |
# readUart(uart0, .25) | |
if uart != '0': | |
print("writing to UART1") | |
uart1.write(commandNL.encode('utf-8')) | |
delay = 5000 | |
if command[1:] == "AT+HELP": | |
delay = 10000 | |
readUartBytes(uart1, delay) | |
# readUart(uart1, .5) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, PWM, UART | |
import utime | |
import time | |
from neopixel import NeoPixel | |
NUMBER_PIXELS = 2 | |
LED_PIN = 18 | |
#Define the pin as output | |
pin = Pin(LED_PIN, Pin.OUT) | |
# Define the NeoPixel Strip | |
strip = NeoPixel(pin, NUMBER_PIXELS) | |
# Color RGB values | |
red = (255, 0, 0) | |
yellow = (255, 150, 0) | |
green = (0, 255, 0) | |
black = (0, 0, 0) | |
startColors = (red, black, red, black, yellow, black, yellow, black, green, black, green, black) | |
def showColor(color): | |
for i in range(NUMBER_PIXELS): | |
strip[i] = color | |
strip.write() | |
# Using Grove 5 Connector | |
TRIGGER_PIN = 6 # White Wire | |
ECHO_PIN = 26 # Yellow Wire | |
# Init HC-SR04P pins | |
trigger = Pin(TRIGGER_PIN, Pin.OUT) # send trigger out to sensor | |
echo = Pin(ECHO_PIN, Pin.IN) # get the delay interval back | |
def ping(): | |
trigger.low() | |
utime.sleep_us(2) # Wait 2 microseconds low | |
trigger.high() | |
utime.sleep_us(5) # Stay high for 5 microseconds | |
trigger.low() | |
while echo.value() == 0: | |
signaloff = utime.ticks_us() | |
while echo.value() == 1: | |
signalon = utime.ticks_us() | |
timepassed = signalon - signaloff | |
distance = (timepassed * 0.0343) / 2 | |
return distance * .254 | |
BUZZER_PIN = 22 | |
buzzer = PWM(Pin(BUZZER_PIN)) | |
def playTone(): | |
buzzer.duty_u16(1000) | |
buzzer.freq(150) | |
def stopTone(): | |
buzzer.duty_u16(0) | |
#define the UART | |
uart0 = UART(id=1, baudrate=9600, tx=Pin(4), rx=Pin(5)) | |
# Get the data from the UART | |
def readUartBytes(uart): | |
resp = b"" | |
while uart.any(): | |
resp = b"".join([resp, uart.read(1)]) | |
return resp.decode() | |
# Motor definitions | |
FULL_POWER_LEVEL = 65024 | |
RIGHT_FORWARD_PIN = 11 | |
RIGHT_REVERSE_PIN = 10 | |
LEFT_FORWARD_PIN = 9 | |
LEFT_REVERSE_PIN = 8 | |
right_forward = PWM(Pin(RIGHT_FORWARD_PIN)) | |
right_reverse = PWM(Pin(RIGHT_REVERSE_PIN)) | |
left_forward = PWM(Pin(LEFT_FORWARD_PIN)) | |
left_reverse = PWM(Pin(LEFT_REVERSE_PIN)) | |
def forward(): | |
right_reverse.duty_u16(0) | |
left_reverse.duty_u16(0) | |
right_forward.duty_u16(FULL_POWER_LEVEL) | |
left_forward.duty_u16(FULL_POWER_LEVEL) | |
def forwardSlow(): | |
right_reverse.duty_u16(0) | |
left_reverse.duty_u16(0) | |
right_forward.duty_u16(FULL_POWER_LEVEL // 2) | |
left_forward.duty_u16(FULL_POWER_LEVEL // 2) | |
def reverse(): | |
right_forward.duty_u16(0) | |
left_forward.duty_u16(0) | |
right_reverse.duty_u16(FULL_POWER_LEVEL) | |
left_reverse.duty_u16(FULL_POWER_LEVEL) | |
def reverseSlow(): | |
right_forward.duty_u16(0) | |
left_forward.duty_u16(0) | |
right_reverse.duty_u16(FULL_POWER_LEVEL // 2) | |
left_reverse.duty_u16(FULL_POWER_LEVEL // 2) | |
def left(): | |
left_forward.duty_u16(0) | |
right_reverse.duty_u16(0) | |
left_reverse.duty_u16(FULL_POWER_LEVEL // 2) | |
right_forward.duty_u16(FULL_POWER_LEVEL // 2) | |
def right(): | |
right_forward.duty_u16(0) | |
left_reverse.duty_u16(0) | |
right_reverse.duty_u16(FULL_POWER_LEVEL // 2) | |
left_forward.duty_u16(FULL_POWER_LEVEL // 2) | |
def stop(): | |
right_forward.duty_u16(0) | |
right_reverse.duty_u16(0) | |
left_forward.duty_u16(0) | |
left_reverse.duty_u16(0) | |
def reverseAndTurn(): | |
utime.sleep(.5) | |
reverseSlow() | |
utime.sleep(1.5) | |
left_reverse.duty_u16(0) | |
left_forward.duty_u16(FULL_POWER_LEVEL // 2) | |
utime.sleep(.75) | |
stop() | |
utime.sleep(.25) | |
stopTone() | |
auto = False; | |
autoPin = Pin(20, Pin.IN) | |
leftPin = Pin(7, Pin.IN) | |
rightPin = Pin(0, Pin.IN) | |
for i in range(20): | |
if not autoPin.value(): | |
auto = True | |
utime.sleep(.25) | |
for color in startColors: | |
showColor(color) | |
utime.sleep(.5) | |
while auto: | |
if leftPin.value() and rightPin.value(): | |
stop() | |
elif leftPin.value(): | |
left() | |
elif rightPin.value(): | |
right() | |
else: | |
forwardSlow() | |
forward() # Assume the way ahead is clear | |
color = green | |
while True: | |
dir = readUartBytes(uart0) # read from the UART | |
if dir == 'h': | |
stop() | |
color = red | |
elif dir == 'f': | |
forward() | |
color = green | |
elif dir == 'l': | |
left() | |
color = green | |
elif dir == 'r': | |
right() | |
color = green | |
distance = ping() # Check the distance | |
if distance < 5 or dir == 's': # Obstruction ahead, slow down | |
forwardSlow() | |
color = yellow | |
if distance < 2.5: # Obstruction too close, stop, and play tone | |
stop() | |
color = red | |
playTone() | |
showColor(color) | |
if distance < 2.5: # If we were too close, back up and turn | |
reverseAndTurn() | |
forward() | |
color = green |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin | |
import utime | |
import time | |
# Using Grove 5 Connector | |
TRIGGER_PIN = 6 # White Wire | |
ECHO_PIN = 26 # Yellow Wire | |
# Init HC-SR04P pins | |
trigger = Pin(TRIGGER_PIN, Pin.OUT) # send trigger out to sensor | |
echo = Pin(ECHO_PIN, Pin.IN) # get the delay interval back | |
def ping(): | |
trigger.low() | |
utime.sleep_us(2) # Wait 2 microseconds low | |
trigger.high() | |
utime.sleep_us(5) # Stay high for 5 microseconds | |
trigger.low() | |
while echo.value() == 0: | |
signaloff = utime.ticks_us() | |
while echo.value() == 1: | |
signalon = utime.ticks_us() | |
timepassed = signalon - signaloff | |
distance = (timepassed * 0.0343) / 2 | |
return distance * .254 | |
while True: | |
print("Distance:", ping(), "inches") | |
utime.sleep(.25) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment