Recitation 10: Image & Video

Recitation 10: Image & Video

This is a prototype for my final project. We are using buttons to control the animations on processing.  I started with one button to play the “do” note, so I replicated the circuit to allow the next button to control the “re” note. 

After the circuit was done, I played the code, but only one button seemed to work. I tried to fix it by switching the buttons but that wasn’t the problem. Then, I switched it from the A2 plug in to A5 plug in. From this, I discovered that the A2 plug in on my Arduino board is broken.  

This is a video of the final product. One button plays “do” and the other plays “re.” These sounds also produce random colored firework explosions on the processing screen.

Codes:

Processing 

import processing.serial.*;
import osteele.processing.SerialRecord.*;
import processing.sound.*;

Serial serialPort;
SerialRecord serialRecord;
SoundFile sound1;
SoundFile sound2;
float pre1;
float pre2;
int x;
int y;
int radius=100;
boolean playSound1=true;
boolean playSound2=true;

int value1 = 0; //serialRecord.values[1];
int value2 = 0; //serialRecord.values[2];

class Fire {
  float a;
  float b;
  float va;
  float vb;
  color col;
  float lifetime = 100;

  Fire(float a, float b, float va, float vb, color col) {
    this.a = a;
    this.b = b;
    this.va = va;
    this.vb = vb;
    this.col = col;
  }

  void draw() {
    if (lifetime-50>0) {
      noStroke();
      fill(col, lifetime-50);
      ellipse(a, b, 15, 15);
      lifetime -= 0.1;
    }
  }

  void update() {
    vb += G;
    a += va;
    b += vb;
  }
}

ArrayList<Fire> fireworks = new ArrayList();

final float G = 0.04;

void setup() {
  //background(0);
  size(800, 800);
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 3);
  sound1 = new SoundFile(this, "do.aiff");
  sound2 = new SoundFile(this, "re.aiff");

  value1 = serialRecord.values[1];
  value2 = serialRecord.values[2];
}


void draw() {
  serialRecord.read();
  ellipse(x, y, 150, 150);
  drawFireworks();
  drawDo();
  drawRe();
}

void drawFireworks() {
  noStroke();
  background(0);
  for (int i = 0; i < fireworks.size(); i++) {
    fireworks.get(i).update();
    fireworks.get(i).draw();
  }
  if (value1==1023||value2==1023) {
    fireworks.clear();
    color c = color(random(50, 255), random(50, 255), random(50, 255));
    int numFires = (int)random(4, 1000);
    //float x = random(0, 500);
    //float y = random(0, 500);
    for (int i=0; i<numFires; i++) {
      float r = random(0, TWO_PI);
      float R = random(0, 2);
      fireworks.add(new Fire(width/2,height/2 , R*sin(r), R*cos(r), c));
    }
  }
}

void drawDo() {
  value1 = serialRecord.values[1];
  if (value1 == 1023) {
    x=x+1;
    if (playSound1 == true) {
      sound1.play();
      playSound1 = false;
    }
    fill(200, 0, 100);
  } else {
    playSound1= true;
    fill(255);
  }
}

void drawRe() {
  value2 = serialRecord.values[2];
  if (value2 == 1023) {
    y=y+1;
    if (playSound2 == true) {
      sound2.play();
      playSound2 = false;
    }
    fill(200, 0, 100);
  } else {
    playSound2= true;
    fill(255);
  }
}

Arduino

#include "SerialRecord.h"

// Change this number to send a different number of values
SerialRecord writer(3);

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

void loop() {
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A5);

  writer[0] = millis() % 1024;
  writer[1] = sensorValue1;
  writer[2] = sensorValue2;
  writer.send(); 

  // This delay slows down the loop, so that it runs less frequently. This
  // prevents it from sending data faster than a Processing sketch that runs at
  // 60 frames per second will process it. It also makes it easier to debug the
  // sketch, because values are received at a slower rate.
  delay(100);
}

Leave a Reply

Your email address will not be published. Required fields are marked *