Receive int value from Processing

Receive int value from Processing

Hello everyone. I know that there are several threads about this but none of them have helped me.

In summary: i want to send an int value from processing to arduino. This are my codes:

arduino:

#include <UTFT.h>

extern uint8_t SmallFont[];

int inc = 0;

UTFT myGLCD(ITDB32S,38,39,40,41);

void setup() 
  {
  Serial.begin(9600);
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  myGLCD.clrScr();
  }

void loop() 
  {
  if(Serial.available())
    {
    inc = Serial.parseInt();  //read values until a coma but not read the comma
    Serial.read();  //read the comma
    myGLCD.fillScr(VGA_BLACK);
    myGLCD.setColor(VGA_WHITE);
    myGLCD.printNumI(inc,50,50);
    }
  }

processing:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val = 0;        // Data received from the serial port

void setup() 
  {
  size(200, 200);
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  }

void draw() 
  {
  background(255);                // change color and
  val++;
  if(val>1024)
    val = 0;
  
  myPort.write(int(val));              
  myPort.write(','); 
  println(val);
  rect(50, 50, 100, 100);         // Draw a square
  }

That is all i have. In the TFT display the values start rising up to nine! I mean, it shows 0, then 1, 2...up to nine and stops! but the RX led keeps blinking (like is still receiving data)

If i use arduino IDE´s serial monitor and i send some values, they are shown correctly on the display, so i guess it is a processing problem but i can not be sure. So, has anybody received an int value from processing? Can someone help me please?

Thanks!!!

Juan.

  myPort.write(int(val));

val is an int. The purpose of casting an int to an int is?

PaulS:
The purpose of casting an int to an int is?

Pointless. I read someone did that way and i just was testing it out and i accidently left it in the code. It does not work with nor without the int().

One of the examples in serial input basics should be appropriate.

...R

processing side

Serial.write(myInt/256);
Serial.write(myInt%256);

Arduino Side

arduinoInt = Serial.read()*256 + Serial.read;

very simple method that only works with positive integers but easy to use :slight_smile: This in effect sending it as binary data. If you need signed ints can try unions

send

typedef union {
float floatNum;
long longNum;
int intNum;
byte b [sizeof(long)];
} myUnion;

myUnion dataUnion;

dataUnion.intNum = arduinoInt;
Serial.write(dataUnion.b[0]);
Serial.write(dataUnion.b[1]);

receive

typedef union {
float floatNum;
long longNum;
int intNum;
byte b [sizeof(long)];
} myUnion;

myUnion dataUnion;

dataUnion.b[0] = Serial.read();
dataUnion.b[1] = Serial.read();
arduinoInt = dataUnion.intNum;

slzer:
very simple method that only works with positive integers but easy to use :slight_smile: This in effect sending it as binary data. If you need signed ints can try unions

Too simple, methinks. What happens if the Arduino gets mixed up and uses the second byte of value 1 and the first byte of value 2. You should have some method so it can be sure it is reading the correct data - hence serial input basics which will work with binary or ascii values. You just need to parse them differently.

Unions can be very useful.

...R

I have reliably used both methods. If you know when it is being sent or use a header sequence that is followed by your set of data - I simply posted the method for sending the parts of the integers not how to handle the data validation part of it.

I dislike the parsing of ascii it is so slow and so much data to send.

slzer:
If you know when it is being sent or use a header sequence that is followed by your set of data

That is the point I was trying to make.

...R

Problem solved. Processing should send ("/n") instead of ('/n').

Thank you all.

or in this case (",") instead of (´,´)