2018-02-07 10:42:46 +01:00
# Coding in MakeCode
2018-04-13 18:05:34 +02:00
This guide helps users who are used to work with @boardname @ to get familiar with using blocks in MakeCode.
2018-02-07 10:42:46 +01:00
2018-02-07 22:55:49 +01:00
## Snap together the blocks
2018-02-07 10:42:46 +01:00
2018-02-07 22:55:49 +01:00
Just like with LabView, blocks in the MakeCode editor can be dragged from the cabinet and snapped together
2018-02-07 10:42:46 +01:00
to create a sequence of program instructions.
2018-02-07 22:55:49 +01:00
Take a look a the LabView program below: it **starts** , turns on motor A, waits a second, and finally stops motor A.
2018-02-07 10:42:46 +01:00
![sequence of block ](/static/labview/sequencing.png )
2018-02-07 22:55:49 +01:00
The blocks in MakeCode have similar functions and go together in the same way: they snap into the ``||loops:on start||`` block and then connect to each other vertically.
2018-02-07 10:42:46 +01:00
```blocks
2018-02-19 16:35:08 +01:00
motors.largeA.run(50)
2018-02-15 01:05:31 +01:00
pause(1000)
2018-02-07 10:42:46 +01:00
motors.largeA.stop()
```
2018-02-07 22:55:49 +01:00
Any block program can be converted to JavaScript and you can edit it as lines of code too.
2018-02-07 10:42:46 +01:00
```typescript
2018-02-19 16:35:08 +01:00
motors.largeA.run(50)
2018-02-15 01:05:31 +01:00
pause(1000)
2018-02-07 10:42:46 +01:00
motors.largeA.stop()
```
## Download to the EV3
2018-04-17 01:15:58 +02:00
Before you actually run your program on the EV3 Brick, you can first try it in the simulator. The MakeCode editor includes a simulator in the browser for you to test your code. You can make changes to your program and check them out it the simulator to make sure your code works the way want. The similator knows when you modify your code and it restarts automatically to run the new code.
2018-02-07 22:55:49 +01:00
2018-04-17 01:15:58 +02:00
Once you're ready to transfer your program to the EV3 Brick, click the ``|Download|`` button and follow the instructions.
2018-02-07 10:42:46 +01:00
## Single motors
2018-02-07 22:55:49 +01:00
This program controls a large motor on port A in several different ways. It sets just the speed and then sets speed for: an amount of time, angle of movement, and a number of rotations.
2018-02-07 10:42:46 +01:00
![Single motor blocks ](/static/labview/motors.png )
```blocks
2018-02-19 16:35:08 +01:00
motors.largeA.run(50);
motors.largeA.run(50, 1000, MoveUnit.MilliSeconds);
motors.largeA.run(50, 360, MoveUnit.Degrees);
motors.largeA.run(50, 1, MoveUnit.Rotations);
2018-02-07 10:42:46 +01:00
motors.largeA.stop();
```
## Steering
2018-02-07 22:55:49 +01:00
The **steering** blocks let you to synchronize two motors at a precise rate. They can also specify the duration, angle, or number of rotations for the motors to turn.
2018-02-07 10:42:46 +01:00
![Steering blocks ](/static/labview/steer.png )
```blocks
motors.largeBC.steer(0, 50);
motors.largeBC.steer(0, 50, 1000, MoveUnit.MilliSeconds);
motors.largeBC.steer(0, 50, 360, MoveUnit.Degrees);
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations);
motors.largeBC.stop();
```
## Tank
2018-02-07 22:55:49 +01:00
The **tank** blocks control the speed of two motors. These are commonly used for a differential drive robot. The blocks can also specify the duration, angle, or number of rotations.
2018-02-07 10:42:46 +01:00
![Tank block ](/static/labview/tank.png )
```blocks
motors.largeBC.tank(50, 50);
motors.largeBC.tank(50, 50, 1000, MoveUnit.MilliSeconds);
motors.largeBC.tank(50, 50, 360, MoveUnit.Degrees);
motors.largeBC.tank(50, 50, 1, MoveUnit.Rotations);
motors.largeBC.stop();
```
## Coasting and braking
2018-02-07 22:55:49 +01:00
By default, all motors coast when any command used to move finishes. You can keep them from coasting with the ``||motors:set brake||`` block.
2018-02-07 10:42:46 +01:00
![Brake block ](/static/labview/brake.png )
```blocks
motors.largeD.setBrake(true);
2018-02-19 16:35:08 +01:00
motors.largeD.run(50, 1, MoveUnit.Rotations)
2018-02-07 10:42:46 +01:00
```
## Inverting and regulating motors
2018-02-07 22:55:49 +01:00
If you wan to change the direction that a motor turns, use the ``||motors:set inverted||`` block.
2018-02-07 10:42:46 +01:00
![Brake block ](/static/labview/invertmotor.png )
```blocks
motors.largeA.setInverted(true);
```
By default, the speed of motors is regulated. This means that if your robot goes up a hill,
the regulator will adjust the power to match the desired speed. You can disable this feature
2018-02-07 22:55:49 +01:00
using ``||motors:set regulated||``.
2018-02-07 10:42:46 +01:00
![Brake block ](/static/labview/unregulatedmotor.png )
```blocks
motors.largeA.setRegulated(false);
```
## Brick
2018-02-07 22:55:49 +01:00
The **Brick** category has a number of blocks to display graphics on the brick screen.
2018-02-07 10:42:46 +01:00
![brick image ](/static/labview/brickimage.png )
```blocks
brick.clearScreen()
brick.showImage(images.expressionsWink)
```
![brick status light ](/static/labview/brickstatuslight.png )
```blocks
brick.setStatusLight(StatusLight.Off);
brick.setStatusLight(StatusLight.Red);
brick.setStatusLight(StatusLight.OrangePulse);
```
## Waiting (pausing)
2018-02-07 22:55:49 +01:00
It is quite common to have to wait for a task to finish or for a sensor state to change, such as a touch button pressed. The ``||loops:pause||`` and ``||sensors:pause until||`` blocks provide a way for your program to wait for a period of time.
2018-02-07 10:42:46 +01:00
![pause for time ](/static/labview/pausefortime.png )
```blocks
2018-02-19 16:35:08 +01:00
motors.largeD.run(50)
2018-02-15 01:05:31 +01:00
pause(1000)
2018-02-07 10:42:46 +01:00
motors.largeD.stop();
```
![pause for touch ](/static/labview/pausefortouch.png )
```blocks
2018-02-19 16:35:08 +01:00
motors.largeD.run(50)
2018-02-07 10:42:46 +01:00
sensors.touch1.pauseUntil(ButtonEvent.Pressed)
motors.largeD.stop();
```
![pause for distance ](/static/labview/pausefordistance.png )
```blocks
2018-02-19 16:35:08 +01:00
motors.largeD.run(50)
2018-02-07 10:42:46 +01:00
sensors.ultrasonic4.pauseUntil(UltrasonicSensorEvent.ObjectNear)
motors.largeD.stop();
```
2018-02-07 22:55:49 +01:00
You can also use the ``||loops:pause until||`` block to wait on any [boolean ](/types/boolean ) expression. As your program runs, it waits until the condition (expression) inside becomes true.
2018-02-07 10:42:46 +01:00
```blocks
2018-02-19 16:35:08 +01:00
motors.largeD.run(50)
2018-02-07 10:42:46 +01:00
pauseUntil(() => sensors.touch1.isPressed())
motors.largeD.stop()
```
## Loops
![Single loop ](/static/labview/loopinfinite.png )
```blocks
2018-02-15 01:05:31 +01:00
forever(() => {
2018-02-19 16:35:08 +01:00
motors.largeD.run(50, 1, MoveUnit.Rotations);
motors.largeD.run(-50, 1, MoveUnit.Rotations);
2018-02-07 10:42:46 +01:00
})
```
![While loop ](/static/labview/while.png )
```blocks
for(let i = 0; i < 10 ; i + + ) {
2018-02-19 16:35:08 +01:00
motors.largeD.run(50, 1, MoveUnit.Rotations);
motors.largeD.run(-50, 1, MoveUnit.Rotations);
2018-02-07 10:42:46 +01:00
}
let k = 0;
while(k < 10 ) {
2018-02-19 16:35:08 +01:00
motors.largeD.run(50, 1, MoveUnit.Rotations);
motors.largeD.run(-50, 1, MoveUnit.Rotations);
2018-02-07 10:42:46 +01:00
k++;
}
```
## Variables
![Variable block ](/static/labview/speedoflightvar.png )
```blocks
let light = 0;
2018-02-15 01:05:31 +01:00
forever(function () {
2018-02-07 10:42:46 +01:00
light = sensors.color3.light(LightIntensityMode.Reflected);
2018-02-19 16:35:08 +01:00
motors.largeD.run(light)
2018-02-07 10:42:46 +01:00
})
```
## Concurrent loops
2018-02-07 22:55:49 +01:00
You can start up multiple ``||loops:forever||`` loops that will run at the same time. Actually, only the code in just one of the loops is really running at any exact moment in time. Each loop, though, gets a turn to run all of its code and this makes them run [_concurrently_ ](https://en.wikipedia.org/wiki/Concurrent_computing ).
2018-02-07 10:42:46 +01:00
2018-02-07 22:55:49 +01:00
![Multiple loops running at the same time ](/static/labview/multipleloops.png )
2018-02-07 10:42:46 +01:00
```blocks
2018-02-15 01:05:31 +01:00
forever(() => {
2018-02-19 16:35:08 +01:00
motors.largeD.run(50, 1, MoveUnit.Rotations);
motors.largeD.run(-50, 1, MoveUnit.Rotations);
2018-02-07 10:42:46 +01:00
})
2018-02-15 01:05:31 +01:00
forever(() => {
2018-02-07 10:42:46 +01:00
brick.showImage(images.eyesMiddleRight)
2018-02-15 01:05:31 +01:00
pause(1000)
2018-02-07 10:42:46 +01:00
brick.showImage(images.eyesMiddleLeft)
2018-02-15 01:05:31 +01:00
pause(1000)
2018-02-07 10:42:46 +01:00
})
```
## Conditional
2018-02-07 22:55:49 +01:00
The ``||logic:if||`` block allows you to run different code depending on whether some condition ([boolean](/types/boolean) expression) is `true` or `false` . Also, this is similar to the ``||loops:switch||`` block.
2018-02-07 10:42:46 +01:00
![Brake block ](/static/labview/ife.png )
```blocks
2018-02-15 01:05:31 +01:00
forever(function() {
2018-02-07 10:42:46 +01:00
if(sensors.touch1.isPressed()) {
2018-02-19 16:35:08 +01:00
motors.largeD.run(50)
2018-02-07 10:42:46 +01:00
} else {
motors.largeD.stop()
}
})
```
## Random
2018-02-07 22:55:49 +01:00
The ``||math:pick random||`` block returns a random number selected from a range of numbers.
2018-02-07 10:42:46 +01:00
![Brake block ](/static/labview/random.png )
```blocks
2018-02-15 01:05:31 +01:00
forever(function () {
2018-02-07 10:42:46 +01:00
motors.largeBC.steer(Math.randomRange(-5, 5), 50)
2018-02-15 01:05:31 +01:00
pause(100)
2018-02-07 10:42:46 +01:00
})
```