basic circuits – lab 4 serial and osc (maybe some theory too) xmedia spring 2011

16
Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Upload: roberta-waters

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Basic Circuits – Lab 4Serial and OSC

(maybe some theory too)

XmediaSpring 2011

Page 2: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Agenda

• Build your resistive sensor circuit

• Serial Communication– Arduino to Processing

• OSC– Processing to Unity

Page 3: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Arduino – Resistive Sensor

// Example 06B: Set the brightness of LED to// a brightness specified by the// value of the analogue input#define LED 9 // the pin for the LEDint val = 0; // variable used to store the value// coming from the sensor

void setup() {pinMode(LED, OUTPUT); // LED is as an OUTPUT// Note: Analogue pins are// automatically set as inputs}

void loop() {val = analogRead(0); // read the value from// the sensorSerial.println(val);analogWrite(LED, val/4); // turn the LED on at// the brightness set// by the sensordelay(10); // stop the program for// some time}

Page 4: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Arduino – Serial Communication

#define LED 9int val = 0;

void setup() {

Serial.begin(9600);pinMode(LED, OUTPUT); }

void loop() {val = analogRead(0);

Serial.println(val);analogWrite(LED, val/4);delay(10);}

import processing.serial.*

Serial sPort;int val;

void setup(){

println(Serial.list());//find the port that is your arduinosPort = new Serial(this, Serial.list()[2], 9600);//set the position of the array so it

corresponds//to your arduino

}

void draw(){

while(sPort.available() > 0){

val = sPort.read();println(val);background(val);

}}

Page 5: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

• Serial sPort; //creates the serial port object• sPort = new Serial(this, Serial.list()[2], 9600); //instantiates the object and

//opens the port• sPort.available(); //returns the number of bytes in the buffer to be read• sPort.read(); //returns the next byte in the buffer

– Each value added to the buffer by the Arduino is one byte (0 to 1023).– The buffer may contain more than one byte.– If you need to transmit data that requires more than one byte, you need to set up a protocol.– http://www.processing.org/learning/library/serialcallresponse.html

• void serialEvent(Serial sPort) {} //event method called when data is

//available– http://www.processing.org/reference/libraries/serial/serialEvent_.html

Processing – Notes about code

Page 6: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Processing - OSC

• Basically a data structure String like object sent to a port

• Use oscP5 and netP5 libraries – http://www.sojamo.de/libraries/oscP5/

• Open Sound Control– originally for sharing music performance data

• Message Specification– similar to xml

• Name Value Pairs

Page 7: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Processing – Serial and OSC

import processing.serial.*;

import oscP5.*;import netP5.*;OscP5 oscP5;NetAddress myRemoteLocation;

Serial sPort;int val;

void setup(){ oscP5 = new OscP5(this,12000); myRemoteLocation = new NetAddress("127.0.0.1",32000); println(Serial.list()); sPort = new Serial(this, Serial.list()[2], 9600);}

//. . .

//. . .

void draw(){ while(sPort.available() > 0) { val = sPort.read(); } }

void mousePressed(){ OscMessage myOscMessage = new OscMessage("/pilot"); myOscMessage.add(val); println(myOscMessage.get(0).intValue()); oscP5.send(myOscMessage, myRemoteLocation);}

Page 8: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Processing - OSC• oscP5 = new OscP5(this,12000);

• listening on port 1200 for OSC messages

• myRemoteLocation = new NetAddress("127.0.0.1",32000);• creates a location to send messages to on this machine on port 32000

• OscMessage myOscMessage = new OscMessage("/pilot");• creates a new OSC message with the address pattern of /pilot

• myOscMessage.add(val);• adds the value from the sensor to the messge

• println(myOscMessage.get(0).intValue());• prints the value at location 0 in the message as an int

• oscP5.send(myOscMessage, myRemoteLocation);• sends the message to the location defined in setup()

Page 9: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Processing - OSC• The specifics of the address pattern and the remote

location will depend on the 3D side of the project. We will be coordinating this.

Page 10: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Scaling Function

Page 11: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Lighting 3 LEDs in Parallel

• Each LED gets its own resistor

• Build this circuit• Measure the voltage

across each branch• Measure the current out

of the battery and before each LED

Page 12: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Current Split - Parallel

• Sum of the current through each branch equals the current from the power source

• Voltages are the same in each branch

Page 13: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Lighting 3 LEDs in Series

• One resistor for all the LEDs

• Build this circuit• Measure the voltage

across each LED• Measure the current out

of the battery and before each LED

Page 14: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Voltage Split - Series

• Voltage across each component is different

• Current through each component is the same

Page 15: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Voltage Divider

• Vout = Vin * R2 / (R1 + R2)

• If R1 is variable, as R1 increases Vout decreases

Page 16: Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011

Calculating Resistance

• Series– Rtotal = R1 + R2 + . . . + Rn

• Paralell– 1/Rtotal = 1/R1 + 1/R2 + … + 1/Rn