Receive Multiple Sensor Information from Arduino to Processing

edited July 2014 in Arduino

Hello all,

I am new to Processing. Currently I am working with a simple ultrasonic distance sensors and with Arduino Mega. The question is:

How can I receive mutiple sensor information at every instance of time in processing from Arduino side it transmits via serial port. I worked with an example of single sensor and it works fine. But how about multiple sensors via serial communication.

Kindly let me know. Thanks in advance.

Answers

  • Answer ✓

    The basic idea I use is to print from the Arduino a line of tab-separated values from the sensors:

    int count = 6;
    int val = 0;
    
    void setup()
    {
      Serial.begin(9600);
    }
    
    void loop()
    {
      for (int i = 0;i<count;i++)
      {
        val = analogRead(i);
    
        Serial.print(val);
        Serial.print("\t");
      }
      Serial.println();
    }
    

    and here is the Processing code to read those values:

    // serialEvent method is run automatically
    // whenever the buffer receives a linefeed byte 
    
    void serialEvent(Serial myPort) 
    { 
      // read the serial buffer:
      String myString = myPort.readStringUntil(linefeed);
    
      // if we get any bytes other than the linefeed:
      if (myString != null) 
      {
        // remove the linefeed
        myString = trim(myString);
    
        // split the string at the tabs and convert the sections into integers:
        int mysensors[] = int(split(myString, '\t'));
        count = mysensors.length;
    
        for (int i=0; i<count; i++)
        {
          // set sensor[i] value for use in draw()
          sensors[i].value = mysensors[i];   <--- you'll need to change this line to put
                                                     the values where you want them
          // print out the values we got:
          print(i + ": " + sensors[i].value + "\t");
        }
        println();
      }
    }
    

    The line you'll need to change will put the values into a global array where the main body of your program can read them. In my case it's the sensors[] array of Sensor objects I created.

    HTH...

  • Thank you very much for your quick response... Now I can transfer multiple sensor information from my Arduino to Processing and visualize them.... :)>-

Sign In or Register to comment.