Lab 5: Coincidential Input/Output

I hooked up the computer that would have been the output for the Processing code to a small pico projector and placed it in a box. I cut the top of the box off and replaced it with a translucent material (waxed paper) so that the top of the box is effectively a small rear-projection screen.

I placed a mirror in the end of the box, opposite the pico projector so that the image would be reflected up 90 degrees. Also in the box are the Arduino and a photocell resistor. When one waves a source of light over the box where the photocell is, the Processing graphics on the box moves in response.

 

Components:

1 Arduino

1 breadboard

1 pico projector

1 photocell resistor

10-ohm resistor

cables

cardboard box

small mirror

waxed paper

tape

computer

 

code

Arduino:

int potPin = 0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int ledR = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int ledG = 10; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int ledB = 11; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
}

void loop() {
potValue = analogRead(potPin); // read the pot value
analogWrite(ledR, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(ledG, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(ledB, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
Serial.println(potValue); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop
}

Processing:
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = “/dev/cu.usbmodem1421”; // or “COM5″
Serial port;
String buf=””;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

void setup() {
size(1280,720);
frameRate(10);
smooth();
background(255,255,255);
noStroke();
port = new Serial(this, portname, 9600);
}

void draw() {
// erase the screen
background(0, 0, 0);

// draw the ball
noFill();
stroke(200);
strokeWeight(.5);
line(150,serialVal,serialVal,100);
line(100,serialVal,serialVal,150);
ellipse(serialVal, serialVal, 50 + serialVal, 50 + serialVal);

strokeWeight(1);
line(200,serialVal,serialVal,75);
line(75,serialVal,serialVal,200);
ellipse(serialVal, serialVal, serialVal, serialVal);

strokeWeight(2);
line(250,serialVal,serialVal,55);
line(55,serialVal,serialVal,250);
ellipse(serialVal, serialVal, serialVal -50, serialVal – 50);

strokeWeight(2.5);
line(300,serialVal,serialVal,35);
line(35,serialVal,serialVal,300);
ellipse(serialVal, serialVal, serialVal -150, serialVal – 150);

strokeWeight(3.5);
line(500,serialVal,serialVal,15);
line(15,serialVal,serialVal,500);

}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println(“potValue=”+serialVal);
buf = “”;
}
}

 

 

Leave a Reply