Hello, Arduino community!
I`m a new explorer in the Arduino world and my last attempt was to write a little program that was supposed to be able to stablish serial communication between Arduino and Processing via USB. I hope this is the proper section for this post, since my main problem is related to serial communication.
So, my objetive is to send RGB analog values that come from the mouse position in Processing window.
The codes:
Arduino:
int outR = 7;
int outG = 8;
int outB = 9;
int serialCount = 0;
int in[4];
int Rv = 0;
int Gv = 0;
int Bv = 0;
boolean firstContact = false;
void setup(){
Serial.begin(9600);
pinMode(outR, OUTPUT);
pinMode(outG, OUTPUT);
pinMode(outB, OUTPUT);
/* This is a "synchronization"part of the program.
I did this because I believe the information sent and
received is not being well understood when getting
from one dispositive to another. I`m not sure if this is
necessary, but the "establishContact" function found at
SerialCallResponse example doesn`t seems to be enough and,
in a similar example, the "Dimmer", it made the example work for me.*/
Sync1: Serial.write('O');
delay(10);
while(!Serial.available()){
delay(1);
goto Sync1;
}
while(Serial.read() != 'O')goto Sync1;
Sync2: Serial.write('k');
delay(10);
while(!Serial.available()){
delay(1);
goto Sync2;
}
while(Serial.read() != 'k')goto Sync2;
Serial.flush();
}
void loop(){
}
void serialEvent(){
int inByte = Serial.read();
Serial.write(inByte);
if (firstContact == false) {
if (inByte == 'k') {
Serial.flush(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
Serial.write('k'); // ask for more
}
}
else{
in[serialCount] = inByte;
serialCount++;
}
if(serialCount > 2){
Rv = in[0];
Gv = in[1];
Bv = in[2];
analogWrite(outR, Rv);
analogWrite(outG, Gv);
analogWrite(outB, Bv);
//debug purpose prints
Serial.print(Rv);
Serial.print(" ");
Serial.print(Gv);
Serial.print(" ");
Serial.println(Bv);
Serial.write('k');
//
serialCount = 0;
}
}
Processing:
import processing.serial.*;
Serial port;
boolean ready = false;
int read;
char k = 'k';
void setup(){
// //set Serial
println(Serial.list());
port = new Serial(this, Serial.list()[0] , 9600);
// //end set Serial
// //Sync
char rightAnswer = 'O';
while(ready == false){
if (port.available() != 0){
if(port.read() == rightAnswer) {
port.write(rightAnswer);
if(rightAnswer == 'k'){
ready = true;
println("Ok");
}
rightAnswer = 'k';
}
}
}
port.clear();
// //End Sync
// //Set Screen
size(256, 256);
for(int x = 0; x < 256; x++){
for (int y = 0; y < 256; y++){
stroke(x,y,127);
point(x,y);
}
}
establishContact();
}
void draw(){
while (port.available() > 0){
int a = port.read();
//Write Red
port.write(mouseX);
//Write Green
port.write(mouseY);
//Write Blue
port.write(127);
//wait for a while
delay(100);
}
}
void establishContact(){
while(port.available() <= 0){
port.write('k');
}
}
My problem is that, using Serial Monitor, the arduino program runs fine, but by sending the same information through the Procesing program, it doesnt work! It first stucks at establishContact()(keeps sending the 'k', so I believe arduino is not reading 'k' as 'k'), and then stucks in a white window in processing, when it was supposed to be a colored window due to "set Screen"part( the RX and TX keeps blinking during this part, so I believe it
s a mismatch of information being sent/received).
Thanks in advance,
Lucas Bortoto Rocha