Go Down

Topic: Array of values Arduino2Processing via Serial (Read 832 times) previous topic - next topic

noidsign

Hi everyone,

I am trying to send via serial the state of some buttons from the Arduino to Processing.
What I get in the serial monitor in Arduino and in Processing is correct, but I am not able to use the information coming from Arduino in Processing. Let me show the code and explain further more below:

Arduino code:
Code: [Select]

// constants won't change. They're used here to
// set pin numbers:
int buttonPins[] = {
  2,4,7,8,12}
;     // the number of the pushbutton pin
int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonStates[5];         // variable for reading the pushbutton status
int i;
void setup() {
  // initialize the pushbutton pin as an input:
  for (i=0; i<5; i++){
    pinMode(buttonPins[i], INPUT);     
  }
  Serial.begin(9600);
}

void loop(){
  for (int i=0; i<5; i++){
    // read the state of the pushbutton value:
    buttonStates[i] = digitalRead(buttonPins[i]);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonStates[i] == HIGH) {     
      Serial.print("ON");
      Serial.println(buttonPins[i]);
      Serial.print ("VAL");
      Serial.println (buttonStates[i]);
      Serial.write (buttonStates[i]); 
    }
    else {
// do nothing
    }


  }
  delay(5000); // just for having the time to check
}



Processing code:
Code: [Select]


// Example by Tom Igoe

import processing.serial.*;

Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  // Expand array size to the number of bytes you expect
  byte[] inBuffer = new byte[5];
  while (myPort.available() > 0) {
    inBuffer = myPort.readBytes();
    myPort.readBytes(inBuffer);
    if (inBuffer != null) {
      String myString = new String(inBuffer);
      println(myString);
    }
  }
}


That's the example code in Processing. I tried a // if string == "bla bla bla" // but it doesn't work and it's also quite not good I think to go for that (easy to make mistakes).

What I need is to understand
1. if I am sending the data from the Arduino correctly, and if not how should I send it to Processing
2. how to use the data in Processing. Basically something like being able to define a
buttonStates in Processing and saying

if buttonStates[0] == 1 > do A
if buttonStates[1] == 1 > do B
if buttonStates[2] == 1 > do Z 

Thank you so much for the help!
Cheers

PaulS

Code: [Select]
// constants won't change. They're used here to
// set pin numbers:
int buttonPins[] = {
  2,4,7,8,12}
;     // the number of the pushbutton pin
int ledPin =  13;      // the number of the LED pin

None of these comments are even close to being useful. Most of them are plain wrong. Variables are not constants, without the const keyword.

Using three lines to define the array looks silly, too.

All the serial data is going to Processing. So, for one switch press, it gets "ONx<cr><lf>VALy<cr><lf>z".

Code: [Select]
  // Expand array size to the number of bytes you expect
  byte[] inBuffer = new byte[5];

So, how are those 12 values (per switch) supposed to fit in a 5 element array?

Code: [Select]
    inBuffer = myPort.readBytes();
    myPort.readBytes(inBuffer);

There are two overloads of the readBytes() method. You should be using ONE of them, not both.

You need to remove most of that stuff you are sending. Send something like "n=x<cr><lf>", where n is the pin number and x is the state for every pin, pressed or not.

In Processing, you need some major redesign. You need to use the Serial::bufferUntil() method to tell Processing to accumulate the serial data until a specific character has arrived (like the <cr> or <lf>).

You need to add a serialEvent() function. Processing will call this when the bufferUntil() character has arrived. Then, you can read the serial data using readStringUntil(), to get the whole string.

Then, you can use int() on that string to get an array of integer values from the string (containing n in the 0th position and x in the 1th position). Then, you can use those ints to do whatever you want.
The art of getting good answers lies in asking good questions.

Go Up