|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
Dano TuesIntro to Physical Computing (Fall 2008)
LaptopsLaptops 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. Journalsadd a link to your pcomp journals below with your name:
Weekly LinksWeek 1:
void setup()
{
pinMode(2, OUTPUT); // sets the digital pin as output
pinMode(3, INPUT);
}
void loop()
{
if (digitalRead(3)){
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:
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(0); //can only use pins 0-5
Serial.println(analogReading); //talk back to pc
}
Week 3: Week 4 Week 5 ARDUINO SEND A SINGLE BYTE int analogReading; //variable int size because adc number potentially as big as 1024 void setup() { beginSerial(9600); //EITHER use serial monitor or processing to recieve serial //NOT BOTH } void loop() { analogReading = analogRead(0)-500; // make it byte sized Serial.print(analogReading,BYTE);//talk back to pc }
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);
}
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 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(analogReading,DEC);
Serial.print(44,BYTE);
analogReading = analogRead(4); //can only use pins 0-5
Serial.print(analogReading,DEC);
Serial.print(44,BYTE);
analogReading = analogRead(3); //can only use pins 0-5
Serial.print(analogReading,DEC);
Serial.print(44,BYTE);
Serial.println(10,BYTE); //talk back to pc
if (Serial.available() >=3){
int r = Serial.read();
int g = Serial.read();
int b = Serial.read();
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
int input = Serial.read();
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
heat = analogRead(5);
light = analogRead(0);
onOff = digitalRead(4);
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
http://www.sparkfun.com/commerce/product_info.php?products_id=8661
Week 10 Week 11 Week 12 |