Remote control

This page proposes two simple programs for controlling your EV3 robot in real time from the PC. The first program appears in two versions, one using 'If' and the other using arrays. 

The first program below detects keypresses and uses them in real time to control the robot's motors. It uses the number keys on the numeric keypad. If you don't have a numeric keypad then you can use the other number keys or change the program to replace the numbers with more convenient letter keys. This program won't work in brick mode since it makes use of the Small Basic text window.

You can copy and paste this program into Small Basic.

'********************************************************************************************

'Connect motors to ports B and C.
'Type numbers into the text window to control the robot in real time.
'If your computer does not have a numeric keypad then just change the
'numbers to keyboard letters that you choose.

Textwindow.Writeline("Press the 8 key to go forwards extra fast")
Textwindow.Writeline("Press the 5 key to go forwards at normal speed")
Textwindow.Writeline("Press the 4 key to turn left")
Textwindow.Writeline("Press the 6 key to turn right")
Textwindow.Writeline("Press the 2 key to go backwards")
Textwindow.Writeline("Press the 0 (zero) key to stop")
Textwindow.Writeline("You can also try the 7, 9, 1, and 3 keys")
Textwindow.Writeline("Don't hold the keys down!")
Textwindow.Writeline("")

While "True"   'repeat forever, because "True" is always true!
    inputletter=TextWindow.ReadKey()
    If inputletter="8" Then
        b_speed = 80
        c_speed = 80   
    ElseIf inputletter="4" Then
        b_speed = -10
        c_speed = 10   
    ElseIf inputletter="5" Then
        b_speed = 40
        c_speed = 40  
    ElseIf inputletter="6" Then
        b_speed = 10
        c_speed = -10   
    Elseif inputletter="2" Then
        b_speed = -30
        c_speed = -30   
    Elseif inputletter="7" Then
        b_speed = 0
        c_speed = 20 
    Elseif inputletter="9" Then
        b_speed = 20
        c_speed = 0 
    Elseif inputletter="1" Then
        b_speed = -20
        c_speed = 0 
    Elseif inputletter="3" Then
        b_speed = 0
        c_speed = -20 
    Elseif inputletter="0" Then
        b_speed = 0
        c_speed = 0  
    EndIf
    Motor.StartSync("BC", b_speed, c_speed)
    TextWindow.Write (inputletter)
EndWhile

'*******************************************************************************

Here below is an alternate, neater, version of the same program which avoids the clumsy If..ElseIf..EndIf structure in favour of two arrays which hold the speeds that each motor should have when each number key is pressed.

The way the arrays are created is incompatible with the EV3 Explorer compiler so you cannot compile the program below to the brick but that's fine because the program would never work in brick mode anyway since it makes use of the Small Basic text window (just like the program above). In other words, run these programs only in PC mode (directly from Small Basic).

Know that using PC mode with a wireless connection is not as reliable as using PC mode with a USB connection. If you decide to use a wireless connection then prefer Bluetooth to WiFi.

'*******************************************************************************

'connect motors to ports B and C
Textwindow.Writeline("Press the 8 key to go forwards extra fast")
Textwindow.Writeline("Press the 5 key to go forwards at normal speed")
Textwindow.Writeline("Press the 4 key to turn left")
Textwindow.Writeline("Press the 6 key to turn right")
Textwindow.Writeline("Press the 2 key to go backwards")
Textwindow.Writeline("Press the 0 (zero) key to stop")
Textwindow.Writeline("You can also try the 7, 9, 1, and 3 keys")
Textwindow.Writeline("Don't hold the keys down!")
Textwindow.Writeline("")

'next two lines define arrays with the speeds for motors B and C
b_speed="0=0;1=-20;2=-30;3=0;4=-10;5=40;6=10;7=0;8=80;9=20"
c_speed="0=0;1=0;2=-30;3=-20;4=10;5=40;6=-10;7=20;8=80;9=0"

While "True"   'repeat forever
    inputletter=TextWindow.ReadKey()
    Motor.StartSync("BC", b_speed[inputletter], c_speed[inputletter])
    TextWindow.Write (inputletter)
EndWhile

'*******************************************************************************

The next program (below) uses the position of the mouse over the Small Basic graphics window to control the robot's motors which must be attached to ports B and C. This program neatly demonstrates that EV3 Basic can be used to make programs that cannot be made with the normal Lego EV3 software. It's also neat because it provides a very simple way to choose a combination of motor speeds from more than one hundred thousand possible combinations!

The graphics window is first resized to 400 x 400 pixels, then reference lines are drawn. The coordinates of the mouse are then calculated relative to those lines and finally the coordinates are used to adjust the speeds of motors B and C.

You can copy and paste this program into Small Basic.

Note that this program will, of course, only work if you run the program from the PC ("PC mode"). If you download the program into the EV3's file system using EV3 Explorer and then try to run it from the brick screen ("Brick mode") it will fail because it will not be able to interact with the Small Basic graphical window on the PC.

'********************************************************************************************
'Attach motors to ports B and C
GraphicsWindow.WIDTH=400
GraphicsWindow.HEIGHT=400
GraphicsWindow.DrawLine(200,0,200,400 )
GraphicsWindow.DrawLine(0,200,400 ,200 )

While "True"    'repeat forever
    x = (GraphicsWindow.MouseX-200)/2
    y = (200-GraphicsWindow.MouseY)/2
    b_speed = (x+y)/2     'calculate speed for motor B
    c_speed = (y-x)/2     'calculate speed for motor C
    Motor.StartSync("BC", b_speed, c_speed)
    Program.Delay(100)    'This line is not vital. Is it helpful?
EndWhile
Comments