Pages: [1]   Go Down
Author Topic: Arduino and Processing communication  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

Pardon my English I am French student
I have a problem of communication between an arduino UNO rev3 and processecing
I would like know how to do communicate the two
I managed to retrieve a value, I can display it independent but I cannot use it as a variable for a test

Code:
  text(Power,10,10);  //OK

Power is a String value

Code:
 void Etat_Arduino(int X,int Y)
  {
   if (Power == "Y")  //Yes or No     the test is always wrong even when the test should be true
   {
   text("OK",X,Y);
   }
   else
   {
   text("ERROR",X,Y);
   }
  }
 
However, if I declare
Code:
String Power = "Y";
The program displays "OK"
but I am obliged to declare
Code:
String Power = "N"; or "";

Could you help me,
where can I find tutorials to explain the communication between Arduino and Processing?
My program must receive the Arduino variables but also be capable of change them

Thank you
« Last Edit: May 04, 2012, 03:48:56 pm by P390 » Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1010
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you read this: http://www.arduino.cc/playground/Interfacing/Processing
On this page are examples at section "4. communication":  http://arduino.cc/hu/Tutorial/HomePage
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I read.
it slightly helped me. I can not always run my test

Code:
String Power = "";

if (Power == "1") //OK => Connect
{
text();
}
else // No Connect
{
text();
}

void serialEvent (Serial port)
  {
    data = port.readStringUntil('.');
    data = data.substring(0, data.length() - 1);
   
    index_Power = data.indexOf(",");     //  My arduino send "1"
    Power = data.substring(0, index_Power );
 
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46003
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That snippet makes it appear that serialEvent() is defined inside the draw() function. That is most certainly NOT allowed.

It is why we don't want you posting snippets. Post ALL of your code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 import processing.serial.*;
  Serial port;
  
  String data = "";
  String Power = "No Connect !";
  int index_Power = 0;
  int index_Alarme =0;
  
  void setup()
  {
   background(0,0,0);
   size(600,400);
   port = new Serial(this, "COM11", 57600);
   port.bufferUntil('.');
  }  
  
  void draw()
  {
  Etat_Arduino(Power,112,103); //print : "ERROR !" instead of "OK !"
  fill(46, 209, 2);
  text(Power, 70, 320);   //print : "1"
  }
  
  void serialEvent (Serial port)
  {
  data = port.readStringUntil('.');
  data = data.substring(0, data.length() - 1);
  index_Power = data.indexOf(",");
  Power = data.substring(0, index_Power );
  }
  
  void Etat_Arduino(String V,int X,int Y)
  {
  if (V == "1")
  {
  text("OK !",X,Y);  
  }
  else
  {
  text("ERROR !",X,Y);
  }
  }

I would also like to change the variable that the program of the arduino send me, for example, it sent me "Power = 1" and I want to "Power = 0" so I sent it 0, and I will add another variables, how can I do for the arduino to receive. and processing sent it?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46003
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is sending data to the Processing application?

Code:
  data = port.readStringUntil('.');
So, what is in data now?

Code:
  data = data.substring(0, data.length() - 1);
Make a substring of all the characters in data, and store in data. How useful is that?

Code:
index_Power = data.indexOf(",");
Suppose data doesn't contain a ,. What will be the value in index_Power?

Code:
  Power = data.substring(0, index_Power );
Does is make sense to always call this function? What is the output if index_Power is -1?

Code:
  Etat_Arduino(Power,112,103); //print : "ERROR !" instead of "OK !"
What does this comment mean? If you want the function to always print "ERROR!", remove the code that does otherwise.

Quote
I would also like to change the variable that the program of the arduino send me, for example, it sent me "Power = 1" and I want to "Power = 0" so I sent it 0, and I will add another variables, how can I do for the arduino to receive. and processing sent it?
Is the Arduino the master, and the Processing application the slave? If so, it doesn't make sense for you to be modifying what the Arduino sent, and telling it it was wrong.

The Processing sketch that you posted is not expecting an input string that contains "Power = 1" or "Power = 1" or any variation that contains the string "Power =". There is nothing in the Processing application that changes the value in Power, except for that code that sets it based on what it receives from the serial port.

You haven't posted the Arduino code at all.

So, it isn't clear how to help you.
Logged

Pages: [1]   Go Up
Jump to: