Research & Learning

Other Class pages

# Intro to Physical Computing (Fall 2009)

## Laptops

Laptops are very useful tools, but they are also very effective instruments of distraction. Everyone benefits if we all pay attention. I'll do my best to keep the class interesting, I hope you'll join me in this pursuit. You are welcome to use your laptop in class when I am speaking, or when it is relevant to the classwork being presented. However, during discussions and when your fellow students are talking, please be respectful of everyone's time and close the lid. If necessary, I'll remind of this, but even better would be if everyone does so naturally.

## First First Week Proposals

1. Alexandra Kuechenberg

## Second Week of Proposals

Week 1:

```void setup()
{
pinMode(2, OUTPUT);      // sets the digital pin as output
pinMode(3, INPUT);
}

void loop()
{
digitalWrite(2, HIGH);   // sets the LED on
delay(1000);                  // waits for a second
digitalWrite(2, LOW);    // sets the LED off
delay(1000);                  // waits for a second
}
}
```

Week 2:

Week 3:

```  if (input > biggestNumberIEverSaw){
biggestNumberIEverSaw = input;
}
input = map(input, 800,biggestNumberIEverSaw,0,255);
```

Week 4

```
#include <Tone.h>

Tone tone1;

int lastLight;
int lastPulse;

int tilts[5] ;
int placeInTiltArray = 0;

int notes[] = {
NOTE_CS3 , NOTE_D3 ,NOTE_E3 ,NOTE_F3,NOTE_G3,NOTE_A3,NOTE_B3,NOTE_C4 };

void setup(){
Serial.begin(9600);
tone1.begin(6);
Serial.println("Start");
}

void loop(){

int lightout = map(light,0,500,0,255);
analogWrite(11,lightout);

tilts[placeInTiltArray] = tilt;
placeInTiltArray++;
if (placeInTiltArray >= sizeof(tilts)) placeInTiltArray = 0;
int total = 0;
for(int i = 0; i < sizeof(tilts); i++){
total = total + tilts[i];
}
tilt = total/sizeof(tilts);

if (abs(light-lastLight) > 20){
int note = 7- map(tilt,400,600,0,7);
tone1.play(notes[note], 300);
Serial.println( note);
}
lastLight = light;

if (millis() - lastPulse >= 20){
int pulse =  2500- map(tilt,300,700,500,2500);
//Serial.println(pulse);
digitalWrite(3, HIGH);   // Turn the motor on
delayMicroseconds(pulse);       // Length of the pulse sets the motor position
digitalWrite(3, LOW);    // Turn the motor off
lastPulse = millis();
}
delay(5);

}

```
```    * PROCESSING CIRCLE
```

import processing.serial.*;

Serial myPort; int input;

void setup(){

```  size(800,600);
println(Serial.list());
myPort = new Serial(this, "COM28", 9600);
ellipseMode(CENTER);
```

}

void draw(){

```  background(255);
ellipse(width/2,height/2,input*5,input*5);
```

} void serialEvent(Serial p) {

```  input = p.read();
println("input" + input);
```

}

```    * PROCESSING GRAPH
```

import processing.serial.*;

Serial myPort; int input; int xpos;

void setup(){

```  size(800,600);
println(Serial.list());
myPort = new Serial(this, "COM28", 9600);
background(255);
```

}

void draw(){

```  ellipse(xpos,input,2,2);
xpos++;
if (xpos > width){
xpos = 0;
background(255);
}
```

} void serialEvent(Serial p) {

```  input = p.read();
println("input" + input);
```

}

# Week 7

## Arduino

``` int heat = 65; int light = 65; int onOff = 0;
```

void setup() {

```  Serial.begin(9600);
pinMode(4, INPUT);
pinMode(7, OUTPUT);
Serial.println("Start");
```

}

void loop() {

```  if (Serial.available() > 0){  //only send if you have hear back
input = map(input,0,255,500,2500);    // convert the analog value
// to a range between minPulse
Serial.println(input);
digitalWrite(7, HIGH);   // Turn the motor on
delayMicroseconds(input);       // Length of the pulse sets the motor position
digitalWrite(7, LOW);    // Turn the motor off

Serial.print(heat);
Serial.print(",");
Serial.print(light);
Serial.print(",");
Serial.print(onOff);
Serial.print(",");

Serial.print(10, BYTE);  //send a return
//Serial.flush();
delay(20);  //you might use this instead of if available
}
```

}

## Procressing

import processing.serial.*;

float bgColor; // Background color

Serial port; // The serial port float xpos; // position of the ball float ypos ; int shape;

void setup() {

```  size(800, 600);  // Stage size
ypos = height*3/4;
xpos = width/4;
noStroke();      // No border on the next thing drawn
ellipseMode(CENTER);
rectMode(CENTER);

// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);  //you can pull the name out of the list
//port = new Serial(this, "/dev/tty.BlueRadios-COM0-1", 9600);  //or you can just specify it
port.write(65);    // Send a capital A in case the microcontroller is waiting to hear from you
// println("OKAY LET'S GO");
```

}

void draw() {

```  background(0);
fill(255-bgColor,0,0);
// Draw the shape
if (shape == 0){
ellipse(xpos, ypos, 60, 60);
}
else{
rect(xpos, ypos, 60, 60);
}
```

}

void serialEvent(Serial port) {

```  String input = port.readStringUntil(10);  //make sure you return (Ascii 13) at the end of your transmission
if (input != null){
print("Raw Input: " + input);
String[] parts = input.split(",");  //this will only work if you put commas (Ascii 44) between things in your transmission
if (parts.length >= 3){
ypos = int(parts[0]);
ypos = map(ypos,300,900,height,0);  //400 and 625 were arrived at emperically by looking at readings from the sensor
bgColor =  int(parts[1]);
bgColor = map(bgColor,430,550,0,255);
shape = int(parts[2]);
float mouseDistanceAcross = map(mouseX,0,width,0,255);
port.write(int(mouseDistanceAcross)); //in this example you might use the mouse distance across to control the panning of a servo motor

}
}
```

}

# Week 6 ACCELEROMETER + RGB LED

ARDUINO SIDE

int analogReading; //variable int size because adc number potentially as big as 1024

void setup() {

```  beginSerial(9600);  //set up communication back to pc
//don't forget to press "serial monitor button"
```

}

void loop() {

```  analogReading = analogRead(5); //can only use pins 0-5
Serial.print(44,BYTE);
Serial.print(44,BYTE);
Serial.print(44,BYTE);
Serial.println(10,BYTE); //talk back to pc
if (Serial.available() >=3){
analogWrite(6,b);
analogWrite(5,6);
analogWrite(11,g);
}
delay(20);
```

}

PROCESSING SIDE import processing.serial.*;

Serial port; // The serial port int xpos, ypos; // position of the ball int r,g,b;

void setup() {

```  size(255, 255);  // Stage size

ellipseMode(CENTER);

xpos = width/2;
ypos = height/2;
// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
println(Serial.list());
//port = new Serial(this, Serial.list()[0], 9600);  //you can pull the name out of the list
port = new Serial(this, "COM28", 9600);  //or you can just specify it
sendColors();  // Send stuff in case the microcontroller is waiting to hear from you
```

}

void sendColors(){

```  port.write(r);
port.write(g);
port.write(b);
//println("R" + r+ " G" + g + " B" + b);
```

}

void draw() {

```  for(int i=0; i<width; i++) {
for(int j=0; j<height; j++) {
stroke(0, i, j);
point(i, j);
}
```

}

```  int thisPixel = get(xpos,ypos);
r = int(red(thisPixel));
g = int(green(thisPixel));
b = int(blue(thisPixel));
fill(255,255,255);
ellipse(xpos,ypos,10,10);
```

}

void serialEvent(Serial port) {

```  String input = port.readStringUntil(10);  //make sure you return (Ascii 13) at the end of your transmission
if (input != null){
print("Raw Input: " + input);
String[] parts = input.split(",");  //this will only work if you put commas (Ascii 44) between things in your transmission
if (parts.length > 2){ //make sure it is a full valid message
//turn them from ASCII into numbers
int xtilt = int(parts[0]);
int ytilt = int(parts[1]);
if (xtilt > 500) {
xpos = xpos + 1 ;// same as xpos++
}
else {
xpos = xpos - 1 ;// same as xpos--
}
xpos = min(xpos,width);
xpos = max(xpos,0);
if (ytilt > 500) {
ypos = ypos + 1 ;// same as ypos++
}
else {
ypos = ypos - 1 ;// same as ypos--
}
ypos = min(ypos,width);
ypos = max(ypos,0);
println("Positionx:" + xpos + " y:" + ypos );
sendColors();
}
}
```

}

2 analog, 1 switch and a servo

ARDUINO

int heat = 65; int light = 65; int onOff = 0;

void setup() {

```  beginSerial(9600);
pinMode(4, INPUT);
pinMode(2, OUTPUT);
```

}

void loop() {

```  if (Serial.available() > 0){  //only send if you have hear back
input = map(input,0,255,500,2500);    // convert the analog value
// to a range between minPulse
digitalWrite(2, HIGH);   // Turn the motor on
delayMicroseconds(input);       // Length of the pulse sets the motor position
digitalWrite(2, LOW);    // Turn the motor off

Serial.print(heat,DEC);
Serial.print(44, BYTE);
Serial.print(light,DEC);
Serial.print(44, BYTE);
Serial.print(onOff,DEC);
Serial.print(44, BYTE);

Serial.print(10, BYTE);  //send a return
delay(20);  //you might use this instead of if available
}
```

}

PROCESSING import processing.serial.*;

float bgColor; // Background color

Serial port; // The serial port float xpos; // position of the ball int shape;

void setup() {

```  size(256, 256);  // Stage size
noStroke();      // No border on the next thing drawn
ellipseMode(CENTER);
rectMode(CENTER);

// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
println(Serial.list());
//port = new Serial(this, Serial.list()[0], 9600);  //you can pull the name out of the list
port = new Serial(this, "COM30", 9600);  //or you can just specify it
port.write(65);    // Send a capital A in case the microcontroller is waiting to hear from you
// println("OKAY LET'S GO");
```

}

void draw() {

```  background(bgColor);
fill(255,0,0);
// Draw the shape
if (shape == 0){
ellipse(xpos, height/2, 20, 20);
}
else{
rect(xpos, height/2, 20, 20);
}
```

}

void serialEvent(Serial port) {

```  String input = port.readStringUntil(10);  //make sure you return (Ascii 13) at the end of your transmission
if (input != null){
println("Raw Input: " + input);
String[] parts = input.split(",");  //this will only work if you put commas (Ascii 44) between things in your transmission
if (parts.length >= 3){

bgColor =  int(parts[0]);
bgColor = map(bgColor,500,1000,0,255);

xpos = int(parts[1]);
xpos = map(xpos,500,580,0,width);  //400 and 625 were arrived at emperically by looking at readings from the sensor

shape = int(parts[2]);

float mouseDistanceAcross = map(mouseX,0,width,0,255);
port.write(int(mouseDistanceAcross)); //in this example you might use the mouse distance across to control the panning of a servo motor
}
}
```

}

Week 7

Week 8

Week 9

Week 10

int r,g,b;

void setup() {

```  Serial.begin(9600);  //set up communication back to pc
//don't forget to press "serial monitor button"
```

}

void loop() {

```  int analogReading = analogRead(5); //can only use pins 0-5
Serial.print(",");
Serial.print(",");
Serial.print(",");
Serial.println(10,BYTE); //talk back to pc
if (Serial.available() >=3){
}
analogWrite(6,b);
analogWrite(5,r);
analogWrite(3,g);
delay(20);
```

}

import processing.serial.*; Serial port; // The serial port

int xpos, ypos; // position of the ball

int r,g,b; PImage pic ;

void setup() {

```  size(255, 255);  // Stage size
ellipseMode(CENTER);
size(pic.width,pic.height);
xpos = width/2;
ypos = height/2;
// Print a list of the serial ports, for debugging purposes to find out what your ports are called:
println(Serial.list());
//port = new Serial(this, Serial.list()[0], 9600);  //you can pull the name out of the list
port = new Serial(this, "/dev/tty.usbserial-A7004nUQ", 9600);  //or you can just specify it
sendColors();  // Send stuff in case the microcontroller is waiting to hear from you
```

}

void sendColors(){

```  port.write(r);
port.write(g);
port.write(b);
```

}

void draw() {

```  //image of a color space
image(pic,0,0);

/*  draw a color space
for(int i=0; i<width; i++) {
for(int j=0; j<height; j++) {
stroke(0, i, j);
point(i, j);
}
}
*/
//find the color of the pixel at the current spot
int thisPixel = get(xpos,ypos);
r = int(red(thisPixel));
g = int(green(thisPixel));
b = int(blue(thisPixel));
//put an ellipse over the spot
fill(255,255,255);
ellipse(xpos,ypos,10,10);
```

}

void serialEvent(Serial port) {

```  String input = port.readStringUntil(10);  //make sure you return (Ascii 13) at the end of your transmission
if (input != null){
print("Raw Input: " + input);
String[] parts = input.split(",");  //this will only work if you put commas (Ascii 44) between things in your transmission
if (parts.length > 2){ //make sure it is a full valid message
int xtilt = int(parts[0]);
int ytilt = int(parts[1]);
xpos = int( map(xtilt,400,600,0,width) );
ypos = int( map(ytilt,400,600,0,height) );
sendColors();
}
}
```

}

Week 11

Week 12

 Edit | View | History | Print | Recent Changes | Search Page last modified on December 21, 2009, at 10:37 AM