Serial write command to arduino works in mouse event methods but not in the draw() method.....

edited May 2014 in Arduino

Hey everyone, I've been banging my head against the keyboard trying to figure this one out, I'm fairly new to processing so maybe I'm overlooking something!

So far from arduino TO processing I've had no problems, followed some tutorials and everything behaved as it should have. I read serial messages in the processing sketch inside of the draw() method.

So, I try and do the reverse (what I'm ultimately trying to do is sending frame data to led's connected to the arduino) and use something like this:

// -psuedocode-

void draw() {
myPort.write(someValueArrayHere);
}

This does not work, it does cause the arduino's RX light to flash every frame as it should, but the led's do not update... What is even more strange, is if I use an event, say mousePressed() like this:

// -psuedocode-

void draw() {
  Some drawing stuff here
}

void mousePressed() {
myPort.write(someValueArrayHere);
}

it DOES work, and of course it only works when I click the mouse which isn't what I need, as I want the serial data to get sent as often as possible to maximize frame rate for the video.

I've found several examples and tutorials that suggest writing serial data in the draw loop, but but it doesn't totally work for me.

Is there another sort of event that happens every frame after or before draw that I could use? Am I doing something wrong that might keep it from working in draw()?

Here's my full code:

import codeanticode.syphon.*;
import processing.serial.*;
Serial port;

int rows = 8;
int columns = 8;
int dataChunkSize = 3;

byte[] vals = new byte[rows * columns * dataChunkSize];
int rowSize, columnSize;

PGraphics canvas;
SyphonClient client;

public void setup() {
  size(256, 256, P3D);

  rowSize = width/rows;
  columnSize = height/columns;

  String portName = Serial.list()[0];
  port = new Serial(this, portName, 115200); //115200

  println("Available Syphon servers:");
  println(SyphonClient.listServers());
  client = new SyphonClient(this, "Simple Server");
}

void draw() {    
  if (client.available()) {
    canvas = client.getGraphics(canvas);
    image(canvas, 0, 0, width, height);
    loadPixels();
  }
}


void mouseMoved() {
  int pixelCounter = 0;
  for (int y = 0; y < columns; y++) {
    for (int x = 0; x < rows; x++) {
      int x_loc = x*rowSize + rowSize/2;
      int y_loc = y*columnSize + columnSize/2;
      int loc = x_loc + y_loc*width;

      color c = pixels[loc];
      int r = (int) (((c >> 16) & 0xFF) * .3);
      int g = (int) (((c >> 8) & 0xFF) * .3);
      int b = (int) ((c & 0xFF) * .3);

      vals[pixelCounter*dataChunkSize] = (byte) r;
      vals[pixelCounter*dataChunkSize+1] = (byte) g;
      vals[pixelCounter*dataChunkSize+2] = (byte) b;
      pixels[loc] = color(255,0,0);
      pixelCounter += 1;
    }
  }
  port.write(vals);
}

Answers

  • if you have Arduino mega try... put a delay in the setup:

     String portName = Serial.list()[0];
      port = new Serial(this, portName, 115200); //115200
    delay (2000);
    
  • Wow... that was 100% it. Working beautifully now thank you so much for that piece if advice!

Sign In or Register to comment.