SendingMultipleData \ Learning \ Wiring ALPHA 1.0
Author: Hernando Barragán
Description: Wiring is an electronic sketchbook and hardware electronics for developing ideas. It is a context for learning fundamentals of computer programming and prototyping with electronics within the context of the electronic arts.
Keywords: Wiring, Processing, Interactive Media, Electronic Arts, Programming, C, C++, Hernando Barragán

Search wiring.org.co:

This example is for Wiring version 0023+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.

Data from multiple sensors / Processing by BARRAGAN
Reads values from four photoresistors connected to the analog input pins 0-3. The values read from the sensors are proportional to the amount of light that hits their surface. The values read are printed comma separated through the serial to use them in Processing

    int sens1, sens2, sens3, sens4;

    void setup() {
      Serial.begin(9600);
    }

    void loop() {
      sens1 = analogRead(0);       // read sensor in analog input 0
      sens2 = analogRead(1);       // read sensor in analog input 1
      sens3 = analogRead(2);       // read sensor in analog input 2
      sens4 = analogRead(3);       // read sensor in analog input 3
      Serial.print(sens1, DEC);    // print sensor 1
      Serial.print(",");           // print ','
      Serial.print(sens2, DEC);    // print sensor 2
      Serial.print(",");           // print ','
      Serial.print(sens3, DEC);    // print sensor 3
      Serial.print(",");           // print ','
      Serial.println(sens4, DEC);  // print sensor 4 and newline (println)
      delay(200);                  // wait 200ms for next reading
    }

    // cut the code below and paste it into Processing

    /**
     * Data from multiple sensors / Processing
     * by BARRAGAN <http://barraganstudio.com>
     * based on Tom Igoe's example from Making Things Talk book
     *
     * read serial data until a linefeed character is found
     * data are values comma separated. Split the data and convert it
     * into numbers in an array for firther use.
    */

    /*
    import processing.serial.*;

    Serial myPort;
    int linefeed = 10;   // Linefeed in ASCII
    int numSensors = 4;  // we will be expecting for reading data from four sensors
    int sensors[];       // array to read the 4 values
    int pSensors[];      // array to store the previuos reading, usefur for comparing
    // actual reading with the last one

    void setup() {
      myPort = new Serial(this, Serial.list()[1], 9600);
      // read bytes into a buffer until you get a linefeed (ASCII 10):
      myPort.bufferUntil(linefeed);
      size(200, 200);
    }

    void draw() {
      if((pSensors != null)&&(sensors != null)) {

        // if valid data arrays are not null
        // compare each sensor value with the previuos reading
        // to establish change

        for(int i=0; i < numSensors; i++) {
          float f = sensors[i] - pSensors[i];  // actual - previous value
          if(f > 0) {
            println("sensor "+i+" increased by "+f);  // value increased
          }
          if(f < 0) {
            println("sensor "+i+" decreased by "+f);  // value decreased
          }
        }

        // now do something with the values read sensors[0] .. sensors[3]

      }
    }

    void serialEvent(Serial myPort) {

      // read the serial buffer:
      String myString = myPort.readStringUntil(linefeed);

      // if you got any bytes other than the linefeed:
      if (myString != null) {

        myString = trim(myString);

        // split the string at the commas
        // and convert the sections into integers:

        pSensors = sensors;
        sensors = int(split(myString, ','));

        // print out the values you got:

        for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
          print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
        }

        // add a linefeed after all the sensor values are printed:
        println();

      }
    }

    */