Music Mouse, Part IV

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:

  • What value do you see when the joystick is pushed all the way down?
  • What value do you see when the joystick is pushed all the way up?

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 code to handle changes to y 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.


Leave a Reply

Your email address will not be published. Required fields are marked *