xxxxxxxxxx
168
let serial;
let latestData = "waiting for data" ;
var spaceBarSound;
var clickSound;
var leftSound;
var rightSound;
var upSound;
var downSound;
var balls = [];
var rects = [];
var squares =[];
function preload() {
heavySound = loadSound('super-mario-coin-sound.mp3');
mediumSound = loadSound('haiya.mp3');
lightSound = loadSound('donald-trump.mp3');
//veryLight = loadSound('assets/EMideate4.mp3');
//veryHeavy = loadSound('assets/EMideate5.mp3');
//downSound = loadSound('assets/EMideate2new.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight);
//background = (255, 0, 0);
serial = new p5.SerialPort();
serial.list();
serial.open('/dev/tty.usbmodem144101');
serial.on('connected', serverConnected);
serial.on('list', gotList);
serial.on('data', gotData);
serial.on('error', gotError);
serial.on('open', gotOpen);
serial.on('close', gotClose);
}
function serverConnected() {
print("Connected to Server");
}
function gotList(thelist) {
print("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
print(i + " " + thelist[i]);
}
}
function gotOpen() {
print("Serial Port is Open");
}
function gotClose() {
print("Serial Port is Closed");
latestData = "Serial Port is Closed";
}
function gotError(theerror) {
print(theerror);
}
function gotData() {
let currentString = serial.readLine();
trim(currentString);
if (!currentString) return;
console.log(currentString);
latestData = currentString;
newVal = round(map(latestData,0,1023,0,500));
}
function draw() {
//colorMode(RGB)
background(0);
for (var i = 0; i < balls.length; i++) {
balls[i].update();
balls[i].render();
if (balls[i].ballisFinished()) {
balls.splice(i, 1);
}
}
for (var j = 0; j < rects.length; j++) {
rects[j].update();
rects[j].render();
if (rects[j].rectisFinished()) {
rects.splice(j, 1);
}
}
for (var k = 0; k < squares.length; k++) {
squares[k].update();
squares[k].render();
if (squares[k].squareisFinished()) {
squares.splice(k, 1);
}
}
fill(255);
textAlign(CENTER,CENTER)
textSize(100)
text(latestData+'g', windowWidth/2, windowHeight/2);
}
// Polling method
/*
if (serial.available() > 0) {
let data = serial.read();
ellipse(50,50,data,data);
}
*/
function keyPressed() {
if (key == " " && (newVal >=0 && newVal < 300)) {
lightSound.play();
for (var j = 0; j < 10; j++) {
rects.push(new Rect(windowWidth/2, windowHeight/2));
}
}
else if(key == " " && (newVal >300 && newVal <400)){
mediumSound.play();
for (var i = 0; i < 10; i++) {
balls.push(new Ball(windowWidth/2, windowHeight/2));
}
}
else if(key == " " && (newVal >400 && newVal <500)){
heavySound.play();
for (var k = 0; k < 10; k++) {
squares.push(new Square(windowWidth/2, windowHeight/2));
}
}
}
// function keyReleased() {
// if (keyCode == UP_ARROW && (newVal >=0 && newVal < 150)) {
// lightSound.pause();
// }
// else if(keyCode == UP_ARROW&& (newVal >300 && newVal <400)){
// mediumSound.pause();
// }
// else if(keyCode == UP_ARROW&& (newVal >400 && newVal <500)){
// heavySound.pause();
// }
// }