Code:
Category Archives: Assignments
Final Project
Concept:
This was a game I designed for the final project of Parsons MFADT Physical Computing class. The goal of this game is to control and mix R/G/B color values to match the background color within 10 seconds each level. I used three photoresistors connected to arduino uno to sense the color value controlled by 1 to 3 players, and use openframeworks to build the user interface of this game.
Code:
Final Project
Processing basketball game
code:
arduino:
void setup() {
Serial.begin(9600); // initialize serial communication
}
void loop() {
// read the inputs
int xpos = analogRead(A0);
int ypos = analogRead(A1);
Serial.print(xpos);
Serial.print(“,”); // add a comma
Serial.println(ypos);
delay(10); // delay before sending the next set
}
processing:
/*
tweak from openprocessing example
http://www.openprocessing.org/sketch/104227
*/
import processing.serial.*;
Serial myPort;
PImage basketball, hoop, net, court;
float xpos, ypos, xvel, yvel, accel, n, xbasket, ybasket, boardHeight, give, space, i, wind;
int value = 0, dir = 1, level, score, hiscore, ballColor, comment;
boolean falling, Shoot, backboard, unlock1, unlock2, reset;
PShader blur;
void setup() {
size (1000, 800 );
basketball = loadImage(“Basketball.png”);
court = loadImage(“court2.png”);
hoop = loadImage(“hoop.png”);
xpos = width/5;
ypos = height-300;
yvel = .5;
xvel = .5;
accel = 2;
falling = false;
Shoot = false;
xbasket = random(400, 800);
ybasket = random(200, 600);
backboard = false;
give = 20;
score = 0;
space = 0;
level = 1;
hiscore = 0;
comment = 0;
unlock1 = false;
unlock2 = false;
ballColor = 4;
reset = false;
// 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()[5], 9600);
// don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);
}
void draw() {
//background();
image (court, 0, 0, width, height);
fill(255);
if (level == 2) {
unlock1 = true;
}
if (level == 3) {
unlock2 = true;
}
boardHeight = 180 – level*30;
n = 180 – level*30;
noStroke();
fill(255);
if (score > hiscore) {
hiscore = score;
}
textSize(20);
text(“HIGHSCORE: ” + hiscore, 10, height – 70);
text(“SCORE: ” + score, 10, height – 30);
level = score/10 + 1;
text(“LEVEL: ” + level, 10, height – 50);
image(hoop, xbasket, ybasket, 150, 100);
println(score + “, ” + level);
if (xpos >= width||ypos >= height) {
reset = true;
}
if (xvel > 25) {
xvel = 25;
}
if (yvel >25) {
yvel = 25;
}
rect(0, 0, yvel*20, 5);
rect(0, 10, xvel*20, 5);
rect(xbasket+n, ybasket – boardHeight, 5, boardHeight+5);
if (Shoot == false||falling==false) {
xvel = (mouseX-width/5)/7;
yvel = (height-300 – mouseY)/7;
stroke(255);
line(xpos, ypos, xpos+xvel*5, ypos-yvel*5);
noStroke();
}
if (Shoot == true||falling==true) {
falling = true;
if (backboard) {
yvel -= 5 ;
xpos -= 1.25*xvel;
give = 40;
} else {
xpos+= xvel;
}
yvel -= accel;
ypos -= yvel;
}
if (xpos > xbasket-20 + n && xpos < xbasket + n + 30 && ypos > ybasket -boardHeight-5 && ypos < ybasket) {
backboard = true;
}
if (xpos>xbasket-10 && xpos<xbasket+20 && ypos < (ybasket + give) && ypos > (ybasket -10)) {
yvel = -yvel;
xvel = -xvel;
}
if (xpos>xbasket+20 && xpos<(xbasket+n) && ypos < (ybasket + give) && ypos > (ybasket -10) && yvel < 1 ) {
xpos = width/5;
ypos = height-300;
yvel = .1;
xvel = .1;
accel = 1;
falling = false;
Shoot = false;
ybasket = random(300, 700);
xbasket = random(400, 800);
// n -=30;
backboard = false;
// boardHeight-=30;
score += 1;
comment = score + 1;
}
if (ballColor == 4) {
fill(242, 98, 15);
}
if (ballColor == 1) {
fill(255, 0, 0);
}
if (ballColor == 2) {
fill(0, 255, 0);
}
if (ballColor == 3) {
fill(random(0, 255), random(0, 255), random(0, 255));
}
image (basketball, xpos, ypos, 40, 40);
/* if (unlock1 == true){
fill(255, 0, 0);
ellipse(int(width-40), int(height – 40), 30, 30);
fill(0,255, 0);
ellipse(int(width-80), int(height – 40), 30, 30);
fill(random(0,255),random(0,255),random(0,255));
ellipse(int(width-120), int(height – 40), 30, 30);
} */
if (reset == true) {
xpos = width/5;
ypos = height-300;
yvel = .1;
xvel = .1;
accel = 1;
n = 150;
falling = false;
Shoot = false;
ybasket = random(200, 500);
xbasket = random(400, 800);
backboard = false;
boardHeight = 150;
give = 15;
score = 0;
i = 0;
space = 0;
comment = 1;
reset = false;
}
}
void mouseClicked() {
if (value == 0) {
Shoot = true;
}
}
void keyPressed() {
if (key == ‘s’) {
Shoot = true;
}
if (key == ‘r’) {
reset = true;
}
if ( unlock1 == true) {
if (key == ‘c’) {
ballColor = 1;
}
if (key == ‘v’) {
ballColor = 2;
}
if (key == ‘b’) {
ballColor = 3;
}
if (key == ‘n’) {
ballColor = 4;
}
}
}
void serialEvent(Serial thisPort) {
// read the serial buffer:
String inputString = thisPort.readStringUntil(‘\n’);
println(inputString);
if (inputString != null)
{
// trim the carrige return and linefeed from the input string:
inputString = trim(inputString);
// split the input string at the commas
// and convert the sections into integers:
int sensors[] = int(split(inputString, ‘,’));
// if we have received all the sensor values, use them:
if (sensors.length == 2) {
// scale the sliders’ results to the paddles’ range:
mouseX = int(map(sensors[0], 0, 1023, 0, width));
mouseY = int(map(sensors[1], 0, 1023, 0, height));
}
}
}
Circuit
Servo hw
code:
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(10); // waits 15ms for the servo to reach the position
}
for(pos = 90; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(10); // waits 15ms for the servo to reach the position
}
}
FINAL_LUOBIN_WANG
This is the final project for my pComp I class. The initial idea was to create a light illusion inside of a gate, however in the proposal the gate was only around 2 feet high or lower. After the review in class, I decide to make this an art installation in my locker and make it interactive. The final concept was two use several boards(not limited in 2), and place light strip on all edges. By manipulating the lightness of each edges, the light should create an illusion that the space is shifting with the movement of the audience. During the making process the broads were being limited in 2, but still delivers decent effect. I used two meter NeoPixel from Adafruit, which comes with a library only for Arduino, which left me no choice but not using firmata. The biggest problem when doing this project is communication. Sending values from Arduino to openFrameworks is easy, but the protocol doesn’t support the other way that well(or I haven’t found the best way). It took me a long time to make the Arduino board to no only separate the serial value but also find the head of the string. The face detection was done in with ofxfacetracker. To have the best result, the tracking point was set to the center of nose.
_________arduino code_________
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define PINLEFTBIG 13
#define PINRIGHTBIG 12
#define PINUPBIG 10
#define PINDOWNBIG 11
#define PINLEFTS 9
#define PINRIGHTS 8
#define PINUPS 6
#define PINDOWNS 7
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS1 10
#define NUMPIXELS2 30
#define NUMPIXELS3 5
#define NUMPIXELS4 15
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter–see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixelleft = Adafruit_NeoPixel(NUMPIXELS2, PINLEFTBIG, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelright = Adafruit_NeoPixel(NUMPIXELS2, PINRIGHTBIG, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelup = Adafruit_NeoPixel(NUMPIXELS1, PINUPBIG, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixeldown = Adafruit_NeoPixel(NUMPIXELS1, PINDOWNBIG, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixellefts = Adafruit_NeoPixel(NUMPIXELS4, PINLEFTS, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelrights = Adafruit_NeoPixel(NUMPIXELS4, PINRIGHTS, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixelups = Adafruit_NeoPixel(NUMPIXELS3, PINUPS, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixeldowns = Adafruit_NeoPixel(NUMPIXELS3, PINDOWNS, NEO_GRB + NEO_KHZ800);
int delayval = 0; // delay for half a second
int disX=0;
int disY=0;
int noseX = 0;
int noseY = 0;
int valueX, valueY;
void setup() {
Serial.begin(9600);
pixelleft.begin(); // This initializes the NeoPixel library.
pixelright.begin();
pixelup.begin();
pixeldown.begin();
pixellefts.begin(); // This initializes the NeoPixel library.
pixelrights.begin();
pixelups.begin();
pixeldowns.begin();
}
void loop() {
//led
// noseX = getNoseX();
// noseY = getNoseY();
for(int i=0;i<10;i++){
if (Serial.find(“s”)){
noseX = Serial.parseInt();
noseY = Serial.parseInt();
}
if(noseX != NULL){
if(noseX >= 70 || noseX <= 500 ){
valueX = noseX ;
}
}
Serial.print (valueX);
if(noseX != NULL){
if(noseY >= 70 || noseY <= 400 ){
valueY = noseY ;
}
}
disX= map(valueX, 60, 520, 0, 230);
disY= map(valueY, 80, 400, 0 ,255);
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixelup.setPixelColor(i, pixelup.Color(255-disY,255-disY,255-disY)); // Moderately bright green color.
pixelup.show(); // This sends the updated pixel color to the hardware.
pixeldown.setPixelColor(i, pixeldown.Color(disY,disY,disY)); // Moderately bright green color.
pixeldown.show(); // This sends the updated pixel color to the hardware.
// Delay for a period of time (in milliseconds).
}
for(int i=0;i<30;i++){
disX= map(valueX, 60, 520, 0, 230);
disY= map(valueY, 80, 400, 0 ,255);
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixelleft.setPixelColor(i, pixelleft.Color(disX,disX,disX)); // Moderately bright green color.
pixelleft.show(); // This sends the updated pixel color to the hardware.
pixelright.setPixelColor(i, pixelright.Color(230-disX,230-disX,230-disX)); // Moderately bright green color.
pixelright.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
for(int i =0;i<5;i++){
disX= map(valueX, 60, 520, 0, 230);
disY= map(valueY, 80, 400, 0 ,255);
pixelups.setPixelColor(i, pixelups.Color(255-disY,255-disY,255-disY)); // Moderately bright green color.
pixelups.show();
pixeldowns.setPixelColor(i, pixeldowns.Color(disY,disY,disY)); // Moderately bright green color.
pixeldowns.show();
delay(delayval);
}
for(int i =0;i<15;i++){
disX= map(valueX, 60, 520, 0, 230);
disY= map(valueY, 80, 400, 0 ,255);
pixellefts.setPixelColor(i, pixellefts.Color(disX,disX,disX)); // Moderately bright green color.
pixellefts.show(); // This sends the updated pixel color to the hardware.
pixelrights.setPixelColor(i, pixelrights.Color(230-disX,230-disX,230-disX)); // Moderately bright green color.
pixelrights.show();
delay(delayval);
}
delay(0);
}
_______openFramworks code___________
#include “testApp.h”
using namespace ofxCv;
using namespace cv;
void testApp::setup() {
ofSetVerticalSync(true);
ofBackground(0);
ofSetLogLevel(OF_LOG_VERBOSE);
serial.listDevices();
vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
// this should be set to whatever com port your serial device is connected to.
// (ie, COM4 on a pc, /dev/tty…. on linux, /dev/tty… on a mac)
// arduino users check in arduino app….
int baud = 9600;
serial.setup(0, baud); //open the first device
//serial.setup(“COM4”, baud); // windows example
//serial.setup(“/dev/tty.usbserial-A4001JEC”, baud); // mac osx example
//serial.setup(“/dev/ttyUSB0”, baud); //linux example
cam.listDevices();
cam.setDeviceID(1);
cam.initGrabber(640, 480);
tracker.setup();
tracker.setRescale(.5);
}
void testApp::update() {
bSendSerialMessage = true;
noseBase = tracker.getImageFeature(ofxFaceTracker::NOSE_BASE).getCentroid2D();
int valueX = noseBase.x;
int valueY = noseBase.y;
noseX = ofToString(valueX);
noseY = ofToString(valueY);
//update nose position
// ofLog(OF_LOG_NOTICE) << “nosePosition XY ” << noseBase.x , noseBase.y;
//camera function
cam.update();
if(cam.isFrameNew()) {
if(tracker.update(toCv(cam))) {
classifier.classify(tracker);
}
}
//serial write
if (bSendSerialMessage){
// (1) write the letter “a” to serial:
string a = “s”; // string a
a.append(noseX);
a.append(“,”);
a.append(noseY);
a.append(“\n”);
//string b = “s5,200\n”;
//unsigned char* nextChar = (unsigned char*) b.c_str();
unsigned char* valChar = (unsigned char*) a.c_str(); // cast from string to unsigned char*
// ofLog(OF_LOG_NOTICE) << “warning ” << valChar;
//ofDrawBitmapString(a, 30, 100); // draws a
//ofDrawBitmapString(string((char *)b), 30, 130); // draws b
//unsigned char buf[3] = {‘2’, ‘,’, ‘1’};
//serial.writeBytes(&buf[0], 3);
serial.writeBytes(&valChar[0], 8);
//serial.writeBytes(&nextChar[0], 6);
// (2) read
// now we try to read 3 bytes
// since we might not get them all the time 3 – but sometimes 0, 6, or something else,
// we will try to read three bytes, as much as we can
// otherwise, we may have a “lag” if we don’t read fast enough
// or just read three every time. now, we will be sure to
// read as much as we can in groups of three…
nTimesRead = 0;
nBytesRead = 0;
int nRead = 0; // a temp variable to keep count per read
unsigned char bytesReturned[3];
memset(bytesReadString, 0, 4);
memset(bytesReturned, 0, 3);
while( (nRead = serial.readBytes( bytesReturned, 3)) > 0){
nTimesRead++;
nBytesRead = nRead;
};
memcpy(bytesReadString, bytesReturned, 3);
bSendSerialMessage = false;
readTime = ofGetElapsedTimef();
ofLog(OF_LOG_NOTICE) << “return “ << bytesReadString;
}
}
void testApp::draw() {
ofSetColor(255);
cam.draw(0, 0);
tracker.draw();
int w = 100, h = 12;
ofPushStyle();
ofPushMatrix();
ofTranslate(5, 10);
}
HOMEWORK_LOLSHIELD_LUOBIN
During soldering process I encountered a problem that can’t get all the LED to line up straight. Then I found this video which is very useful to watch before proceeding to the making part.
front_______________________
QUASI : Final project Enrica and Roula
QUASI [kwey-zahy, -sahy, kwah-see, -zee] : a combining form meaning “resembling,” “having some, but not all ofthe features of,” used in the formation of compound words: quasi-definition; quasi-monopoly; quasi-official; quasi-scientific.
Quasi is a Quasi-object and a Quasi-Human. He is so emotionally connected to us that will be lovely and calm when we are around it, but can be very very very pissed if we leave it alone, by itself, like a normal object. If he gets pissed then it will start sending email to show its angry emotions. The messages he send are directly pulled from Twitter, Quasi wants to speak like a human.
Its body is composed by a hearth and a brain and an eye.
The brain is composed by an Arduino, the eye by a PIR sensor and its heart is a 3d printed heart. He lives in a Jar that can protect him, just like the rose of the Little Prince.


code:
[all] Final Presentation Day – some photos and videos
Thank Aisen!
Final – ALAN
Alan is creature that is trying to figure out an identity. He started his journey by trying to walk, but evolved to become a swimmer. His members are controlled by an app interface. His body is entirely 3D printed, which allows him to be modified into becoming what he wants to be. Alan can evolve by data over the internet. The online interface let the user control all of Alan’s six legs. Thus, the game play with multiple gamers could develop in a complex team competition with the goal to let Alan walk.
Interface Node:
3D modeling:
Process:
Video: