sending Analog values from Arduino into Processing via Serial Library

edited December 2015 in Arduino

I am stuck trying to send 3 analog values (potentiometer inputs) from arduino into processing. the values are 0-1023 (10bit)... I cant figure out how to do it.. as a test sketch I tried this for the arduino side:

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

void loop()
{
  int a = -11023;
  int b = 564;
  int c=analogRead(0);
   Serial.println(a);
   Serial.print(",");
   Serial.print(b);
   Serial.print(',');
   Serial.print(c);
   Serial.print(',');
  delay(500);
}

and this for processing:

    import processing.serial.*;
    Serial myPort;
    int value;

    void setup()
    {
      myPort = new Serial(this, Serial.list()[0], 9600);
    }

    void draw()
    {
    }

    void serialEvent(Serial p) {
      // get message till line break (ASCII > 13)
      String message = myPort.readStringUntil(',');
      if(message != null){
        value = int(message);
        println(value);
      }
    }

All I get are zeros on the serial monitor... The idea is to ultimately send data to processing in groups of 3 (for the 3 sensors) and assign them to 3 variables in processing but I can see Im not grasping the concept of serial transmission :(

Answers

  • Answer ✓

    Hi, http://www.arduino.cc/en/Tutorial/VirtualColorMixer watch the processing code just below commented or in arduino ide: file-->example-->communication--> VirtualColorMixer and see the processing sketch

  • edited May 2015 Answer ✓

    Dunno whether that's the problem, but you're not using serialEvent()'s parameter -> p.
    Rather the "global" variable myPort inside serialEvent(). :|
    Actually AFAIK, you can get rid of that useless myPort variable from your sketch! =;
    Lest not to get into temptation of using that in the wrong places: :-\"

    // forum.processing.org/two/discussion/10452/
    // sending-analog-values-from-arduino-into-processing-via-serial-library
    
    // 2015-Apr-22
    
    import processing.serial.Serial;
    
    int[] readings;
    
    void setup() {
      noLoop();
      frameRate(4);
      new Serial(this, Serial.list()[0], 9600).bufferUntil(ENTER);
    }
    
    void draw() {
      println("Frame: #", frameCount, ':');
      println(readings);
      println();
    }
    
    void serialEvent(Serial s) {
      readings = int(trim(split(s.readString(), ',')));
      redraw = true;
    }
    
Sign In or Register to comment.