Postponed #1: Unlock the Safe!

I planed to do this patch with you in class. However, due to time constrains it will be postponed.

In this patch we will use a dial to represent the lock of a safe. The owner can open the safe by moving the dial to the right numbers in the right order. At every correct number, the user will have to wait at least 4 seconds.

The final patch will look like the picture on the left when the safe is locked and like the picture on the right, after the safe has been unlocked. You see an option to reset the process (a message box). Important elements of the patch we will use are the coll object which is used to store the correct numbers and their order, the dial object which represents the safe and is used for the user input and several little other new objects such as prepend, append and counter. We will also use a lot of objects we know already like select, metro, toggle, bang and number boxes.

 

Copy the user interface. Add a dial and change its size until it is rather big, add the comment on the top, a message with the reset message (also rather big), connect the number box to the outlet of the dial and add a comment with instructions to the patch.

The color of the dial’s background can be changed by sending a the “bgcolor”message to the dials inlet. We realize this like this:

 

We give the color values in the RGB format and use a prepend object to prepend the word “bgcolor”. If we click the message box with a higher red value the dial will receive a message in the format “bgcolor 0.7 0.4 0.4″ and change to a red background.

Now we add functionality to our save. First we add a counter which counts 4 seconds and sends a bang when 4 seconds have been reached. We use a loadbanng to make sure the counter is off when the patch is loaded. We also use a metro object in combination with a counter. The argument of the counter indicates that we count upwards, start with zero and count till 4. The counter counts one step with every bang it receives. In order to count seconds, we send a bang every second with the metro.  We add the comments to describe the intended behavior.

Our comments already indicate, that we only want to count the seconds when the dial is not changed. We can do that by connecting the output of the dial using the change object. The change object in combination with the button will produce a bang whenever the dial is moved. We use this bang to set the counter to zero. It’s output will also be set to zero and consequently, the seconds since nothing changed are also zero. When the dail stands still, the counter will not be set to 0 and therefor continue counting. If the dial is not moved for 4 seconds, we will see a flash on the button on the bottom.

 

 

We have used a loadbang to make sure the time is not yet counting when the patch is loaded. Let’s make sure, the toggle connected to the metro is switched on when the user starts. We can use the change object again to do so.

 

When the dial is first moved, the toggle is switched on. (If the toggle is on already and the dial is changed, we also receive the 1s. This ain’t pretty, but let’s ignore it for now).

When the counter has reached 4, we want to print a message which tells the value of the first/second/third number. We can use a message with replaceable arguments for that. When the counter reaches 4 seconds, we trigger a message in the style “The 1. number was a 74″.

 

We do not only want to print out the selected numbers but compare them to a secret code. There are several options. One is to combine all three numbers into one list and then compare the created list with a solution-list. We use the zl object for that.  “zl compare” compares two lists and tells us which indexes differ.

First, we have to combine all three numbers the user chooses into a list. We can do that using the gate and pack objects. As the left inlet of pack triggers the pack object ,  we count backwards from 3 to 1 and use a gate to route the  first number in the most right inlet of the pack object, then the middle number in the middle and then the last number in the left inlet. Automatically, a list will put out of pack with all three values. We then use zl compare to compare the list to the solution.

The relevant part looks like this:

We  make sure, that the safe opens (turns green) when all numbers are correct. If all numbers were correct, we send a bang to green. Have a look at the send help patch. We now have to receive the bang  at the right postion to turn the safe green.

 

We also have to make sure we start out with a red safe. Let’s add a loadbang to the red color message.

 

The only thing left to do is the reset button. When clicking the reset message, we basically want the same state as when we loaded the patch. We thus can use send and receive to solve this. We cause the reset message to send a bang.

And we go through our patch and make sure, every loadbang will receive that message. That way, we can simulate the conditions from loading the patch.

We add receive reset to all loadbangs:

Now we hide everything besides the necessary inputs and outputs (Hide on lock!) and the patch is finished!

The whole patch as a screenshot:

 

.