Patch #5: Counting on you

This exercise it meant to give you a head start with the homework assignment 1. The homework assignment asks you to create a counter which reacts to the keyboard’s up and down arrow keys. We make a simplified counter wich counts how often you manage to press the space bar in one minute.

First, we need an object which registers which keys we press. This object is called key. Press n and type ‘key‘.

Open the help patch. Try typing a few letters and watch the output. We are interested in the output of the space bar. The space bar causes the ASCII value “32”. This is a good start. We know that the key object will output 32 out of its left outlet whenever we press the spacebar. Therefor we need some object, which can check wether the output matches 32. The select object does so. Add a select object by pressing n and typing ‘select‘. Open the select help: select will output a bang in response to input which matches its argument. Therefor, “select 32” should do the trick. In this little patch, the button flashes, whenever we press the space bar:

We now have to count the bangs. Max of course has a built in count object. But because we prefer you to learn some basics first, we will built our own counter, by adding up 1 to the current number of space-bar-presses with every space-bar hit. We need the + (plus) object to do so. Add it (press n and type ‘+‘). The plus object works just like the divide object we got to know earlier. It adds the number it receives on its left inlet to the number it has received on its right inlet. Again, receiving a number in the left inlet, triggers the calculation. You can learn this as a general rule: the left inlet of a max object always triggers the action. Let’s also add a number box to display the current amount of spacebar-hits. 

 

If we want to add one to the current amount with every bang, we should send a 1 to the to the plus object. The question is, should it be send to the left or the right inlet? As it should trigger the new value immediately, the left inlet seems appropriate.

This only works once. Why? Because the plus object does not add the 1 to the amount of clicks but to its initial 0 every time. In order to add one to the amount of clicks, we have to send the amount of clicks in the right inlet of the + object. That way, when it receives another 1 in its left inlet, it adds the 1 to the amount of clicks and also updates the amount of clicks immediately. We use the + object as memory cell which knows about the amount of clicks.

The patch should look like this:

Don’t forget to make comments to explain your patches. Comments are required in the homework assignment. Our patch now counts the spacebar presses. Note, that we can not switch the right and left inputs of the + object and feed the amount of clicks back to the left inlet. That’s because the left inlet triggers the calculation. If the output of the + object is fed right back into the left inlet of the same object, every output immediately triggers new output and we get a infinite loop. Computers are not good in dealing with those.

We are almost done. We now want to count how often we can press the spacebar in a minute. An object which comes handy is the delay object.  Add the delay object to your patch (press n and type delay) and open the help. The delay object receives a bang and then holds onto it for a specified amount of time before sending the bang to the next object. We make sure our delay object receives a bang when we press the spacebar for the first time and waits a minute before sending the bang to the next object. We can do that by using select again:

(You might have noticed that the patchcords from the number box going back to the + object is not taking the direct root but are nicely rooted. You achieve this by selecting the patchcord once it has been connected and pressing shift+cmd+y or by using the menu bar > Arrange > Route Patch Cords.)

We now want the final bang to report the amount of spacebar hits. We can do that by silently changing a message to contain the current amount of presses and only outputting the message when the minute is over. We can silently change a message using the set message or using the right inlet! Have a look:

And even nicer, using a replaceable argument as well:

The only problem now is that we have to reset the counter to 0 by hand if we want to try again, otherwise, we do not trigger the the delay with counting the first press and of course, we would count a wrong amount of presses if we do not start with 0. Therefor we make sure it starts with 0 when loading the patch and add an automatic reset after the minute is over. We use a loadbang for setting the value to 0 when starting the patch. A loadbang sends a bang from its outlet whenever the patch is loaded. We have to be careful to reset the amount of presses when the minute is over after the score has been printed to the max window. Because order is an issue we’ll use the trigger object.

Of course, you can copy+paste and adapt relevant parts of the patch for the Lab Assignment.

 

WHAT YOU SHOULD BE ABLE TO DO BY NOW:

  • use key to register keypresses
  • use select to check for certain messages (and certain keypresses in combination with key)
  • use the + (plus) object to add numbers
  • build a counter
  • use the daly object to delay a bang
  • use a loadbang to send a bang when a patcher is loaded

WHAT YOU SHOULD KNOW BY NOW:

Key: Key reports key presses on the computer keyboard

Select:  Select will output a bang in response to a input which matches its arguments and will output non-matching messages out its right-most outlet.

+ (Plus): + takes two given numbers, adds them, and then outputs the result. The left inlet triggers the calculation.

Delay: delay delays a bang for a specific amount of time

Loadbang: sends a bang when a patcher is loaded