'Serial communication won't print out any results
I am pretty new to all of this and really need to get this working in time for my assessments to come around. Unfortunately, I am completely stuck. I've been able to make p5.js read the output of a single button and of a joystick and react accordingly, but I can't get it to print out the value, I would be getting out of my setup.
Setup: 4 buttons, that all get checked for being on or off and a value variable that tells me if and which buttons are on and off by adding specific numbers together. (Can post the Arduino Code if necessary)
Is there any reason my Code in P5.Js isn't working right now? (No output whatsoever from serial communication) (probs to Scott Fitzgerald for the code groundwork)
let serial; // variable for the serial object
let value = "waiting for data"; // variable to hold the data
function setup() {
createCanvas(windowWidth, windowHeight);
// serial constructor
serial = new p5.SerialPort();
// get a list of all connected serial devices
serial.list();
// serial port to use - you'll need to change this
serial.open('COM7');
// callback for when the sketchs connects to the server
serial.on('connected', serverConnected);
// callback to print the list of serial devices
serial.on('list', gotList);
// what to do when we get serial data
serial.on('data', gotData);
// what to do when there's an error
serial.on('error', gotError);
// when to do when the serial port opens
serial.on('open', gotOpen);
// what to do when the port closes
serial.on('close', gotClose);
}
function serverConnected() {
console.log("Connected to Server");
}
// list the ports
function gotList(thelist) {
console.log("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
console.log(i + " " + thelist[i]);
}
}
function gotOpen() {
console.log("Serial Port is Open");
}
function gotClose() {
console.log("Serial Port is Closed");
value = "Serial Port is Closed";
}
function gotError(theerror) {
console.log(theerror);
}
// when data is received in the serial buffer
function gotData() {
let currentString = serial.readLine(); // store the data in a variable
trim(currentString); // get rid of whitespace
if (!currentString) return; // if there's nothing in there, ignore it
console.log(currentString); // print it out
value = currentString; // save it to the global variable
}
function draw() {
background(255, 255, 255);
fill(0, 0, 0);
text(value, 10, 10); // print the data to the sketch
// in this example, we are reciving a 0 and a 1
// if the button is not pressed we get a 0
//if (latestData == 0) {
// ellipse(width / 2, height / 2, 100, 100);
//} else { // if it is pressed, we get a 1
// rectMode(CENTER);
//rect(width / 2, height / 2, 100, 100);
if (value == 0) {
console.log("NO SENSOR")
}
// Test first bit:
if (value % 2 == 1) {
console.log("FIRST SENSOR")
}
// Test second bit:
if ((value >> 1) % 2 == 1) {
console.log("SECOND SENSOR")
}
if ((value >> 2) % 2 == 1) {
console.log("THIRD SENSOR")
}
if ((value >> 3) % 2 == 1) {
console.log("FOURTH SENSOR")
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|