I am new here! I have a school project that I am working on. I am trying to make a etch-a-sketch using an Arduino Uno, Arduino IDE, and p5.js. On the Arduino I have two potentiometers and a button. All three values are being read correctly by the Arduino IDE and in p5.js. The issue I am having is mapping correctly. The potentiometers are drawing without using the mapping that my professor suggested (I am not sure why) but I am trying to get the button to clear the canvas when it is pressed. The logic I am using does not work. Any help is appreciated.
let prevX, prevY, button;
let isCleared = false;
let serialPortName = '/dev/tty.usbserial-1130';
let serial;
let sensors = [200,200,0];
function setup() {
createCanvas(400, 400);
prevX = width/2;
PrevY = height/2;
serial = new p5.SerialPort();
serial.open('/dev/tty.usbserial-1130');
serial.on('data', gotData);
serial.on('open', gotOpen);
}
function draw() {
if (isCleared ==true){
background(250);
isCleared = false;
}
// prevX = map(prevX, 0,1023, 0, 200);
// prevY = map(prevY, 0,1023, 200,0 );
strokeWeight(10);
line(prevX, prevY, sensors[0], sensors[1]);
prevX = sensors[0];
prevY = sensors[1];
//button = sensors[2];
//prevX = map(prevX, 0,1023, 0, 200);
// prevY = map(prevY, 0,1023, 200,0 );
}
function gotData() {
let currentString = serial.readLine();
trim(currentString);
if (!currentString) return;
sensors = split(currentString, ',');
console.log(sensors);
serial.write('A');
} if (sensors[2] == 1){
isCleared = true;
}
function gotOpen(){
print("Serial Port is Open");
serial.clear(); // slears the buffer of any outstanding data
serial.write('A'); // send a byte to the Arduino
}
//function buttonPressed(){
// if (sensors[2] == 1){
// isCleared = true;
//}
//}
A link to the Tinkercad where the Arduino IDE code can be viewed as well as how I set up the physical arduino: https://www.tinkercad.com/things/2aAKAbBGa4M-fantabulous-blorr-hillar/editel
