Attached is the processing sketch used to read out the data from the ILX511 CCD running on the Arduino Mega.
boolean myDebug = false;
int sampleBufferSize = 2201; // The sample buffer
int[] sampleBuffer = new int[sampleBufferSize + 1]; // Samples
int sampleCount = 0; // Sample position counter
int timeout = 0; // Used for timeout counter while waiting for Serial data
import processing.serial.*;
Serial arduinoSerial; // The serial port
void setup() {
if (myDebug)
println(Serial.list()); // List all the available serial ports
else {
//arduinoSerial = new Serial(this, Serial.list()[0], 230400);// Open the port you are using at the rate you want:
arduinoSerial = new Serial(this, Serial.list()[0], 115200);// Open the port you are using at the rate you want:
arduinoSerial.clear(); // Flush serial buffers
}
println("*******************************");
println("* -= ILX511 Scanner v5 MEGA=- *");
println("*******************************");
if (!myDebug)
delay(500); // Delay some to allow scanner to reboot and start sending serial data
if (myDebug) { // Put some data in the buffer if debugging
for (int x=0; x < sampleBufferSize; x++) {
sampleBuffer[x] = x & 255;
}
}
size((sampleBufferSize+10)/2, 300); // Display size just wider than sample buffer size
frameRate(15); // Reduce framerate to save CPU load
}
void serialEvent(Serial arduinoSerial) {
if (millis() > timeout + 50) { // Timeout if no serial received for 50 milliseconds or more
//print("Timeout ");
//println(millis());
sampleCount = 0; // Timout so reset counter
}
sampleBuffer[sampleCount++] = arduinoSerial.read(); // Add value to buffer
if (sampleCount > sampleBufferSize) { // End of buffer?
print("Overflow ");
println(millis());
sampleCount = 0; // zero it
}
timeout = millis(); // Get current time
}
void draw() {
int y = 0;
background(180); // Clear to background colour
doScale(); // Paint on the scale
stroke(255, 255, 0); // Set pen colour
for (int x = 0; x < sampleBufferSize; x++) { // Draw the buffer data
if ((x % 2) == 0) {
y = sampleBuffer[x] * 256;
}
else {
y = y + sampleBuffer[x];
//line((x/2)+5, sampleBuffer[x], (x/2)+5, 280);
//point((x/2)+5, sampleBuffer[x]/2);
point((x/2)+5, 280 - (y / 4));
}
}
}
void doScale() {
stroke(0);
line(0+5, 280, 0+5, 280 - 255);
line(16+5, 280, 16+5, 280 - 255);
line(1040+5, 280, 1040+5, 280 - 255);
line(1043+5, 280, 1043+5, 280 - 255);
}