User Tools

Site Tools


courses:intro.prototyping.spring.2016.nov8

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

courses:intro.prototyping.spring.2016.nov8 [2016/11/07 06:26]
rickard created
courses:intro.prototyping.spring.2016.nov8 [2016/11/08 06:02] (current)
rickard
Line 1: Line 1:
 +====== Serial communication between Arduino and Processing ======
 +
 +===Arduino transmitting ===
 +
 +Using the the Arduino example "AnalogReadSerial" we get a simple source of values to transmit.
 +
 +
 +==Processing code to receive:==
 +<code java>
 +import processing.serial.*;
 +
 +int linefeed = 10; // new line ASCII = 10
 +Serial myPort;
 + 
 +int value1 = 0; //this variable will contain the reading
 +void setup () {
 +  size(800, 600);
 +  // change port!
 +  myPort =  new Serial(this, Serial.list()[2], 9600);
 +  // here we're saying that we need to buffer until 'NEW LINE'
 +  myPort.bufferUntil(linefeed);
 +
 +}
 +
 +void draw () 
 +
 +  //do something with "value1"
 +}
 +
 +void serialEvent (Serial myPort) 
 +{
 +  // read serial buffer as string
 +  String myString = myPort.readString();
 +
 +  // if we have any other bytes than linefeed
 +  if (myString != null) 
 +  {
 +    // trim crap
 +    myString = trim(myString);
 +    value1 = int(myString); //make string to integer
 +    println(value1);
 +  }
 +}
 +</code>
 +
 +
 +== Processing recieving TWO values ==
 +
 +If we want to send more values we can have the Arduino do that with a comma between. Note the use of both print and println to make it all be one transmitted row.
 +<code java>
 +  Serial.print(oneValue);
 +  Serial.print(",");
 +  Serial.println(otherValue);
 +</code>
 +
 +
 + By changing the processing code for the serialEvent we can recieve more values.
 +<code java>
 +void serialEvent (Serial myPort) 
 +{
 +  // read serial buffer as string
 +  String myString = myPort.readString();
 +
 +  // if we have any other bytes than linefeed
 +  if (myString != null) 
 +  {
 +    // trim crap
 +    myString = trim(myString);
 +    // split the string at commas 
 +    // and convert sections into integers. 
 +    String sensors[] = split(myString, ',');
 +    if(sensors.length>1)
 +    {
 +      value1 = int(sensors[0]); 
 +      value2 = int(sensors[1]); //remember to create another value variable
 +      print(value1);
 +      print(',');
 +      println(value2);
 +    }
 +  }
 +}
 +</code>
 +
 +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, sliders, etc).
 +
 +
 +===Processing transmitting ===
 +
 +We can also do the opposite. Control stuff in the real world with stuff on the computer. Keyboard, mouse, GUI, network etc.
 +
 +== Processing code to transmit ==
 +
 +<code java>
 +import processing.serial.*;
 + 
 +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.
 +}
 + 
 +void draw() {
 + 
 +}
 + 
 +void mousePressed() {
 +  if (mouseButton == LEFT) {
 +    myPort.write("180\n");
 +  }
 +  else
 +  {
 +    myPort.write("20\n");
 +  }
 +}
 +</code>
 +== Arduino code to receive ==
 +
 +<code java>
 +
 +// pins for the LEDs:
 +int ledPin = 3;
 +int linefeed = 10;
 +
 +void setup() {
 +   // initialize serial:
 +   Serial.begin(9600);
 +   // make the pins outputs:
 +   pinMode(ledPin, OUTPUT); 
 +}
 +
 +void loop() {
 +   // if there's any serial available, read it:
 +   while (Serial.available() > 0) {
 +
 +     // look for the next valid integer in the incoming serial stream:
 +     int intensityVal = Serial.parseInt(); 
 +
 +     // look for the newline. That's the end of your
 +     // sentence:
 +     if (Serial.read() == linefeed) {
 +       // fade the red, green, and blue legs of the LED: 
 +       analogWrite(ledPin, intensityVal);
 +     }
 +   }
 +   delay(20);
 +}
 +</code>
 +
 +== Sending several values from Processing ==
 +
 +<code java>
 +  myPort.write("200,90,10\n");
 +</code>
 +
 +== Recieving several values on Arduino ==
 +
 +It's just a question of listening for more values. But it's important that you program to recieve as many as you send. Otherwise you can get unpredictable behaviour.
 +<code java>
 +     // look for the next valid integer in the incoming serial stream:
 +     int firstValue = Serial.parseInt(); 
 +     // do it again:
 +     int secondValue = Serial.parseInt();
 +     // do it again:
 +     int thirdValue = Serial.parseInt(); 
 +</code>
  
courses/intro.prototyping.spring.2016.nov8.txt · Last modified: 2016/11/08 06:02 by rickard