Here is my graphing code in Processing. Dont hate me for the poor programming techniques...
//Reads 0 - 5 volts
import processing.serial.*;
Serial myPort; // Create object from Serial class
float val, oldVal; // Data received from the serial port
String portName;
color lineColor = color(255,0,0);
static final int STARTX = 72;
static final int STARTY = 32;
float heightResolution = 0.01;
int GRAPH_HEIGHT;
float GRAPH_LOW;
float GRAPH_HIGH;
float GRAPH_STEPY;
byte serialPort = 3;
void setup()
{
size(800, 400);
// /dev/tty.usbmodem1411
portName = Serial.list()[serialPort];
myPort = new Serial(this, portName, 115200);
GRAPH_LOW = 00;
GRAPH_HIGH = 254;
GRAPH_STEPY = 10;
heightResolution = (GRAPH_HIGH - GRAPH_LOW) / (height - STARTY);
GRAPH_HEIGHT = height;
textFont(createFont("Consolas", 16));
noStroke();
background(255); // Set background to white
frameRate(120);
fill(0);
drawGridUnits(GRAPH_LOW, GRAPH_HIGH, GRAPH_STEPY);
}
int x = 0, oldX = 0;
int t = 0;
int temp = 0, temp2 = 0;
void draw()
{
noStroke();
if(myPort != null && myPort.available() > 0)
{ // If data is available,
//val = getValue(); // read it and store it in val
//fill(255,0,0);
//rect(x, y, w, h)
/*rect(oldX + STARTX, ((GRAPH_HEIGHT - (oldVal * (1.0 / heightResolution))) - (12 + 2)) - STARTY, textWidth("888.888"), 12 + 2);
fill(0);
text(val, x + STARTX, ((GRAPH_HEIGHT - (val * (1.0 / heightResolution))) - 2) - STARTY);
graph(x + STARTX, val, lineColor);
oldX = x;
oldVal = val;
x++;
x %= width - STARTX;*/
fill(255);
strokeWeight(0);
rect(STARTX, height - STARTY, width - STARTX, -GRAPH_HEIGHT);
x = 0;
if(myPort.read() == 255) //start value
{
drawLinearGrid(GRAPH_LOW, GRAPH_HIGH, GRAPH_STEPY);
temp2++;
fill(0);
int Sread;
while(x < 64)
{
while(myPort.available() <= 0)
delay(1);
Sread = myPort.read();
rect(STARTX + x*11, (GRAPH_HEIGHT - (val * (1.0 / heightResolution))) - STARTY, 10, -((Sread - GRAPH_LOW > 0) ? (Sread - GRAPH_LOW) : 0) * (1.0 / heightResolution));
x++;
}
}
if(millis() - temp > 1000)
{
stroke(255);
strokeWeight(0);
temp = millis();
fill(255);
rect(8, height - 7, textWidth("888 hz "), -(12 + 2));
fill(0);
text(temp2 + " hz", 8, height - 8);
temp2 = 0;
}
delay(1);
}
//print(millis());
//print(" ");
//println(myPort);
if(myPort == null && Serial.list().length >= serialPort && Serial.list()[serialPort].equals(portName))
myPort = new Serial(this, portName, 115200);
else if(Serial.list().length < serialPort)
{
if(myPort != null)
myPort.stop();
myPort = null;
}
}
void graph(int x, float y, color c)
{
// draw the line:
stroke(c);
line(x, (GRAPH_HEIGHT - STARTY), x, (GRAPH_HEIGHT - STARTY) - (y * (1.0 / heightResolution)));
stroke(255);
line(x, 0, x, (GRAPH_HEIGHT - STARTY) - (y * (1.0 / heightResolution)) - 1);
int inc = 1;
if(red(c) >= 255 && green(c) < 255 && blue(c) <= 0)
{
lineColor = color(red(c), green(c) + inc, blue(c));
}
else if(green(c) >= 255 && red(c) > 0 && blue(c) <= 0)
{
lineColor = color(red(c) - inc, green(c), blue(c));
}
else if(green(c) >= 255 && blue(c) < 255 && red(c) <= 0)
{
lineColor = color(red(c), green(c), blue(c) + inc);
}
else if(green(c) > 0 && blue(c) >= 255 && red(c) <= 0)
{
lineColor = color(red(c), green(c) - inc, blue(c));
}
else if(blue(c) >= 255 && red(c) < 255 && green(c) <= 0)
{
lineColor = color(red(c) + inc, green(c), blue(c));
}
else if(red(c) >= 255 && blue(c) > 0 && green(c) <= 0)
{
lineColor = color(red(c), green(c), blue(c) - inc);
}
}
void drawLinearGrid(float low, float high, float interval)
{
strokeWeight(0);
stroke(0);
line(STARTX, 0, STARTX, (GRAPH_HEIGHT - STARTY) + 8);
int j = 0;
for(float i = low; i < high; i += interval)
{
line(STARTX - 8, (GRAPH_HEIGHT - STARTY) - j * ((GRAPH_HEIGHT - STARTY) / ((high - low) / interval)), width, (GRAPH_HEIGHT - STARTY) - j * ((GRAPH_HEIGHT - STARTY) / ((high - low) / interval)));
j++;
}
}
void drawGridUnits(float low, float high, float interval)
{
strokeWeight(0);
stroke(128);
textAlign(RIGHT, CENTER);
int j = 0;
for(float i = low; i < high; i += interval)
{
text(i, STARTX - 10, (GRAPH_HEIGHT - STARTY) - j * ((GRAPH_HEIGHT - STARTY) / ((high - low) / interval)));
j++;
}
textAlign(LEFT, BASELINE);
}
float getValue()
{
float value = 0;
float digit = 1;
byte input = 0;
boolean timeout = false;
while(true)
{
while(myPort == null || myPort.available() <= 0)
{
if(timeout)
return -1;
delay(1);
timeout = true;
}
input = (byte)myPort.read();
return input * (20.0 / 1023.0);
}
/*if(input == 11)
return value;
timeout = false;
if(input == 10) // decimal point
{
digit = 0.1;
continue;
}
value += (float)input * digit;
if(digit >= 1)
digit *= 10;
else
digit /= 10;
}*/
}
This is the circuit I used for the electret microphone, it uses a single supply op amp. The 22kOhm resistor is the gain resistor, I changed it to 200kOhm (which seems very high to me but it seems to be what I needed for it to be moderately sensative).