Newbie question

Hi Guys,

I have a question regarding the communication between processing and arduino.
I want to move a servo based on the position of the mouse on the screen but nothing is happening,
from my understanding processing send some data that arduino doesn’t understand but since I
can’t trace from the arduino interface so I don’t have any idea what is going wrong…
I load the following code to arduino:

#include <Servo.h>

Servo servo1;

void setup() {

  pinMode(1,OUTPUT);
  servo1.attach(14); //analog pin 0
  
  Serial.begin(9600);

}

void loop() {

servo1.write(Serial.read());

}

and the following code to processing:

import processing.serial.*;        


int gx = 15;
int gy = 35;
int spos=90;

float leftColor = 0.0;
float rightColor = 0.0;
Serial port;                         // The serial port



void setup() 
{
  size(720, 720);
  colorMode(RGB, 1.0);
  noStroke();
  rectMode(CENTER);
  frameRate(100);

  println(Serial.list()); // List COM-ports

  //select second com-port from the list
  port = new Serial(this, Serial.list()[1], 19200); 
}

void draw() 
{
  background(0.0);
  update(mouseX); 

}

void update(int x) 
{
  //Calculate servo postion from mouseX
  spos= x/4;

   port.write(spos); 
print(spos+"\n");
}

but nothing is happening…
Any help is highly appreciated :slight_smile:

Serial.begin(9600);
port = new Serial(this, Serial.list()[1], 19200);

You have to talk and listen at the same speed.

I changed the port but still nothing is happening... it's like if the values send from Processing 0-180 are in a format that the
arduino doesn't understand...

I changed the port but still nothing is happening

it's like if the values send from Processing 0-180 are in a format that the arduino doesn't understand...

Which is it? If the data were being sent in a format that the Arduino didn't understand, the servo would be twitching all over the place.

Lets start with the basics. Does the RX light on the Arduino come on at all? It should be on nearly steady because the Processing application is just flooding the serial port with data.

Yes the RX light is constantly on. I tried to print the data that is send to the arduino but when I wrote print(Serial.read());
and I click the serial monitor I get an error, I guess because the processing monitor is on....

Yes the RX light is constantly on. I tried to print the data that is send to the arduino but when I wrote print(Serial.read());
and I click the serial monitor I get an error, I guess because the processing monitor is on....

So, the Processing application is able to talk to the Arduino. So, you want to replace, temporarily, servo1.write() with Serial.println(), to send the received data back to Processing.

In the Processing application, add:

  port.bufferUntil('\n');

to the end of setup. Then, add:

void serialEvent(Serial myPort)
{
  boolean validString = true;  // whether the string you got is valid
  String errorReason = "";     // a string that will tell what went wrong

  // read the serial buffer:
  String myString = port.readStringUntil('\n');

  // make sure you have a valid string:

  if (myString != null)
  {
     println("Received: [" + myString + "]");
  }
}

to the end of the Processing sketch.

Then, when you run Processing, the data that the Arduino gets will be echoed back.

Show some sample output, and we can figure out what is going wrong.

I’ve applied the suggested changes but I’m not getting anything back, the SerialEvent doesn’t fire at all
but the RX led is on. Here is how the code looks like now.
ARDUINO:

#include <Servo.h>

Servo servo1;

void setup() {
 pinMode(1,OUTPUT);
 servo1.attach(14); //analog pin 0
 Serial.begin(9600);
}

void loop() {
//servo1.write(Serial.read());
 Serial.println(Serial.read());
}

PROCESSSING:

// Use the included processing code serial library
import processing.serial.*;        

Serial port;                         // The serial port

int spos=90;

void setup() 
{
  size(720, 720);
  colorMode(RGB, 1.0);
  noStroke();
  rectMode(CENTER);
  frameRate(100);

  println(Serial.list()); // List COM-ports

  //select second com-port from the list
  port = new Serial(this, Serial.list()[1], 9600); 
  
port.bufferUntil('\n');
}

void serialEvent(Serial myPort)
{
  boolean validString = true;  // whether the string you got is valid
  String errorReason = "";     // a string that will tell what went wrong

  // read the serial buffer:
  String myString = port.readStringUntil('\n');

  // make sure you have a valid string:

  if (myString != null)
  {
     println("Received: [" + myString + "]");
  }
}

void draw() 
{
  background(0.0);
  update(mouseX); 
}

void update(int x) 
{
  //Calculate servo postion from mouseX
  spos= x/4;

  //Output the servo position ( from 0 to 180)
  port.write(spos); 
//print(spos+"\n");
}
void loop() {
//servo1.write(Serial.read());
 Serial.println(Serial.read());
}

Where is your call to Serial.available()? If there is nothing to read, why are you reading?

I've applied the suggested changes but I'm not getting anything back, the SerialEvent doesn't fire at all but the RX led is on.

Are you sure that it is the RX light that is on, not the TX light?

SUCCESS!
Thanks a lot :slight_smile:
I've added the if( Serial.available()) and everything was fixed.
One last thing that I don't understand... why do we need the condition?
From my understanding If Serial is unavailable nothing is going to be send, so why do we need
to add the condition to make it work?

From my understanding If Serial is unavailable nothing is going to be send, so why do we need
to add the condition to make it work?

unavailable doesn't mean that the serial function is not working, it means that there has not been a complete serial character received yet. When at least one character or more characters has been completely received then the unavalible test will return a positive number and the value of the number is how many characters are waiting in the buffer to be read.

Lefty