Sunday, January 4, 2009

Arduino Control Voltage: Rough Code

I've worked a little more on the Arduino Control Voltage. The processing code bellow takes midi input from any midi device, reads the note and velocity information and then spits it back out over the USB Serial connection to the Arduino board. It's a roundabout convoluted way of getting midi information (midi being a serial data stream to begin with), I hope to get a direct midi input wired up soon.
Arduino PWM to CV out circuit


import promidi.*;
import processing.serial.*;

int com = 0; //Comport/Serial port #
MidiIO midiIO;
Serial sOut;
PFont font;
int pit;
int vel;
void setup(){

font = loadFont("ArialMT-40.vlw");
midiIO = MidiIO.getInstance(this);
println(Serial.list());

midiIO.printDevices();

midiIO.plug(this,"getMidi",0,0);
sOut = new Serial(this, Serial.list()[com], 9600);
}

void draw(){
background(255,255,255);
textFont(font,15);
fill(0,0,0);
text("Midi Data",0,14);
textFont(font,12);
text("Note:",0,28);
text("Velo:",0,42);
textFont(font,12);
text(pit,42,28);
text(vel,42,42);
}

void getMidi(Note note){
println("Got a note!");
vel = note.getVelocity();
pit = note.getPitch();
println(vel);
//println(pit);
sOut.write(vel);
sOut.write(pit);
}


Bellow is the (now) simple code to be uploaded to the Arduino board which just reads the serial data and controls the PWM output.

/*Arduino CV. Reads note and velocity information form the serial line and outputs to PWM pin */
byte note = 0;
byte vel = 0;
int synPin = 11; //Pin # for pitch CV output

void setup() {
Serial.begin(9600);
}

void loop() {
if(Serial.available() >= 2 ){ //Wait untill there is enough information
Serial.println(Serial.available(),DEC);
vel = Serial.read(); //FIX ME: Implement velocity sensitivity (VCA Control)
note= Serial.read();
analogWrite(synPin,note);
}

}

Creative Commons License
Arduino Control Voltage by Dana Simmons is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
Based on a work at code.google.com.