Trilobot control program

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import pygame
from trilobot import Trilobot, BUTTON_A
import os #so can shutdown
import time

import datetime

from datetime import datetime
from picamera import PiCamera

from trilobot import Trilobot
tbot = Trilobot()

camera = PiCamera()
camera.resolution = (1280, 720)
camera.framerate = 25[date=2022-02-09 timezone=“Europe/London”]
time.sleep(2)
camera.start_preview(alpha=100) ## alpha 0 to 255

record = False
##define colours for lights
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

Need to open screen to allow it to detect user events

screen = pygame.display.set_mode([240, 160])

tbot.fill_underlighting(RED)
time.sleep(2)
tbot.fill_underlighting(GREEN)
time.sleep(2)
tbot.fill_underlighting(BLUE)
time.sleep(2)
tbot.fill_underlighting(255,255,255)

try:

while not tbot.read_button(BUTTON_A):
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                if record == True:
                    camera.stop_recording        
               
                tbot.clear_underlighting()   
                os.system ('sudo shutdown now')
                
            elif event.key == pygame.K_r:
                if record == False:
                    record = True
                    moment = datetime.now()


                    camera.start_recording('/home/pi/Videos/vid_%02d_%02d_%02d.h264' % (moment.hour, moment.minute, moment.second))                        
                    
            elif event.key == pygame.K_t:
                if record ==True:
                    record = False
                    camera.stop_recording()
            
            elif event.key == pygame.K_RIGHT:
                tbot.curve_forward_right()

            elif event.key == pygame.K_LEFT:
                tbot.curve_forward_left()

            elif event.key == pygame.K_UP:
                

                
                distance = tbot.read_distance()    
                   
                if distance < 30:

                    tbot.stop()
                    
                    
                else:
                    while event.key == pygame.K_UP:     
                        tbot.forward()
                        time.sleep(0.2)
                    
                        distance = tbot.read_distance()
                        if distance < 30:
                            print(distance)
                                  
                            break

            
            
            elif event.key == pygame.K_DOWN:
                tbot.backward()

            elif event.key == pygame.K_q:
                if record == True:
                    record = False
                    camera.stop_recording()                    
                camera.stop_preview                 
                pygame.quit()

        elif event.type == pygame.KEYUP:
            tbot.stop()  

else:   
    if record ==True:
        record = False
        camera.stop_recording()
    tbot.clear_underlighting()
    GPIO.cleanup
    os.system ('sudo shutdown now')           

finally:
if record == True:
camera.stop_recording
camera.stop_preview
tbot.clear_underlighting()
GPIO.cleanup

Above is code I have put together.
It works but not as intended.

I intended to add code to avoid collisions when going forward (up arrow press)
The above code does this.

However when going forward the motor continues when the up arrow is released.
other directions stop motion when direction key is released

I have tried various code beyond the break to try and ensure that key up is recognised and stops forward motion
none have worked as intended

I would be grateful for some help.

I guess it involves regaining key surveillance in the while event key… loop at the same time maintaining distance surveillance and response?

With respect to Controller for Trilobot Q
I have found the Riii 8 S works fine
Also INIU BI-B61 10000mA power bank is ok (a little heavy but OK)

I have no doubt that in all this time you’ve found the source of your problem here – that your
" ```
while event.key == pygame.K_UP:

kept testing the same event value, with no intervening update, i.e., another event.get to recognize that you released the button.

But I was wondering what else you've been able to develop, and if you'd be willing to share.  I've just gotten started...and I don't really know where to start. ;-)