Most products with MIDI interfaces (keyboards, etc.) produce sound when the receive MIDI notes.
But, with our Arduino kit, we can connect any sort of output device to respond to MIDI.
In this exercise, we'll use a servo to turn MIDI notes into physical motion.
Refer to the Tutorial to identify the three wires on the servo: Ground (GND), Power Supply (VCC), and the Control Signal
Using jumper wires, connect the servo to the breadboard.
Use pin 5 on the Pro Micro for the control signal
In the Arduino IDE
File > New
Select all, delete
Copy/paste the code below:
(Or, you can user File > Examples > Examples from customer libraries > Servo > Knob)
IMPORTANT:
The example sketch has the servo connected to pin 9. We're using pin 5, so make the appropriate edit in the sketch.
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it for use with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
Move the potentiometer and observe the servo motion.
Change the sketch to do something different:
Reverse the relative direction of the potentiometer and servo
Change the endpoint limits
You ideas?
In this example, MIDI_pot_note_servo_ProMicro.ino, MIDI_ON notes C3, D3, E3 on channel 2 to move to 3 different angles (positions.)
Take a look at the code to see how this works.
Find:
void noteOn(byte channel, byte pitch, byte velocity)
and notice the use of switch/case statements to respond to the MIDI note channel and pitch
to direct the output to the servo or the speaker (tone).
Download the sketch, and run it on your kit.
In your DAW or the Online MIDI Editor, create a track that uses MIDI channel 2 and send a sequence of notes to move the servo.
Perhaps it is in sync with the MIDI sequence on the MIDI channel 1 track?
How might you use a servo is some sort of project? Perhaps the servo could move or strike a physical object to make a sound?
How can you modify the Arduino sketch to accept more than 3 MIDI_ON notes?
If you're an experienced Arduino coder, can you think a better technique than switch/case to handle the channel and pitch?
How might MIDI_OFF notes be used with the servo?