Reading Arduino Serial Data

edited September 2014 in Arduino

Hi Everyone! I'm a bit new to Processing and Arduino, so I apologize if this is basic task whose answer I may have missed.

What I'm trying to do is read in a continuous stream of data from a tachometer circuit I made with Arudino, and then feed it into Processing; which I've successfully done using the code below:

if (0 < port.available()) { val = port.read(); println(val); }

What I'm not sure how to do is process the data so that whenever a certain value is detected, an event will occur in Processing.

Thank you! Nick

Answers

  • Answer ✓

    Dunno! Perhaps something like this 1? :-/

    // forum.processing.org/two/discussion/6894/
    // reading-arduino-serial-data
    
    import processing.serial.*;
    Serial port;
    int val;
    
    static final int CERTAIN_VALUE = 100;
    
    void draw() {
      if (port.available() > 0)  decideWhatToDo(val = port.read());
    }
    
    boolean decideWhatToDo(int val) {
      switch (val) {
      case CERTAIN_VALUE : 
        return doCertainThing();
      }
    
      return false;
    }
    
    boolean doCertainThing() {
      return true;
    }
    
  • Hey there! Thanks for your answer. That's sort of what I ended up trying. I put my code below...perhaps that will help explain what I am trying to do (I should have done that much sooner!)

    import processing.video.*; 
    import processing.serial.*; 
    
    
    Serial port; 
    Movie myMovie; 
    double val = 0; 
    
    void setup() 
    {  
     //create screen
     size(320, 240); 
     background(0); 
     //load movie
     myMovie = new Movie(this, "countdown.mov"); 
     myMovie.loop(); 
    
     // print a list of all available ports 
     println(Serial.list()); 
    
     // choose the port to which the Arduino is connected 
     // on the PC this is usually COM1, on the Macintosh 
     // this is usually tty.usbserial-XXX 
     port = new Serial(this, "/dev/tty.usbmodem1411", 9600); 
     port.bufferUntil('\n');  
    }    
    
    
     void draw() { 
     if (0 < port.available()) { 
     String strData = port.readStringUntil('\n'); // string representation of value
     val = Double.parseDouble(strData); // double from string data
    }
    
     image(myMovie, 0, 0); 
    }
    
    
     void movieEvent(Movie m) {
     m.read();
     if (val == 0) { 
     myMovie.speed(0); 
     } 
     else if (val >= 3600) 
      myMovie.speed(1); 
     else { 
     myMovie.speed(0); 
     } 
    }
    

    Basically, I'm using an Arduino Uno to calculate the speed of a computer fan. If the fan stays at 3600 rpm, then I want a movie to play. If it drops below that, I want the movie to stop playing. My Arduino sketch is working (I'm able to read in the data fine on the serial port), but for some reason I can't do that with Processing; no data appears to be coming in. I based this off of other serial read examples I found, but nothing seems to work yet.

  • It's difficult to debug without seeing the Arduino side of the code as well.

    Two things to check: 1. All other serial monitors and applications reading in from that port are closed (only one application can read from a serial port at a time). 2. Your data from the Arduino actually terminates with a \n, so it should be using Serial.println not Serial.print.

    Additionally, have you gotten the other serial examples that you've found working?

  • edited September 2014

    Right, how silly....here's the Arduino code I'm using:

    //// This example shows one way of creating an optoswitch
    //// using an IR LED as emitter and an IR LED receiver as
    //// light sensor.
    ////
    ////           + GROUND                                 +GROUND          
    ////           |                                        |  
    ////           <                                        < 
    ////           > 220 ohm resistor                       > 220 ohm resistor
    ////           <                                        <      
    ////           |                                        |  
    ////           |                                        |
    ////         -----                                    -----
    ////          / \    >>IR LED emitter >>>>>>>>>>>>>>>>  / \   IR LED receiver
    ////         -----                                    -----
    ////           |                                        |
    ////           |                                        |
    ////           + +5VCD                                  +  ANALOG INPUT 0
    ////
    ////
    ////
    ////http://playground.arduino.cc/Learning/Tachometer
    
    
    int val;
    long last=0;
    int currentStatus=LOW;
    int previousStatus=LOW;
    int count=0;
    
    int sens=85;  // this value indicates the limit reading between dark and light,
                  // it has to be tested as it may change acording on the 
                  // distance the leds are placed.
    int nSpokes=7; // the number of blades of the wheel
    
    int milliseconds=500; // the time it takes each reading
    
    
    
    void setup()  
    {
      Serial.begin(9600);
      pinMode(13,OUTPUT);
    }
    
    void loop()
    {
      val=analogRead(0);
      if(val<sens)
        currentStatus=LOW;
       else
        currentStatus=HIGH;
       digitalWrite(13,currentStatus); //as iR light is invisible for us, the led on pin 13 
                              //indicate the state of the circuit.
    
       if(previousStatus!=currentStatus){  //counts when the state changes from (dark to light) or 
                         //from (light to dark), remmember that IR light is invisible for us.
         count++;
         previousStatus=currentStatus;
       }
       if(millis()-last>=milliseconds){
         double rps=((double)count/nSpokes)/2.0*1000.0/milliseconds;
         double rpm=((double)count/nSpokes)/2.0*60000.0/(milliseconds);
    //     Serial.print((count/2.0));Serial.print("  RPS ");Serial.print(rps);
    //     Serial.print(" RPM");
    //     Serial.print(rpm);
    //     Serial.print("  VAL ");Serial.println(val); 
         Serial.println(rpm);
         count=0;
         last=millis();
       }
    }
    

    In regards to your suggestions: 1. Yes, I don't have any other serial applications open. 2. I did try changing my Arduino code from 'Serial.print' to 'Serial.println', but it didn't seem to have any effect.

    And yes, I did get the following example to work fine; it reads in data from a potentiometer on pin 0.

    /*
      AnalogReadSerial
      Reads an analog input on pin 0, prints the result to the serial monitor.
      Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
    
     This example code is in the public domain.
     */
    
    // the setup routine runs once when you press reset:
    void setup() {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
    }
    
    // the loop routine runs over and over again forever:
    void loop() {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A2);
      // print out the value you read:
      Serial.println(sensorValue);
      delay(1);        // delay in between reads for stability
    }
    

    edit: @theleadingzero, The other serial example that I got to work was the 'SerialEvent' sketch included under File/Examples/Communication.

Sign In or Register to comment.