Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 19-8: Reading from serial port

import processing.serial.*;

int val = 0; // To store data from serial port, used to color background
Serial port; // The serial port object

void setup() {
  size(200,200);
  
  // In case you want to see the list of available ports
  // println(Serial.list());
  
  // Using the first available port (might be different on your computer)
  port = new Serial(this, Serial.list()[0], 9600); 
}

void draw() {
  // The serial data is used to color the background.   
 background(val); 
}

// Called whenever there is something available to read
void serialEvent(Serial port) {
  // Data from the Serial port is read in serialEvent() using the read() function and assigned to the global variable: val
  val = port.read();
  // For debugging
  // println( "Raw Input:" + val);
}
  • Jessica

    I tried to get two computers to talk to each other through the serial port using Processing. However, It hasn’t worked. I tried this book and you had this example. I couldn’t find where the +input was coming from for the debugging statement inside the serialEvent(). I don’t think that the serialEvent() function is getting called. If you have any tips on this, that would be great.

  • http://www.learningprocessing.com Daniel Shiffman

    You may want to consider looking at the networking client/server examples for communication between machines as well. Otherwise, it’s hard to know why it isn’t working, perhaps use a console / terminal to make sure your serial ports and working and data is flowing before wondering if it’s an issue with your Processing code.

  • Logictom

    I believe
    // println( “Raw Input:” + input);
    in serialEvent should be:
    // println( “Raw Input:” + val);
    Unless input is some Prcoessing defined variable? (Only been using Processing < 1hr :P )

  • http://www.learningprocessing.com Daniel Shiffman

    Whoops, that’s right! Going to correct it now, thank you!

  • Talib

    hello
    i hope u can help me how to read and save from serial port from the first step.

  • ersinful

    Hi,
    how can i send two variables from arduino to processing ?

  • timshi thakkar

    #include

    #define MEM_SIZE 512

    int trigger_value; // pressure reading threshold for identifying a bike is pressing.

    int threshold = 200; //change this amount if necessary. tunes sensitivity.

    int the_tally; //total amount of sensings.

    int incomingByte = 0; // for incoming serial data

    int the_time_offset; // in case of power out, it starts counting time from when the power went out.

    int latest_minute;

    int the_wheel_delay = 50; //number of milliseconds to create accurate readings for cars. prevents bounce.

    int car_timeout = 3000;

    long the_wheel_timer; //for considering that double wheel-base of cars, not to count them twice.

    int the_max = 0;

    int is_measuring = 0;

    int count_this = 0;

    int strike_number = 0;

    float wheel_spacing = 2.7500; //average spacing between wheels of car (METERS)

    float first_wheel = 0.0000000;

    float second_wheel= 0.0000000;

    float wheel_time = 0.0000000;

    float the_speed = 0.0000000;

    int time_slot;

    int speed_slot;

    int all_speed;

    void setup() {

    pinMode(A3, INPUT);

    pinMode(13, OUTPUT);

    pinMode(A2,OUTPUT);

    pinMode(A4,OUTPUT);

    digitalWrite(A4,LOW);

    digitalWrite(A2,HIGH);

    Serial.begin(9600);

    //EEPROM.read(0, the_tally); //the tally is stored in position 0. assigns the read value to ‘the_tally’.

    //EEPROM.read((the_tally*2)+1, the_time_offset); //read the last time entry

    if(the_tally trigger_value) {

    if (strike_number == 0 && is_measuring == 0) { // FIRST HIT

    Serial.println(“”);

    Serial.println(“Car HERE. “);

    first_wheel = millis();

    is_measuring = 1;

    }

    if (strike_number == 1 && is_measuring == 1) { // SECOND HIT

    Serial.println(“Car GONE.”);

    second_wheel = millis();

    is_measuring = 0;

    }

    }

    //2 – TUBE IS STILL PRESSURIZED

    while(analogRead(A3) > the_max && is_measuring == 1) { //is being pressed, in all cases. to measure the max pressure.

    the_max = analogRead(A2);

    }

    //3 – TUBE IS RELEASED

    if (analogRead(A3) the_wheel_delay)) {

    strike_number = 1;

    }

    if (strike_number == 1 && is_measuring == 0 && (millis() – second_wheel > the_wheel_delay) ){

    count_this = 1;

    }

    }

    //4 – PRESSURE READING IS ACCEPTED AND RECORDED

    if ((analogRead(A3) car_timeout) && is_measuring == 1))

    { //has been released for enough time.

    the_tally++;

    EEPROM.write(0, the_tally); //puts the value of x at the 0 address.

    digitalWrite(13,HIGH);

    time_slot = the_tally*2;

    speed_slot = (the_tally*2)+1;

    Serial.print(“Pressure Reached = “);

    Serial.println(the_max);

    Serial.print(“Current Count = “);

    Serial.println(the_tally);

    Serial.print(“time between wheels = “);

    wheel_time = ((second_wheel – first_wheel)/3600000);

    Serial.println(wheel_time);

    int time = ((millis()/1000)/60) + the_time_offset + 1; // the number of seconds since first record.

    Serial.println(“time passed =”);

    Serial.println(time);

    Serial.println(millis()/1000);

    EEPROM.write(time_slot, time); //puts the value of y at address ‘the_tally’.

    the_speed = (wheel_spacing/1000)/wheel_time;

    if (the_speed > 0 ) {

    Serial.print(“Estimated Speed (km/h) = “);

    Serial.println(the_speed);

    EEPROM.write(speed_slot, int(the_speed)); //puts the value of y at address ‘the_tally’.

    }

    else {

    Serial.println(“Speed not measureable”);

    EEPROM.write(speed_slot, 0); //puts the value of y at address ‘the_tally’.

    }

    //RESET ALL VALUES

    the_max = 0;

    strike_number = 0;

    count_this = 0;

    is_measuring = 0;

    digitalWrite(13,LOW);

    } //4 ends

    if (Serial.available() > 0) {

    // read the incoming byte:

    incomingByte = Serial.read();

    if (incomingByte == ’1′) {

    print_memory();

    }

    if (incomingByte == ’2′) {

    Serial.println(“”);

    Serial.println(“ARE YOU SURE YOU WANT TO ERASE THE MEMORY? Enter Y/N”);

    }

    if (incomingByte == ‘N’ || incomingByte == ‘n’) {

    Serial.println(“MEMORY ERASE CANCELLED”);

    Serial.println(“___________________________________________________”);

    }

    if (incomingByte == ‘Y’ || incomingByte == ‘y’) {

    erase_memory();

    print_memory();

    }

    if (incomingByte == ’3′) {

    process();

    }

    //if ends

    } //serial available ends

    } //loop ends

    void print_memory() {

    if (the_tally > 0) {

    Serial.println(“”);

    Serial.println(“Count , Time (Minutes) , Speed (km/h)”);

    for (int i=1; i<= the_tally; i++){

    Serial.print(i);

    Serial.print(" , ");

    long y = EEPROM.read(2*i);

    Serial.print(y);

    Serial.print(" , ");

    long z = EEPROM.read((2*i)+1);

    Serial.println(z);

    all_speed = (all_speed+z); //add all the speeds together to find average.

    latest_minute = y;

    } //for ends

    }//if ends

    Serial.println("");

    Serial.print("Total Cars, ");

    Serial.println(the_tally);//read memory

    Serial.print("Total Minutes Measured, ");

    Serial.println(latest_minute);

    Serial.print("Traffic Rate (cars per min), ");

    if ((the_tally/latest_minute) <= 0) {

    Serial.println("0");

    }

    else {

    Serial.println(the_tally/latest_minute);

    }

    Serial.print("Average Car Speed (km per hour), ");

    if ((all_speed/the_tally) <= 0) {

    Serial.println("0");

    }

    else {

    Serial.println(all_speed/the_tally);

    }

    Serial.println("___________________________________________________");

    } //print memory ends

    void erase_memory() {

    //erase current tally

    Serial.println("");

    Serial.println("ERASING MEMORY …");

    for (int i = 0; i <= MEM_SIZE; i++){

    EEPROM.write(i, 0);

    }

    the_tally = 0;

    the_time_offset = 0;

    latest_minute = 0;

    }

    void process()

    {

    //Serial.println("Count , Time (Minutes)");

    for (int i=1; i<= the_tally; i++){

    Serial.println(i);

    long y = EEPROM.read(2*i);

    Serial.println(y);}

    }

    this is my program for cars counting …

    import processing.serial.*;

    Serial myPort; // The serial port

    int xPos = 1;

    int the_tally; //data received from serial port

    void setup () {

    // set the window size:

    size(400, 300);

    myPort = new Serial(this, "COM3", 9600);

    // don't generate a serialEvent() unless you get a newline character:

    myPort.bufferUntil('n');

    // set inital background:

    background(0);

    }

    void draw () {

    // everything happens in the serialEvent()

    }

    void serialEvent (Serial myPort) {

    for(int i=1;i= width) {

    xPos = 0;

    background(0);

    }

    else {

    // increment the horizontal position:

    xPos++;

    }

    }

    }

    this is my processing code… but its noit displaying anything on the window can you help me out? i want to display no.of cars vs time….