Arduino to Porcessing

edited July 2014 in Arduino

Hi im really new in Processing and Arduino. I made a project for University last semester, we didnt had enough time in the end so our teachers helped us with the program. And now it doent work anymore :/ my projekt is like a drum kit with 64 buttons. the arduino part works good, i think the problem is my processing sketch doesnt read the serial right, and like i said i have no clue what i can do now maybe u can help me. Processing receives in serial something like "Button: Button: 1 0 1" for example the first number is row the second column and the last is 0 or 1 for on or off. Would be nice if somebody can help me.

Greetings dom

/E. sorry for the messy copy. dont know how to fix this

//Here is the problem i think

void serialEvent(Serial p) { try {

// get message till line break (ASCII > 13)
String message = p.readStringUntil(13);
// just if there is data
if (message != null) {
  println(message);
  // try catch function because of possible garbage in received data
  String[] elements = splitTokens(message);
  // loop through sensor values
  if(elements[2].equals("0"))
    matrix[parseInt(elements[0])][parseInt(elements[1])] = false;
  if(elements[2].equals("1"))
    matrix[parseInt(elements[0])][parseInt(elements[1])] = true;

}

} catch (Exception e) { } }

//

Answers

  • edited July 2014

    In order to have some code block displayed properly in this forum, we gotta highlight it and hit CTRL+K!

    Since Serial doesn't work for me and neither got an Arduino myself I can't help ya!
    Nevertheless, gonna leave ya w/ some points:

    • Rather than using magical numbers like 13, it's much preferred meaningful constants.
    • For example, instead of 13, use '\r' or RETURN.
    • Perhaps you should also consider '\n' or ENTER in case '\r' ends up failing ya down.
    • Perhaps an extra println() for elements could help ya out spotting further bugs.

    A futile attempt of mine below:

    // forum.processing.org/two/discussion/6558/arduino-to-porcessing
    
    import processing.serial.Serial;
    
    static final int ROWS = 10, COLS = 5;
    final boolean[][] matrix = new boolean[ROWS][COLS];
    
    void serialEvent(Serial p) {
      String msg = p.readStringUntil('\r');
      //String msg = p.readStringUntil(RETURN);
    
      //String msg = p.readStringUntil('\n');
      //String msg = p.readStringUntil(ENTER);
    
      println(msg);
      if (msg == null)  return;
    
      int[] elems = int(splitTokens(msg));
    
      println(elems);
      //printArray(elems);
      if (elems.length < 3)  return;
    
      int r = abs(elems[0]), c = abs(elems[1]);
      if (r < ROWS & c < COLS)  matrix[r][c] = elems[2] != 0;
    }
    
Sign In or Register to comment.