Tutorial 04. DIP switch control

The final set of built-in user inputs on MEAP are the eight DIP switches. They toggle between two states: up and down.


  1. This time, let's start with our code from Tutorial 03 where we had created a basic monophonic keyboard. We are going to use one of the dip switches to toggle between two octaves on the keyboard; when the first dip switch is toggled up, it will raise all pitches by an octave.
  2. From the last tutorial, you should be familiar with the structure of updateTouch(). The function that handles the DIP switches, updateDip(), is implemented in exactly the same way. The slight difference is that the second variable that the function gives you is now called up rather than pressed. It will be true when a DIP switch is toggled up, and false when it is toggled down. The DIP switches follow the same numbering format as the touch pads.
  3. There are several ways we could approach the task of raising an octave when the first dip switch is toggled but this is the approach we will take:
    1. Create a variable called octave_multiplier which will have a value of either 1 or 2.
    2. In updateTouch(), whenever we set the frequency of an oscillator, we will first multiply that frequency by octave_multiplier. When octave_multiplier is equal to one, the pitch will be unchanged, but when it is equal to two, the frequency will by doubled, raising it by an octave.
    3. Set octave_multiplier to 2 when DIP 0 is raised, and 1 when it is lowered.
  4. First let's create the octave_multiplier variable as a global variable beneath the declaration of our my_sine oscillator and initialize it to 1

    int octave_multiplier = 1;

  5. Now in updateTouch() we want to multiply every frequency by this variable. For example, the first pad's "pressed" section would now look like this:

    my_sine.setFreq(220 * octave_multiplier);

  6. Now in updateDip() we need to set octave_multiplier. We want it to be 2 when DIP 0 is toggled up and 1 when it is toggled down so we should modify the DIP 0 section as follows.

    if (up) {  // DIP 0 up
        Serial.println("d0 up");
        octave_multiplier = 2;
    } else {  // DIP 0 down
        Serial.println("d0 down");
        octave_multiplier = 1;
    }

  7. Upload the code and try toggling between octaves as you play the keyboard!

FULL CODE HERE