mBot 2 and Bluetooth controller


#1

Hello, can someone give me the coding about bluetooth controller and mbot 2 ? i want the motor run only when i press a button and stop when release. thx


#2

@Yuen
image

Try that. :smiley:


#3

Thx!!! its worked. May I know the principle of this coding ?


#4

@Yuen Of course! Here is how the code works.

Summary:

  • When the CyberPi starts up, it continuously checks the state of four directional buttons.
  • Depending on which button is pressed, it will either move forward, backward, turn left, or turn right at a set speed of 50 RPM.
  • If no button is pressed, the CyberPi will stop all motor movements.

Detailed:

  • When the CyberPi starts up
    • Run this [CODE] forever

Here is the code we are running forever:

If the up button is pressed, move up.
Otherwise, If the down button is pressed, move down.
Otherwise, If the left button is pressed, turn left.
Otherwise, If the right button is pressed, turn right.
Otherwise, None of those buttons are being pressed, So, Stop moving.


#5

@Yuen @Best_codes This is an automated AI response. Verify important information.

This code is a visual representation of a program designed for the CyberPi, which is a programmable controller used in robotics and other electronic projects. The code is written in a block-based programming language similar to Scratch, which is commonly used to teach programming concepts to beginners. Let’s break down the code step by step:

  1. Initialization:

    • The program starts with the block when CyberPi starts up. This means that the following instructions will be executed once the CyberPi is powered on or the program starts.
  2. Forever Loop:

    • The forever block contains all the instructions that will be continuously executed in a loop.
  3. Conditional Statements:

    • The program checks the state of four different buttons (each associated with different directions) and performs corresponding actions.
  4. Button Pressed Actions:

    • Forward Movement:
      • if button pressed then moves forward at 50 RPM: If the button for moving forward (typically “up” button) is pressed, the CyberPi will move forward at a speed of 50 revolutions per minute (RPM).
    • Backward Movement:
      • else if button pressed then moves backward at 50 RPM: If the forward button is not pressed, but the button for moving backward (typically “down” button) is pressed, the CyberPi will move backward at 50 RPM.
    • Left Turn:
      • else if button pressed then turns left at 50 RPM: If neither the forward nor backward button is pressed, but the button for turning left (typically “left” button) is pressed, the CyberPi will turn left at 50 RPM.
    • Right Turn:
      • else if button pressed then turns right at 50 RPM: If none of the previous buttons are pressed, but the button for turning right (typically “right” button) is pressed, the CyberPi will turn right at 50 RPM.
    • Stop:
      • else stop encoder motor all: If none of the buttons are pressed, the CyberPi will stop all motor movements.

This code essentially makes the CyberPi respond to directional button presses to control its movement, making it act like a simple remote-controlled robot.


#6

awesome!!! Thx!!!


#7

No problem! Be sure to ask if you need anything else.


#8

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.