Monday, 27 April 2015

The Sheep Thrills of Programming 2

So I finally figured out all the programming. Here is a picture of my patcher:

Starting in the top left corner, we have the section of code that turns the sensors on. Once they are activated, the sensors each generate numbers. These numbers are sent out of the bottom of that section ("send distance" and "send touch") and received by the receivers ("receive distance" and "receive touch") on the next two columns of code. The numbers are then scaled down by the “scale” objects. This takes the numbers between 0 and 1000 and scales them down to between 0 and 10. Below the “scale” objects are “if” statements. These send the conditions of action.

For the distance sensor section of code, I have added two “if” statements. The one on the left tells the patcher to activate (known as a “bang” in Max 7) if the number is greater than 0. The one on the right tells it to send a “bang” if the number is smaller than 1. This section of code is designed to prevent the sensor from activating repeatedly when someone is standing near George.

The little box with the pointed, solid line is a “gswitch2.” This object changes the direction of the bang. When it is in the “off” (or 0) position, the bang cannot activate the code below (it is currently in the “on” [or 1] position).

There is another “gswitch2” below the first that basically controls which audio snippet is selected for playback. The “bang” travels through the “switch” and a “trigger” (an object that sends one “bang” to multiple outputs), which activates a “counter” object. This object counts up from 0 to 6 and then restarts. The number it generates will select what audio track is selected. The first audio track is number 1, the second is 2, and so on. So if the counter counts 1 after the first “bang,” it will activate the first track. When it receives another “bang,” it counts 2 and activates the next track, and so on.

Below the “counter,” there is also another “if” statement. This statement sends a “bang” if the number displayed by the counter is lesser than or equal to 1. Below that is a 3-second delay that will “bang” the fifth track. This piece of code was added so that I could play track 1 (“Hi! My name is George”) followed three seconds later by track 5 (“Pat my head if you want to hear a sheep joke!”).

The “trigger” object also sends a “bang” to a “message” that contains the number 1. This “message,” when banged, will turn the “trigger” off so that no new input will be accepted. It also sends a “bang” to a “delay” object that waits for a certain amount of time before sending the “bang” it received. After the delay, the “bang” is sent to a “message” that contains the number 0, which will open the “gswitch2” again and allow it to accept new input. This ensures that two tracks cannot start playing at the same time.

That is basically it. The touch sensor code works the same way. I included some extra messages to the right of the audio tracks that, when pressed, will activate a specific track. I added these for easy access and for demonstration purposes.

Voila! Now you understand what makes George tick. It is a simple patcher once you understand how it works, but it has fairly hilarious outcomes when powering a talking sheep.

No comments:

Post a Comment