|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
Benedetta F2011-1
InfoWhat: Intro to Physical Computing H79.2301.2 ContactBest way to reach me is via email at: bp432@nyu.edu GradingParticipation & Attendance: 40% SuppliesSee http://itp.nyu.edu/physcomp/Intro/Supplies Useful Linkshttp://www.arduino.cc - Arduino website. Check out the Getting Started and the Reference pages Cool Reference BooksPhysical Computing: Sensing and Controlling the Physical World with Computers Class BlogsPlease press "Edit" at the bottom of this page, login and enter the url to your blog (if you are new to wiki formatting just follow the pattern)
Notes from ClassWeek1 - September 12th
Resistors Breadboards Some Helpful Online Components Stores Week2 - September 19th
Week3 - September 26th
Useful links from class: Lion Tracking Project (mentioned in class because of all the animal tracking fantasy devices!): Week4 - October 3rdCode from class: //declare our FSR pin int sensorPin = A0; //declare our FSR value int sensorValue = 0; //declare LED pin int ledPin = 3; void setup(){ //init Serial monitor Serial.begin(9600); //declare our output pinMode(ledPin, OUTPUT); } void loop(){ sensorValue = analogRead(sensorPin); //read our FSR value // we remember that the range of our FSR 0 - 980 int brightness = map(sensorValue, 0, 980, 0, 255); //remap the sensor value to a range from 0 to 255 analogWrite(ledPin, brightness); //PWM led pin 0 - 255 resolution Serial.println(sensorValue); delay (10); } Analog Input and Analog Output: Demonstrates analog input by reading an analog sensor on analog pin 0 and controlling a servo motor on pin 9 using the Servo library. #include <Servo.h> // include the servo library Servo servoMotor; // creates an instance of the servo object to control a servo int servoPin = 9; // Control pin for servo motor int sensorPin = A0; // Pin our FSR is plugged into int sensorValue = 0; // initialize value at 0 void setup() { Serial.begin(9600); // initialize serial communications servoMotor.attach(servoPin); // attaches the servo on pin 3 to the servo object } void loop() { sensorValue = analogRead(sensorPin); // read the analog input Serial.println(sensorValue); // print it delay(5); // if your sensor's range is less than 0 to 1023, you'll need to // modify the map() function to use the values you discovered: int servoAngle = map(sensorValue, 0, 975, 0, 179); // Serial.println(servoAngle); // delay(5); // move the servo using the angle from the sensor: servoMotor.write(servoAngle); } Week5 - October 17thMedia Controller Groups:
Week6 - October 24thWeek7 - October 31stCode from class: int xPin = A0; // the analog pin our accelerometer is plugged into Serial.begin(9600);
Serial.println("hello world");
} void loop (){ xValue = analogRead(xPin);
Serial.print("x = "); // just some nice formatting
Serial.println(xValue); // print the ASCII encoded decimal value
// print in many formats:
Serial.write(xValue); // Print the raw binary value
Serial.print('\t'); // print a tab
Serial.print(xValue, BIN); // print the ASCII encoded binary value
Serial.print('\t'); // print a tab
Serial.print(xValue, DEC); // print the ASCII encoded decimal value (same as above)
Serial.print('\t'); // print a tab
Serial.print(xValue, HEX); // print the ASCII encoded hexadecimal value
Serial.print('\t'); // print a tab
Serial.println(xValue, OCT); // print the ASCII encoded octal value
delay(300); // just so that you can read them slowly
}
Punctuation Method - Virtual Color Mixer: int xPin = A0; // pinMode(switchPin, INPUT);
Serial.begin(9600);
//Serial.println("hello world");
} void loop (){ xValue = analogRead(xPin);
Serial.print(xValue);
Serial.print(",");
yValue = analogRead(yPin);
Serial.print(yValue);
Serial.print(",");
zValue = analogRead(zPin);
Serial.println(zValue);
// button = digitalRead(switchPin);
// Serial.println(button);
} // Processing code for this example float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value
Serial myPort;
void setup() {
size(200, 200);
// 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(redValue, greenValue, blueValue);
}
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, 900, 0, 255);
greenValue = map(colors[1], 0, 900, 0, 255);
blueValue = map(colors[2], 0, 900, 0, 255);
}
}
}
int firstSensor = 0; // first analog sensor // start serial port at 9600 bps: Serial.begin(9600); pinMode(2, INPUT); // digital sensor is on digital pin 2 establishContact(); // send a byte to establish contact until receiver responds } void loop() { // if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(A0);
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondSensor = analogRead(A1);
// read switch, map it to 0 or 255L
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
// send sensor values:
Serial.print(firstSensor);
Serial.print(",");
Serial.print(secondSensor);
Serial.print(",");
Serial.println(thirdSensor);
}
} void establishContact() { while (Serial.available() <= 0) {
Serial.println("0,0,0"); // send an initial string
delay(30);
}
} // Processing code to run with this example: size(640,480);
// 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 module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
// draw with smooth edges:
smooth();
} void draw() { background(bgcolor); fill(fgcolor); // Draw the shape ellipse(xpos, ypos, 20, 20); } // serialEvent method is run automatically by the Processing applet // read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
xpos = map(sensors[0], 0,1023,0,width);
ypos = map(sensors[1], 0,1023,0,height);
fgcolor = sensors[2];
}
// send a byte to ask for more data:
myPort.write("A");
}
Serial Communication to make the Arduino manipulate physical things - Physical Pixel: int ledPin = 13; // the pin that the LED is attached to // initialize serial communication: Serial.begin(9600); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); } void loop() { // see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
}
} Processing code: import processing.serial.*; size(200, 200);
boxX = width/2.0;
boxY = height/2.0;
rectMode(RADIUS);
// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
mouseOverBox = true;
// draw a line around the box and change its color:
stroke(255);
fill(153);
// send an 'H' to indicate mouse is over square:
port.write('H');
}
else {
// return the box to it's inactive state:
stroke(153);
fill(153);
// send an 'L' to turn the LED off:
port.write('L');
mouseOverBox = false;
}
// Draw the box
rect(boxX, boxY, boxSize, boxSize);
}
Week8 - November 7thXBee set up Lab Plug in your XBee with PWR (3.3V), GND, TX and RX. You can use all sorts of USB to Serial converter for the XBees, I was using the simple FTDI breakout board from SparkFun. You program the XBees through CoolTerm or another Serial monitor program. 9600 baud rate, local echo and remember only CR as options.
These are some of the basic commands to program both XBees to send to each other over the same Pan ID. +++OK Week10 - November 21stControlling DC Motor Direction with HBridge int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) Serial.begin(9600);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
} void loop() { // Turn on one direction:
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
delay (2000);
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
delay (2000);
} Controlling DC Motor Direction & Speed with HBridge int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) Serial.begin(9600);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
} void loop() { // Turn on one direction:
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
for (int motorSpeed = 0; motorSpeed < 255; motorSpeed++){
analogWrite(enablePin, motorSpeed);
delay(4);
}
delay (2000);
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
for (int motorSpeed = 0; motorSpeed < 255; motorSpeed++){
analogWrite(enablePin, motorSpeed);
delay(4);
}
delay (2000);
} Controlling a Unipolar Stepper Motor with HBridge #include <Stepper.h> Serial.begin(9600);
Serial.println("Resetting");
// set the speed of the motor to 30 RPMs
//stepper.setSpeed(30);
} void loop() { stepper.step(10);
Serial.println("Stepping");
delay(100);
} Final Projects Groups and Ideas
Week11 - November 28thPatches from Luke's demo here! Final Presentation Days & Groups:December 12th
December 14th
Office Hours Thursday 12/8:
|