Fast digitalRead & serial send to Processing

Hello community!

I need to read simple digital output from 4xxx CMOS logic family at audio rate (let's say at least 18khz), send them via serial port to Processing and visualize it (plotting pixels to lines as black and white pixels).

Problem is that something is terribly slow. I was searching on various websites how to solve this and where could be the problem, but seems like everybody is using Arduino just for reading analog inputs, which is kinda slow from what I understood, but digitalRead should be hell lot faster. Also, I consider operations introduced in code to be so simple, that it should run very, very fast.

Here is Arduino Code

#define data 8 // input pin for reading data

byte number;  // byte used for bitpacking
byte numberCounter = 0; // counter for writing data in correct position in byte

void setup(){
  pinMode(data, INPUT);
  Serial.begin(115200); // Highest possible baud rate supported by Processing and Arduino, does not help at all 

}

void loop(){
  if(numberCounter > 7){ //check for position in data byte
    Serial.write(number); //send byte via serial to Processing
    numberCounter = 0; // reset counter
  }
    bitWrite(number, numberCounter, digitalRead(data)); // read digital input and save result as bit in byte
    numberCounter++; //move position in byte

}

Well, my speculations at this point are that maybe serial communication takes maybe more time than I thought and lot of data is slipping while Arduino is sending data via serial. Also, I have no idea how fast is Arduino checking numberCounter for position.

Here is Processing code:

import processing.serial.*;  
Serial port;    
int x, y = 0; // these are coordinates for pixels
char inByte; // input byte
String number; //string for converted binary number

PGraphics out; // initialize off-screen graphics buffer
void setup() {

  println(Serial.list());
  port = new Serial(this, Serial.list()[0], 115200); // open serial port at highest baud rate as possible
  out = createGraphics(1000, 1000, JAVA2D);     // setting for off-screen image buffer
  out.beginDraw();   //begin drawing to buffer
}


void draw() {
   while (port.available () > 0) {
    inByte = port.readChar(); //read byte 
    number = binary(inByte); //convert to binary string
    number = number.substring(8); // cut first 8 digits in string, because they are just zeros, as Processing has different definition of char type
    //println(number); //used for debugging
    for( int i = 0; i < 7; i++){    // go through binary sequence
      if(number.charAt(i) == '1'){ // if digit is 1, change color to white
        out.stroke(255); 
      }
      else{
        out.stroke(0);    // if digit is 0, change color to black
      }
      if(x == out.width){  // control pixel position
        x=0;
        y++;
      }
      if(y== out.height){ // if last line of image is written
        writeImage(); // write image buffer to file
      }
      out.point(x, y);
      x++; // move pixel by one to the right
    }
    
  }
  
}

void writeImage(){
  out.endDraw();
  out.save("out.jpeg");
  y=0;
  println("frameSaved!");
}

Well, Java is slow maybe. Receiving may be interrupted from drawing pixels to off screen buffer. I but serial connection should have some kind of buffer from what I read on the on various website.

I have no clue where is problem. And the main problem is that I need this working for my semester final work next week :slight_smile:

Can anybody help me?

--- edit ---

Here is what I have now. It took about 10 seconds to render, which is rather strange, because it was stream of square waves (like staccato synth tones).

The draw function in Processing is like the loop function on the Arduino, except that loop() is called over and over as fast as possible, while draw() is called over and over, so many times per second, with pauses in between, if needed.

You can change how many times per second draw() is called. Look for information about frame rate.

That might be all that is needed to speed things up. Bear in mind that serial data transmission, even at 115200 is not terribly fast.