I have connected the arduino to an audio lead (Stereo 2 channels) with a capacitor and a 1.5V AA battery so the whole waveform gets read through analog pins 0 and 1 of my arduino mega. This data is then sent to proceessing which then draws a graph of the waveform. When the processing sketch is running the data sent is correct ie. below:
then when my processing app starts running the serial messages glitch as below:
and the graph glitches with the data looking at the right channel(Orange) as left channel(Purple) hasnt been calibrated
The code from the arduino is:
if(Serial.available()) {
allOn();
int vl = analogRead(lStereo);
int vr = analogRead(rStereo);
Serial.print(vl);
Serial.print(",");
Serial.print(vr);
Serial.println(",");
}
and processing code is :
PFont font;
import processing.serial.*;
Serial myPort;
float lStereo;
float rStereo;
int xPos = 50;
int xPosOld = 50;
int on = 50;
float lStereoOld;
float rStereoOld;
void setup () {
size(1280,800);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 115200);
myPort.bufferUntil('\n');
background(100);
fill (0);
stroke(255,255,255);
rect(20, 30, 1250,350);
line(50, 350, 1250, 350);
line(50, 350, 50, 50);
fill(127,34,255);
font = loadFont("Times-Bold-24.vlw");
textFont (font);
text("Left", 35, 375);
fill (0);
stroke(255,255,255);
rect(20, 430, 1250,350);
line(50, 750, 1250, 750);
line(50, 750, 50, 450);
fill(255,59,0);
font = loadFont("Times-Bold-24.vlw");
textFont (font);
text("Right", 35, 772);
}
void draw () {
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
float[] stereo = float(split(inString, ","));
if(stereo.length >= 2)
{
lStereo = map(stereo[0], 0,1, 0, height);
rStereo = map(stereo[1], 100, 500, 0, height);
}
stroke(127,34,255);
line(xPosOld, (height/2) - 50 - lStereoOld, xPos, (height/2) - 50 - lStereo);
stroke(255,59,0);
line(xPosOld, height - 50 + 312 - rStereoOld, xPos, height - 50 - rStereo + 312);
myPort.write("on");
lStereoOld = lStereo;
rStereoOld = rStereo;
xPosOld = xPos;
if (xPos >= 1250) {
xPos = 50;
xPosOld = 50;
background(100);
fill (0);
stroke(255,255,255);
rect(20, 30, 1250,350);
line(50, 350, 1250, 350);
line(50, 350, 50, 50);
fill(127,34,255);
font = loadFont("Times-Bold-24.vlw");
textFont (font);
text("Left", 35, 375);
fill (0);
stroke(255,255,255);
rect(20, 430, 1250,350);
line(50, 750, 1250, 750);
line(50, 750, 50, 450);
fill(255,59,0);
font = loadFont("Times-Bold-24.vlw");
textFont (font);
text("Right", 35, 772);
}
else {
xPos = xPos +2;
myPort.write(on);
}
}
}
Thanks in advance