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. |
![]() |
- 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.
- 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 thanpressed. 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. - 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:
- Create a variable called
octave_multiplierwhich will have a value of either 1 or 2. - In
updateTouch(), whenever we set the frequency of an oscillator, we will first multiply that frequency byoctave_multiplier. Whenoctave_multiplieris 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. - Set
octave_multiplierto 2 when DIP 0 is raised, and 1 when it is lowered.
- Create a variable called
- First let's create the
octave_multipliervariable as a global variable beneath the declaration of ourmy_sineoscillator and initialize it to 1int octave_multiplier = 1; - 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); - 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; } - Upload the code and try toggling between octaves as you play the keyboard!
