Skip to content

Instantly share code, notes, and snippets.

@lizkhoo
Created October 28, 2012 20: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/3969701 to your computer and use it in GitHub Desktop.
Save lizkhoo/3969701 to your computer and use it in GitHub Desktop.
Skunk hat with video
//ARDUINO CODE
//Most of the code courtesy of ITP's Intro to PComp Class
//for skunk hat with two FSRs
const int switchPin = 2;
int switchValue = 0;
int fsr1Value = 0;
int fsr2Value = 0;
void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT);
}
void loop(){
fsr1Value = analogRead(A0);
Serial.print(fsr1Value, DEC);
Serial.print(",");
fsr2Value = analogRead(A3);
Serial.print(fsr2Value, DEC);
Serial.print(",");
switchValue = digitalRead(switchPin);
Serial.print(switchValue, DEC);
Serial.println(); //adds 13 (carrige rtn) and 10 (new line) to data feed
}
///////////////////
//PROCESSING CODE
//Video not included
import processing.video.*;//import video
import processing.serial.*;//import serial library
Movie skunkLeft, skunkRight;
Serial myPort; //initiate your port
float playSpeedL, playSpeedR;// playback speed
void setup() {
size(640,480);
skunkLeft = new Movie(this, "skunk_left.mov");
skunkLeft.loop();
skunkRight = new Movie(this, "skunk_right.mov");
skunkRight.loop();
//store port name as a variable and read from first port [0]
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
// read bytes into a buffer until you get a new linefeed (ASCII 10):
myPort.bufferUntil('\n');
}
void draw() {
if (playSpeedL > 0 && playSpeedR == 0) {
if (skunkLeft.available()) {
skunkLeft.speed(playSpeedL);
skunkLeft.read();
image(skunkLeft, 0, 0);
}
}
if (playSpeedR > 0 && playSpeedL == 0){
if (skunkRight.available()) {
skunkLeft.speed(playSpeedR);
skunkRight.read();
image(skunkRight, 0, 0);
}
}
}
//triggered when there's a byte in the serial port
//function is part of the serial library
void serialEvent(Serial myPort) {
//read value from serial buffer, check if it is null
String myString = myPort.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// make sure there are three values before you use them:
if (sensors.length > 1) {
playSpeedL = map(sensors[0], 0, 480, 0, 4);
playSpeedR = map(sensors[1], 0, 200, 0, 4);
}
// 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment