xxxxxxxxxx
57
let obj;
let distance;
let oWidth = 200; //original width
let oHeight = 200; //original height
let rightValueMin = 300; //min value of right photoresistor
let rightValueMax = 450;
let frontValueMin = 200;
let frontValueMax = 675;
let prevFront = 0;
let prevRight = 0;
class Box{
constructor(x,y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
display(){
rect(this.x-this.w/2, this.y-this.h, this.w,this.h);
}
}
function setup() {
createCanvas(1000, 500);
obj = new Box(0,height-oHeight,oWidth,oHeight);
}
function draw() {
background(220);
obj.display();
if(!serialActive){
console.log("not connected");
}
}
function mousePressed(){
if(!serialActive){
setUpSerial();
}
// console.log(obj.w)
}
function readSerial(data){
if(data != null){
let fromArduino = split(trim(data), ",");
front = map(int(fromArduino[0]),frontValueMin,frontValueMax,5,0);
right = map(int(fromArduino[1]),rightValueMin,rightValueMax,0,width-obj.w);
obj.w = int(oWidth*front);
obj.h = int(oHeight*front);
obj.x = right;
}
}