Bluetooth Key events

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
FifthCloud
 
Posts: 22
Joined: Sat May 08, 2021 9:45 am

Bluetooth Key events

Post by FifthCloud »

Hello,

Have created a simple remote control car app using Raspberry Pi Pico, H-Bridge (for the motors), and the Adafruit Bluefruit LE UART Friend.

Everything is connected and working fine for the most part. What I need help on is the following...
1. My phone is able to connect to the bluefruit and send it commands. I am unable to figure out how I can press and hold a direction and for the car to continue in that direction.
For example, in the app within the controller section there are 4 directions with numbers. If I were to press and hold the right direction how can I send to the pico that right has been pressed and know when right direction has been let go? Key up and key down events, does the Bluetooth handle that? Or is the app limited on what it can send? This is what I don't understand because I am new to bluetooth.


2. If key events are not possible there are times if I were to spam hitting a particular direction the messages will begin sending back strange messages and I am having difficulty flushing message buffer or clearing the message buffer. Any suggestions?

Here are some examples of the right-direction strange messages that I may get. uart.read will respond with the following messages...
  • b'!B813!B804
    !B813!B80b'
    B813!B804b'!
    813!B804b'!B
    13!B804b'!B8
    3!B804b'!B81
I don't want this message to be offset would like if I could flush this type of behavior out and start fresh. The only way I can "fix" this is by a power cycle (off/on).

Within my while true I have the following code...

Code: Select all

    command = str(uart.read(10))
    if(command == "b'!B714!B705'"): # Left - direction has been pressed
        print("Left was pressed!")
        multi_dir_speed(speed, 1, speed, 0, 0.4)
    elif(command == "b'!B813!B804'"): # Right - direction has been pressed
        print("Right was pressed!")
        multi_dir_speed(speed, 0, speed, -1, 0.4)
    elif(command == "b'!B516!B507'"): # Up - direction has been pressed
        print("Up was pressed!")
        multi_dir_speed(speed, 1, speed, -1, 0.4)
    elif(command == "b'!B615!B606'"): # Down - direction has been pressed
        print("Down was pressed!")
        multi_dir_speed(speed, -1, speed, 1, 0.4)
    else:
        # It's possible that there is left over items on the buffer
        # print what is there then remove the remaining on the buffer
        print(command)
        uart.write(command)
        command = uart.read(20)
        uart.write(command)
        # Obviously this does not work.
The function "multi_dir_speed" that makes things move to put it simply asks each wheel what direction, how fast, and how long. The how long I have hard-coded it to be .4 seconds long and I am not happy with that. It would be preferable if the app would determine the length of time by how long the user has pressed a particular direction.

Any help would be greatly appreciated. Thanks!

User avatar
FifthCloud
 
Posts: 22
Joined: Sat May 08, 2021 9:45 am

Re: Bluetooth Key events

Post by FifthCloud »

I found an article that very closely resembles the situation that I am.

https://circuitdigest.com/microcontroll ... olled-car/

If you take a look at the code that is listed at the bottom of the article for every integration with the motor there is sleep timer as shown below.

Code: Select all

def left_side_forward():
    print "FORWARD LEFT"
    GPIO.output(m21 , 1)
    GPIO.output(m22 , 0)
    time.sleep(.5)
    GPIO.output(m11 , 1)
    GPIO.output(m12 , 0)
The sleep timer is something I really want to avoid. Is there any way to do without it? Can anyone recommend a way to create something that resembles what a toy remote control car does when a person pushes the joystick half forward it goes forward at half speed? And if the person puts the joystick between forward and right that car moves diagnally?

Hope my original question makes sense and the article listed helps to illustrate the trouble I am facing.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Bluetooth Key events

Post by adafruit_support_mike »

You have to choose between two control options:

1) When the microcontroller receives a message saying a button was pushed, it turns the appropriate direction for a fixed amount of time, then returns to a neutral state.

2) When the microcontroller receives a message saying a button was pushed, it turns the appropriate direction and stays that way until the next message arrives.

That's a design choice that's mostly independent of the signals themselves.

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”