Course 4 / Lecture 7:

Learn to use states in robot control (Build the safe)

In this lecture you will learn to use states with the help of variables in your code. The main task is to build a safe with a combination lock. You will use a motor as the input for the combination dial and you will control another motor to open the safe's door, once the desired state in your program code is reached.


Figure 1 - Ozeki Safe and the controller program

Requirements

Instruction video

Lecture video - How you can use the Ozeki Safe to keep your money safe

Task #1:

Describe what a combination lock is!

Combination lock - it is a type of locking device in which a sequence of numbers is used to open the lock. If the entered sequence of numbers are valid, the door of the safe will open.

Task #2:

Check this video about how to open a combination lock!

In the following video, you can check how to open a combination lock when you already know its combination. After learning how this type of lock works, you can continue to solve the next tasks. The original link of the video is available at https://youtu.be/AAH8b63szYs.


Task #3:

Create a block for reading the current state of the combination lock of the safe!

To read the state of the combination lock, the first step is to do to get the angle of the combination lock's motor. You can use the 'Motor angle [connection] [motor]' block from Lego motors for it. It returns the angle value between 0 and 360 degrees. It is good to know that when you turn on the Brick, the angle of the motor will be 0 degree by default. So let's get in programming! Select Lego motors category and create a new block! Type 'read state' into the textbox, select the Reporter type then click on OK. You should see an empty 'report' block. It provides the return value of this block. At first, place a 'Motor angle' block then please select the connection of the connected motor for its connection field and choose option A for the motor field because the combination lock's motor connected to the A port of the Brick. Since 10 different numbers can be selected on the lock, the 360 should be divided by 36. Since the result can be a fractional number, so it must be rounded. So, with the mathematical formula it looks like this: round(motor_angle/36). Due to this, you always get the value as a result to which the lock's state is closer. But there is one problem with this formula. You can also get 10 as a result, when the lock's state is closer to 0. To solve this problem, you have to make a condition if the result is equal to 10, then the return value should be 0. The complete block can be seen on Figure 2. Finally click on OK and try it by clicking on the 'read state' block!


Figure 2 - Creating block for reading the lock's state

Task #4:

Create an entry point to read the 3-digit combination to a variable!

At first, you need to create a new variable to store the current combination selected on the lock. So choose the Variables category, click on the 'Make a variable' button and create a new variable called 'combination'. It is recommended to set an initial value to it. It must be a value that you know is an initial value for example -1. It is a good choice, because you can not select -1 state on the lock. The next step is to read the lock's state 3 times, then merge and store the read values in this variable as a combination. Since you have to read 3 times, you will need a 'repeat [3]' block from Control category. Inside this block, you need to do the operations that read and store the lock's state value. The basic idea is to treat the read numbers as text and merge them together to store it in the 'combination' variable. So at first, you need to check if the 'combination' variable holds the initial value. If yes, you just need to replace its value with the current lock state. If not, you need to concatenate the value of the 'combination' variable with the current state of the lock. These steps are demonstrated on Figure 3. You can also see 'wait' and 'Play note' blocks. The first one is to have time to twist the lock to the desired position. The second one is to sign if the current state is read by the program.


Figure 3 - Reading 3-digit combination

Task #5:

Upgrade the program to read the combinations repeatedly until the valid one (1-2-3) is entered!

Suppose that the door lock is opened when the combination 1-2-3 is entered on the combination lock. So your task to read the combinations repeatedly until 1-2-3 is entered. To implement this, you need to use a 'repeat until [condition]' block and move the previously placed 'set' and 'repeat' blocks into it as can be seen on Figure 4. In the condition field of the block, it is necessary to specify the condition to repeat the loop core until the combination 1-2-3 is entered. To do this, place a '[value] = [value]' block into its condition field. The first 'value' field of the '=' block should be the 'combination' variable that stores the lastly read combination, and the second 'value' field's value should be '123'. Due to this, the loop core is repeated until the value of the 'combination' variable equals to 123.


Figure 4 - Reading the combinations repeatedly until the valid one is entered

Task #6:

Open the safe door if the right combination is entered!

You wrote the code in the previous task to wait until the right combination is entered. Now your task is only to open the lock door and blink the LED in green light then close the door if the Touch sensor is pressed. So you just need to attach the blocks to the bottom of the 'repeat until' block. At first, blink the Brick's LED in green light using the 'Set LED [green flashing]' block from Lego category. The motor connected to the C port of the Brick is responsible for the lock door moving, so you need to rotate this motor by -140 degree to open it. You can do it by using the 'Rotate [C] by [-140]° at [30]%' block. Then the program should wait until the Touch sensor is pressed. So put the 'Button on [button] ?' predicate type of Lego block into a 'wait until' block and attach it to the bottom one. Finally, rotate the door's motor by 140 degree and set the LED light to green like Figure 5 shows.


Figure 5 - Opening the safe door

Task #7:

Upgrade the program to ask the combination to be used!

In the previous versions of your program, you burned the combination of the safe into the code (as 123). Now, you need to ask the combination to be used for opening the door. At first, you need to insert an 'ask [text] and wait' block found in Sensing category between the 'set' and 'repeat until' blocks. This block will display the text on the drawing board you typed in its text field and wait for your answare as you can see on Figure 6. At least, you need to replace the text '123' in the 'repeat until' block with the 'answare' built-in variable found in Sensing category. This variable's value will be the answare you typed in due to the 'ask and wait' block. Your safe is ready to use! Try it now!


Figure 6 - Asking the combination to be used


Program code

Program code
Figure 6 - Program code that constrols the Ozeki Safe

More information


Next


Copyright © 2000- |Ozeki Ltd | info@ozekirobot.com |
Page: 6246 | 79.99.42.43 | Login