Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Project Chime p5 sketch for sound and visualization
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbmodem1411'; // fill in your serial port name here
var inData;
var values;
var j;
//var mappedValue;
var counter1=0;
var counter2=0;
var counter3=0;
var counter4=0;
var counter5=0;
//var counter4 = 0;
var piezo1;
var piezo2;
var piezo3;
var piezo4;
var piezo5;
var balls= [];
function preload(){
chime1 = loadSound("http://res.cloudinary.com/abhinav21/video/upload/v1508957364/chime1_wg555g.mp3")
chime2 = loadSound("http://res.cloudinary.com/abhinav21/video/upload/v1508957371/chime2_f8ni4u.mp3")
chime3 = loadSound("http://res.cloudinary.com/abhinav21/video/upload/v1508957420/chime4_qsu5lk.mp3")
chime3r = loadSound("http://res.cloudinary.com/abhinav21/video/upload/v1509056057/chime_3_remade_bae0dv.mp3")
chime4 = loadSound("http://res.cloudinary.com/abhinav21/video/upload/v1508957421/chime5_yn0va2.mp3")
chime5 = loadSound("http://res.cloudinary.com/abhinav21/video/upload/v1509047171/398492__anthousai__wind-chimes-single-03_y11frs.wav")
chime5r = loadSound("http://res.cloudinary.com/abhinav21/video/upload/v1509056052/chime_5_remade_hfdgcl.mp3")
}
function setup() {
createCanvas(1600, 768);
background(0);
angleMode(DEGREES);
print("CHIME starting!");
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('list', printList); // set a callback function for the serialport list event
serial.on('data', serialEvent); // callback for when new data arrives
// change the data rate to whatever you wish
var options = { baudrate: 9600};
serial.open(portName, options);
}
function draw() {
if (piezo1 ==0){
counter1 =0;
background(0);
}
if (piezo2 ==0){
counter2 =0;
background(0);
}
if (piezo3 ==0){
counter3 =0;
background(0);
}
if (piezo4 ==0){
counter4 =0;
background(0);
}
if (piezo5 ==0){
counter5 =0;
background(0);
}
if ( piezo1>0 && counter1 == 0){
chime1.play();
counter1 ++;
}
else if (piezo2>0 && counter2 ==0){
chime2.play();
counter2 ++;
}
else if (piezo3>0 && piezo3<10 && counter3 ==0){
chime3.play();
counter3 ++;
}
else if (piezo3>=10 && counter3 ==0){
chime3r.play();
counter3 ++;
}
else if (piezo4>0 && counter4 ==0){
chime4.play();
counter4 ++;
}
else if (piezo5>0 && piezo5<10 && counter5 ==0){
chime5.play();
counter5 ++;
}
else if (piezo5>10 && counter5 ==0){
chime5r.play();
counter5 ++;
}
for(i=0;i<balls.length;i++){
balls[i].displ();
balls[i].ymotion();
balls[i].dir();
}
}
function serialEvent() {
var inString = serial.readStringUntil("\r\n");
if (inString.length > 0 ) {
values = split(inString, ',');
piezo1 = values[0];
piezo2 = values[1];
piezo3 = values[2];
piezo4 = values[3];
piezo5 = values[4];
if(piezo1 >0 || piezo2>0 || piezo3>0 || piezo4>0 || piezo5>0){
balls.push(new Balls());
}
}
print(piezo1 + " " + piezo2 + " " + piezo3 + " " + piezo4 + " " + piezo5);
}
function Balls() {
this.xpos = random(0,1600)
this.ypos = 0;
this.dir = 1;
this.waveCount = 0;
this.p = 0;
this.xspeed = 0.5;
this.yspeed = 0.1;
this.acc = 0.005;
this.displ= function(){
fill(255);
push();
translate(this.xpos,this.ypos);
ellipse(0,0,30,30);
for(this.p=0;this.p<=360;this.p+=45){
fill(255,0,0);
rotate(this.p);
ellipse(0,-30,8*3,15*3);
}
pop();
};
this.ymotion = function() {
//if(this.waveCount<20){
this.ypos += this.yspeed;
this.yspeed +=this.acc;
//this.xpos += 0.5;
//this.waveCount ++;
}
this.dir = function(){
if(this.waveCount <100){
this.xpos += this.xspeed;
this.waveCount++;
}
else{
this.xspeed *=-1;
this.waveCount = 0;
}
}
}
// print list of ports for debugging
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
print(i + " " + portList[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.