Hello! I am a newbie in Arduino and Processing and I am having some issues with receiving data using serial call response. Processing outputs Error Disabling SerialEvent() for COM6 null and does not print the data i send from the Arduino. The code I use is given below:
Arduino Code:
int firstSensor = 10; // first analog sensor
int secondSensor = 250; // second analog sensor
int thirdSensor = 255; // digital sensor
float keks=4.23;
int kek100=0;
int mul=0;
int rem=0;
int inByte = 0; // incoming serial byte
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(2, INPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
Serial.write(firstSensor);
Serial.write(secondSensor);
Serial.write(thirdSensor);
delay(10);
kek100=keks*100;
mul=ceil(kek100/255);
rem=((kek100/255)-mul);
Serial.write(rem);
Serial.write(mul);
Serial.println();
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
Processing Code:
import processing.serial.*;
int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
int mul,rem;
float ph;
void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
// Print a list of the serial ports, for debugging purposes:
printArray(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
// myPort.bufferUntil('B');
//myPort.bufferUntil('\n');
}
void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 5 ) {
xpos = serialInArray[0];
ypos = serialInArray[1];
fgcolor = serialInArray[2];
// delay(100);
rem=serialInArray[3];
mul=serialInArray[4];
ph=((4*mul)+rem)/100;
// print the values (for debugging purposes only):
println(xpos + "\t" + ypos + "\t" + fgcolor + "\t" + ph);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
Please don’t pay attention to sensors 1 to 3, they are just for testing right now.