Tuesday, November 07, 2006

MIDI LAB

i really don't understand this MIDI lab.

the first problem i had was understanding that the TX is sending data !!FROM!! the arduino !!TO!! the midi port. huge. after all of the work i've done thus far, the idea of sending control information somewhere other than the arduino &/or a visible output of the breadboard, had put me in a very closed minded way of thinking. but, ok that makes sense.

also, when you let someone whom you percieve to be (one of) the class genius(s) solder something because they are doing the lab before you, and need to borrow a component.... you should always check their work. in this case, my MIDI connector was soldered incorrectly which cost me a lot of time. the good news is, i figured out the problem before i jumped out the window, and also became very familiar with both the code, and the curcuit. unfortunately, in this case, not close enough to the code. BECUASE I DON'T UNDERSTAND IT!

in theory, the analog src (in my case a potentiometer) should have some effect over the MIDI data going into the computer, however, the MIDI playback device, just cycles through every MIDI note in a 0-127 range, with no change based on input from either the digital switch state, or the analog potentiometer values.

below is the code, and how i read it is very different. in my microcontroller of a brain, the MIDI data is only transmitted while the switch state is true (on) but, while it is false, no data is transmitted. furthermore, the values should only change should the position of the analog input be changed? am i wrong in this thinking? i dunno, i need sleep now more than i need to stare at this screen anymore. but be certain dear reader, that i must understand this technology, because the potential for what i could do with it is exciting (to me).

here's the code as seen on the lab site:


// The switch is on Arduino pin 10:
#define switchPin 10
// Middle C (MIDI note value 60) is the lowest note we'll play:
#define middleC 60
// Indicator LED:
#define LEDpin 13

// Variables:
char note = 0; // The MIDI note value to be played
int AnalogValue = 0; // value from the analog input
int lastNotePlayed = 0; // note turned on when you press the switch
int lastSwitchState = 0; // state of the switch during previous time through the main loop
int currentSwitchState = 0;

void setup() {
// set the states of the I/O pins:
pinMode(switchPin, INPUT);
pinMode(LEDpin, OUTPUT);
// Set MIDI baud rate:
Serial.begin(31250);
blink(3);
}

void loop() {
// My potentiometer gave a range from 0 to 1023:
AnalogValue = analogRead(0);
// convert to a range from 0 to 127:
note = AnalogValue/8;
currentSwitchState = digitalRead(switchPin);
// Check to see that the switch is pressed:
if (currentSwitchState == 1) {
// check to see that the switch wasn't pressed last time
// through the main loop:
if (lastSwitchState == 0) {
// set the note value based on the analog value, plus a couple octaves:
// note = note + 60;
// start a note playing:
noteOn(0x90, note, 0x40);
// save the note we played, so we can turn it off:
lastNotePlayed = note;
digitalWrite(LEDpin, HIGH);
}
}
else { // if the switch is not pressed:
// but the switch was pressed last time through the main loop:
if (lastSwitchState == 1) {
// stop the last note played:
noteOn(0x90, lastNotePlayed, 0x00);
digitalWrite(LEDpin, LOW);
}
}

// save the state of the switch for next time
// through the main loop:
lastSwitchState = currentSwitchState;
}

// plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(char cmd, char data1, char data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}

// Blinks an LED 3 times
void blink(int howManyTimes) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(LEDpin, HIGH);
delay(100);
digitalWrite(LEDpin, LOW);
delay(100);
}
}