User Tools

Site Tools


courses:intro.prototyping.fall.2011.nov24

Serial communication, Arduino - Processing

Sensors
  • Most analog sensors have 2 or 3 pins/connectors
  • If 3 pins, straightforward: Ground - , Power +3.3/5V , Signal/Data
  • If 2 pins, have to build a small pull-up/pull-down circuit to avoid short-circuit and adjust the sensor range.
  • interactive.usc.edu_membersmedia_npashenkov_flexsensor.jpg
    image from USC
  • See this page for a longish explanation of the voltage divider for analog sensors.
Arduino Sketch to send data to Processing
int analogPin = 0;
int analogValue = 0;
 
void setup(){
  // start serial port at 9600 bps:
  Serial.begin(9600);
}
 
void loop(){
  // read analog input, divide by 4 to make the range 0-255:
  analogValue = analogRead(analogPin); 
  Serial.println(analogValue);
  // pause for 10 milliseconds:
  delay(50);
}
Processing sketch to recieve from serial
import processing.serial.*;
 
int lf = 10;    // Linefeed in ASCII
int value;
String myString = null;
Serial myPort;  // The serial port
 
void setup() {
  size(800,600);
  background(0);
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
}
 
void draw() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
      myString = trim(myString);
      value = int(myString);
      println(value);
    }
  }
  background(0);
  rect(width/2, height, 100, -value/2); 
}

Now you have the basic tool to send a value from Arduino and recieve it in Processing. This means you now have the tool to influence anything you played around with in Processing (text, images, sound, filters, etc) with whatever input you put on Arduino (buttons, distance/light/pressure-sensors, knobs, etc).

Try to build an etch-a-sketch, a digital photoalbum, or a game. And can you figure out how you would do to send several values at the same time from Arduino and recieve it in Processing? A hint: it involves substring on the Processing side.

Simple way to change one value to integer:
      myString = trim(myString); //Trim any spaces from the sides of the string
      myInt = int(myString); //Actually converting the string value into an integer

When you want to send several values (as with the joystick) you need to put some kind of character inbetween the values to make out what is what. In this case I use “|”.

A way to recieve TWO values and change them to integers:
      dividerIndex = myString.indexOf('|'); //at what position in the string is my divider?
 
      stringValueX = myString.substring(0,dividerIndex); //get value before divider
      stringValueY = myString.substring(dividerIndex+1); //get value after divider
 
      stringValueX = trim(stringValueX); //trim away crap
      stringValueY = trim(stringValueY); //trim away crap
 
      myValueX = int(stringValueX); //convert the string value into an integer
      myValueY = int(stringValueY); //convert the string value into an integer
 

Remember that you can use map() to translate any input values to match a different set of numbers as pixels, color or frequency.

If you have 64 bit windows 7 you might find that you don't get any values to Processing. A sure sign is that when you start your sketch theres a printout along the lines of “library mismatch”. This is because Processing came with a serial library that doesn't play well with 64 bit. Have no fear. To correct this, do the following:

goto http://rxtx.qbang.org/wiki/index.php/Download 
 
   and download rxtx-2.1-7-bins-r2.zip from "Binary" column.
 
goto C:\Users\Downloads\rxtx-2.1-7-bins-r2.zip\rxtx-2.1-7-bins-r2 
 
   and copy RXTXcomm.jar to 
 
   C:\Program Files\processing-1.5.1\java\lib\ext
 
goto C:\Users\Downloads\rxtx-2.1-7-bins-r2.zip\rxtx-2.1-7-bins-r2\Windows\i368-mingw32
 
   and copy both rxtxParallel.dll and rxtxSerial.dll to
 
   C:\Program Files\processing-1.5.1\java\bin

If you run into the same thing on OSx, try this:

dont use the RXTXcomm.jar and librxtxserial.jinlib thats with processing. copy the versions that are in your arduino folder under (contents/resources/java) from arudino -22 they are dated 24th dec and are 2.1-7 - ensure these versions are in the processing folder , the arduino folder you just copied them from (obviously they will be the right ones) and under macintosh hd/library/java/extensions

courses/intro.prototyping.fall.2011.nov24.txt · Last modified: 2013/03/06 05:11 by 130.239.234.40