Pages: [1]   Go Down
Author Topic: Fast digitalRead & serial send to Processing  (Read 1046 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
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:
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 smiley

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).



* out.jpeg (123.15 KB, 1000x1000 - viewed 18 times.)
« Last Edit: May 30, 2012, 08:32:28 am by sphere42 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45973
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pages: [1]   Go Up
Jump to: