2010-11-10

3) Choosing Notes by Potentiometers

In this part we introduce one potentiometer (10 kOhm) per step to choose the note of the step. The current code is for N = 4 steps, but can be easily extended to more steps. As an Arduino Uno has only 6 anolog inputs this would allow N = 6 which is not enough for musical applications. This is why an Arduino Mega with 16 analog inputs should be used here. LowestNote and HighestNote define the range of MIDI notes that can be played. Both parameters should be multiples of 12, because then the note range goes from a C to a higher C.

Parts list
-2 x Resistor 220 Ohm
-1 x Resistor 100 kOhm
-1 x Diode
-1 x opto coupler IC GNY17-2
-2 x 5-pole DIN female connector (180°)
-1 x MIDI cable
-1 x MIDI sync master (here: Roland TR-505)
-1 x MIDI sync slave (here: DIY synthesizer with MIDI-input)
-1 x Arduino (here: Arduino Mega. For this demo also Arduinos with less input and output channels will work)
-N x Potentiometer 10 kOhm

Schematics


















Software



// Declaration of Varialbes
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step;
int note;
int noteval;
int LowestNote;
int HighestNote;

// Initialization
void setup() {
Serial.begin(31250);
clock_step=0;
note = 0x3F;
noteval = 0;
LowestNote=36;
HighestNote=36+3*12; //3 Octaves over LowestNote
}

// Main Programm
void loop() {
if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}
else if(data == midi_stop) {
play_flag = 0;
clock_step=0;

sendMidiNote (0x80, note, 0x7F); //last note off
}
else if((data == midi_clock) && (play_flag == 1)) {
Sync();
}
}
}


// Functions

void Sync() { // play 8 fixed 16th notes, repeat after the cycle is finshed
clock_step = clock_step+1;
if (clock_step==1){ //1st step

sendMidiNote (0x80, note, 0x7F); //last note off


noteval
= analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==7){ //2nd step

sendMidiNote (0x80, note, 0x7F); //last note off


noteval
= analogRead(1);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on

}
if (clock_step==13){ //3nd step

sendMidiNote (0x80, note, 0x7F); //last note off


noteval
= analogRead(2);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on

}
if (clock_step==19){ //4nd step

sendMidiNote (0x80, note, 0x7F); //last note off


noteval
= analogRead(3);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on

}

else if (clock_step==24){
clock_step=0;
}
}

void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}



No comments:

Post a Comment