Skip to content

Instantly share code, notes, and snippets.

@lizkhoo
Created October 28, 2012 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizkhoo/3970101 to your computer and use it in GitHub Desktop.
Save lizkhoo/3970101 to your computer and use it in GitHub Desktop.
PComp Midterm: Window Shade Play Test
//ARDUINO CODE
//adapted from ITP's Intro to Physical Computing class Fall 2012
const int switchPin1 = 2;
const int switchPin2 = 4;
int potPin = A0;
int switch1Value = 0;
int switch2Value = 0;
int potValue;
void setup() {
Serial.begin(9600);
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
establishContact();
}
void loop(){
if (Serial.available() > 0){
switch1Value = digitalRead(switchPin1);
Serial.print(switch1Value);
Serial.print(",");
switch2Value = digitalRead(switchPin2);
Serial.print(switch2Value);
Serial.print(",");
potValue = analogRead(potPin);
Serial.print(potValue);
Serial.println(); //adds 13 (carrige rtn) and 10 (new line) to data feed
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("hello"); // send a starting message
delay(300);
}
}
///////////////////
//PROCESSING CODE
//next edit: change image placement to 1 time then translate Y value
import processing.serial.*;
PImage pattern1;
Serial myPort;
float switch1, switch2, xpos, ypos, speed;
boolean firstContact = false;
void setup() {
size(600, 600);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
pattern1= loadImage("http://itp.katieadee.com/pcomp/pattern-demo.png");
}
void draw() {
background(255);
image(pattern1, 0, ypos);
image(pattern1, 0, 0);
ypos+=speed;
if (ypos >= 600){
ypos = -300;
}
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
// if you haven't heard from the microncontroller yet, listen:
if (firstContact == false) {
if (myString.equals("hello")) {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
int switches[] = int(split(myString, ','));
for (int sensorNum = 0; sensorNum < switches.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + switches[sensorNum] + "\t");
}
println();
if (switches.length > 1) {
//closed all the way
if (switches[0] == 1) {
ypos = 100;
speed = 0;
}
//use potentiometer
else if (switches[1] == 1 && switches[2] >= 1) {
ypos = 0;
speed = map(switches[2], 0, 680, 0, 10);
}
}
// when you've parsed the data you have, ask for more:
myPort.write("A");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment