Recitation 10

This combination of the Arduino and the Processing is actually a part of my final project. The circuits of the it are two buttons, when you press the button, it will blossom fireworks on the screen. So it is required to let the Arduino send the messages and the Processing will receive messages and appear fireworks with random colors.

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(A2);

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);
}

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(500, 500);
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, 50, 50);
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);
}
}

Here is the vedio:

Leave a Reply

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