teaching machines

Music Mouse, Part IV

July 15, 2018 by . Filed under electronics, music, public.

This post is part of a series of notes and exercises for a summer camp on making musical instruments with Arduino and Pure Data.

We’ve got horizontal motion working in the Music Mouse. Let’s add vertical motion. This will be very similar to the horizontal motion, but there is an important difference.

Vertical Motion

In loop, read and print the value from pin A1. This pin controls the y-position. Upload and run the program. Inspect the values you see and answer these questions:

It’d be nice if pushing down made the number go negative, idle was 0, and pushing up made the number go positive. Let’s make that happen. We want to turn a number in the top range into a number in the bottom range:

$$\begin{array}{lrcrl}[& 0, && 1023 &] \\ && \downarrow && \\ [& 511, && -512 &]\end{array}$$

How do we do that?
See how the ranges are inversely proportional? That suggests we need to do some negation to flip the direction of the range. Try multiplying by -1 as the first step.

No, really. How do we do that?
Negate the number and add 511. Like this:
$$\begin{array}{lrcrl}[& 0, && 1023 &] \\ && \downarrow & {} \times -1 & \\ [& 0, && -1023 &] \\ && \downarrow & {} + 511 & \\ [& 511, && -512 &]\end{array}$$

Center the reading and test that it works.

Create variable dy and calculate it similarly to dx. Create variable scale_index_y. Add a block laden with conditionals to act on changes to dy just like you did with the horizontal motion—but use the vertical variables instead.

Upload your code and test that you can change both x and y.

Challenges

Things are intense. We must press on.