I want the camera to find for example blue but only if there is 1 send on UART from arduino and when is 0 then yellow also via UART from arduino but the code i have is not working because the camera after 2 seconds just stops.
Any suggestions.
Thank you.
Here is the code:
import sensor, image, time
import math
from pyb import UART
# Set the camera resolution
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
# Define the color thresholds for blue and yellow
blue_threshold = (0, 100, -128, 127, -128, 127) # Modify these values as needed
yellow_threshold = (-20, 50, 15, 85, -105, -30) # Modify these values as needed
# Initialize UART
uart = UART(3, 9600) # Assuming UART3 and baudrate of 9600
# Function to read UART without blocking
def read_uart():
if uart.any():
return uart.readchar()
return None
# Main loop
while True:
# Read the signal from Arduino
signal = read_uart()
if signal is not None:
if signal == b'1': # If Arduino sends '1', find blue
img = sensor.snapshot()
# Find blue objects
blue_blobs = img.find_blobs([blue_threshold])
if blue_blobs:
blue_blob = max(blue_blobs, key=lambda b: b.pixels())
blue_x, blue_y, blue_width, blue_height = blue_blob.rect()
# Calculate the center of the blue object
blue_center_x = blue_x + blue_width // 2
blue_center_y = blue_y + blue_height // 2
# Draw a rectangle around the blue object
img.draw_rectangle(blue_blob.rect(), color=(255, 0, 0))
# Draw a cross at the center of the blue object
img.draw_cross(blue_center_x, blue_center_y, color=(0, 255, 0))
print("Blue Object Center - X:", blue_center_x, " Y:", blue_center_y)
elif signal == b'0': # If Arduino sends '0', find yellow
img = sensor.snapshot()
# Find yellow objects
yellow_blobs = img.find_blobs([yellow_threshold])
if yellow_blobs:
yellow_blob = max(yellow_blobs, key=lambda b: b.pixels())
yellow_x, yellow_y, yellow_width, yellow_height = yellow_blob.rect()
# Calculate the center of the yellow object
yellow_center_x = yellow_x + yellow_width // 2
yellow_center_y = yellow_y + yellow_height // 2
# Draw a rectangle around the yellow object
img.draw_rectangle(yellow_blob.rect(), color=(255, 0, 0))
# Draw a cross at the center of the yellow object
img.draw_cross(yellow_center_x, yellow_center_y, color=(0, 255, 0))
print("Yellow Object Center - X:", yellow_center_x, " Y:", yellow_center_y)
else:
print("Invalid signal received from Arduino")
# Main camera processing loop can go here
# Add your camera processing logic here