xxxxxxxxxx
119
let serial;
let latestData = "waiting for data";
var spaceBarSound;
var clickSound;
var leftSound;
var rightSound;
var upSound;
var downSound;
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(500, 500);
//background = (255, 0, 0);
serial = new p5.SerialPort();
serial.list();
serial.open('/dev/tty.usbmodem14201');
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() {
background(255, 255, 0);
fill(0, 0, 0);
text(latestData, 10, 10);
//if (newVal >0 && <=150){
// Polling method
/*
if (serial.available() > 0) {
let data = serial.read();
ellipse(50,50,data,data);
}
*/
}
function keyPressed() {
if (keyCode == UP_ARROW && (newVal >=0 && newVal < 300)) {
lightSound.play();
}
else if(keyCode == UP_ARROW && (newVal >300 && newVal <400)){
mediumSound.play();
}
else if(keyCode == UP_ARROW && (newVal >400 && newVal <500)){
heavySound.play();
}
}
// 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();
// }
// }