Sunday, October 14, 2012

Midterm Project - Final Code!!!!

Finally, the code is finished. A video showing how it works:


Arduino code:

//Declaration of variables

int bend1 = A0;
int bend2 = A1;
int bend3 = A2;
int bend4 = A3;
int sum;
int x;
int y;
int z;
int w;

void setup()
{
  Serial.begin(9600);
}

void loop()
//Print the analogical input to send to Processing
{
  //Serial.print(
  x = analogRead(bend1);
  //);
  //Serial.print(",");
  //Serial.print(
  y = analogRead(bend2);
  //);
  //Serial.print(",");
  //Serial.print(
  z = analogRead(bend3);
  //);
  //Serial.print(",");
  //print and space
  //Serial.println(
  w = analogRead(bend4);
  //);
  sum = x+y+w+z;
  Serial.println(sum);
}

Processing code:


import processing.serial.*;
import processing.video.*;

float stotal;
Serial myPort;
boolean isBad = true;

Movie nowPlaying;

String[] badVideosPath = {"bad video 1.mp4", "bad video 2.mp4", "bad video 3.mp4"};
String[] goodVideosPath = {"good video 1.mp4", "good video 2.mp4", "good video 3.mp4"};
Movie[] badVideos = new Movie[badVideosPath.length];
Movie[] goodVideos = new Movie[goodVideosPath.length];

void setup() {
  for (int i = 0; i < badVideosPath.length; i++){
    badVideos[i] = new Movie(this, badVideosPath[i]);
  }
  for (int i = 0; i < goodVideosPath.length; i++){
    goodVideos[i] = new Movie(this, goodVideosPath[i]);
  }
  size(480,360,P2D);
  println(Serial.list());
  myPort = new Serial(this, "COM5", 9600);
  myPort.bufferUntil('\n');
}


void serialEvent(Serial myPort) {
  stotal = float(myPort.readStringUntil('\n'));

    boolean notPlaying = (nowPlaying == null || nowPlaying.time() == nowPlaying.duration());
    if ((notPlaying || !isBad) && stotal > 900) {
      if (nowPlaying != null) nowPlaying.stop();
      nowPlaying = badVideos[floor(random(badVideos.length))];
      nowPlaying.play();
      isBad = true;
    } else if (notPlaying && stotal > 500) {
      nowPlaying = goodVideos[floor(random(goodVideos.length))];
      nowPlaying.play();
      isBad = false;
    }
}

void movieEvent(Movie nowPlaying) {
  if (nowPlaying == null) return;
  nowPlaying.read();
}

void draw() {
  if (nowPlaying == null) return;
  image(nowPlaying,0,0);
}


No comments:

Post a Comment