week7: Arduino and computer

This week’s topic is “How to connect physical object with computers”

download
processing >> target="_blank">https://processing.org/download/
( install 1.5.1 (15 May 2011) version and not the latest one)
PureData >> http://puredata.info/
MaxMSP >> http://cycling74.com/downloads/older/

github example code for the course
>> https://github.com/mikst/101/tree/master/week7

example1:
Examples >> Communication >> Graph

If you look at the Arduino sketch, it is simply reading the A0 input pin and communicating with computer via serial port (USB) with Serial.println(); command.

now copy the “/* Processing code for this example” to “*/” into the processing sketch window.
You can graph the sensor input on the computer screen.

If you open the incomingNumber sketch on processing, you can see how the original communication is made.

example2:
Examples >> Communication >> VirtualColorMixer

Copy the “/* Processing code for this example” to “*/” into the processing sketch window.
Now, you can add a rectangle, change its color and position according to the sensor inputs.

If you want to try out, you can also copy “———-begin_max5_patcher———-” to ”
” into new max patch window and see what happens.

===processing code===

// This example code is in the public domain.

import processing.serial.*;

float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value

float xPos=0;
float rectXsize=0;

Serial myPort;

void setup() {
size(600, 600);

// 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 Arduino, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);
}

void draw() {
// set the background color with the color values:
background(255);

fill(greenValue, blueValue,redValue );
rect (xPos,100,50,50);

rect(xPos,200, rectXsize,rectXsize);
ellipse(400,400,rectXsize,rectXsize);

}

void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil(‘\n’);

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
float[] colors = float(split(inString, “,”));
// if the array has at least three elements, you know
// you got the whole thing. Put the numbers in the
// color variables:
if (colors.length >=3) {
// map them to the range 0-255:
redValue = map(colors[0], 0, 1023, 0, 255);
greenValue = map(colors[1], 0, 1023, 0, 255);
blueValue = map(colors[2], 0, 1023, 0, 255);

xPos=map(colors[0], 0, 1023, 0, width);
//yPos=map(colors[1],0,1023,0,height);
rectXsize=map(colors[0], 0, 1023, 10, 300);
}
}
}

=====================

example3:
Open drawer_processing.pde from github

You can make a small drawing application with the processing

example4:
Sound triggering.
Load the sound file and trigger it as your sensor reaches the threshold

PD >> PD_Arduino_soundTrigger.pd
processing >> trigger_sound

To learn more about PD and audio synthesis, here is a good tutorial
>> http://www.pd-tutorial.com/english/index.html

firmata library
http://arduino.cc/en/reference/firmata

Here is a firmata library for processing
http://playground.arduino.cc/Interfacing/Processing

we can try Pduino (for PureData). Download it from here
http://at.or.at/hans/pd/objects.html

Leave a Reply