xxxxxxxxxx
193
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 =[];
let newVal = 0;
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');
font = loadFont('NeueBit-Regular.otf')
}
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(constrain(map(latestData,700,1023,0,500),0,500),2);
console.log(latestData + " / " + newVal);
}
function draw() {
//colorMode(RGB)
background(0);
push();
fill(100,250,87);
noStroke();
rect(width*0.25,height*0.25,width*0.5,height*0.5,20)
pop();
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);
//}
}
let tar = latestData-810
textAlign(CENTER,CENTER)
textSize(100)
if (newVal==0) {
push();
fill(0);
textFont(font);
text('0g', width/2, height/2+ 10);
textSize(40);
fill(255)
text("Weigh your fruit on the Chopping Board", width/2, 100);
pop();
} else {
push();
fill(0);
textFont(font);
text(newVal+'g', width/2, height/2+ 10);
pop();
}
//if ((latestData-750) > 0 && (latestData-750) < 50) {
// Polling method
/*
if (serial.available() > 0) {
let data = serial.read();
ellipse(50,50,data,data);
}
*/
}
function keyPressed() {
if (key == " " && (newVal >=0 && newVal < 200)) {
lightSound.play();
for (var j = 0; j < 10; j++) {
rects.push(new Rect(windowWidth, 0));
}
}
else if(key == " " && (newVal >200 && newVal <400)){
mediumSound.play();
for (var i = 0; i < 10; i++) {
balls.push(new Ball(0, windowHeight/2));
}
}
else if(key == " " && (newVal >400 && newVal <500)){
heavySound.play();
for (var k = 0; k < 10; k++) {
squares.push(new Square(windowWidth, 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();
// }
// }