Unable to receive serial data from Arduino

Hi fellows,

I’m trying for weeks to transmit data from an Arduino Leonardo to a Processing program via the serial port.

I first took over programs that I used, back in 2011, with an Arduino Duemilanove and Processing 1.5. They don’t work anymore, maybe because I since updated my system from XP to Window 7 (32 bits), or because of changes in the IDE.

I then changed to a Leonardo, with the current version of Processing (3.5.3). I reproduce below one of the programs I used.

The Arduino’s serial monitor show exactly what is expected: values from 0 to 1023 as I turn the potentiometer connected to analog input. On the Processing side, I just get random numbers, independents of the pot position (Arduino serial monitor disabled, yes).

Arduino program::

//To send on the serial port a value defined by the
//cursor of a potentiometer

#define POT 0//assigns input 0 to the potentiometer
int val = 0; //val is the input variable

void setup() {
  Serial.begin(9600);//open the serial port and
  // set the speed at 9600 bit/sec
}

void loop() {
  val = analogRead(POT);//read the voltage on pin0 and name it val
  Serial.print(val);//transmit val to the serial port
  delay (10); //wait 10mS
}

Processing program:

import processing.serial.*;
Serial port;
int var;
void setup() {

  port = new Serial(this, "COM5", 9600);// com5 is the port currently used
}//by the Arduino

void draw() {
  if (port.available()>0) {
    var=port.read();
    println(var);
    delay(100);
  }
}

Thanks for any help

1 Like

You are receiving what you are sending:

val = 255;
Serial.print(val);

Will send ASCI characters 50, 53, 53 for 2, 5, 5, and that is what you are seeing.

https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

Try:

val = 255;
Serial.write(val);

And Processing will receive:

Also, you will not be able to send greater than 255 with this example since you are sending bytes and the int will be cast to a byte.
Try the code with the val++ and you will see this.
I will leave that as an exercise for further exploration.

:slight_smile:

2 Likes

i would avoid byte things at all,
stick with readable ASCII you can follow in arduino monitor ( terminal )
and easy debug in processing.
ARDUINO


// https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100
// kll tested also with a arduino leonardo

//Arduino program::
//To send on the serial port a value defined by the
//cursor of a potentiometer

#define POT 0       //assigns input 0 to the potentiometer
int val = 0;        //val is the input variable

void setup() {
  Serial.begin(9600);//open the serial port and
  // set the speed at 9600 bit/sec
}

void loop() {
  val = analogRead(POT);//read the voltage on pin A0 and name it val
//  Serial.print(val);//transmit val to the serial port
  Serial.println(val);//transmit val to the serial port
//  delay (10); //wait 10mS ( 100 lines per sec ?)
  delay (100); //wait  ( 10 lines per sec  better start slow )
}


PROCESSING 3.5.3

import processing.serial.*;
Serial myPort;
String data, val="NaN";
int var;

void setup_serial() {                                 // USB arduino..
  printArray(Serial.list());
  String portName = Serial.list()[1];         // adjust you arduino USB port
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  println("try connect to "+portName);
}

void serialEvent(Serial p) {                          // handle serial data
  data = trim(p.readStringUntil('\n'));
  if (data != null) {
    val = data;
    var = int(val);
    println("val "+val+" var "+var);            // print every GOOD line and if it is understood 
  }
}

void setup() {
  size(640, 360);
  setup_serial();
}

void draw(){}

2 Likes

Yes, now it works! I tried the loop you suggested, then the analogRead, and it prints what is expected. Thanks a lot for your help! I had fiddled a bit with the various ways of sending and receiving data, but probably not in the right combination. Now my question is: how do I deal with the 10 bits value coming from the analog input? How can I receive it at the Processing side and convert it in a integer value?

1 Like

I am pleased that you took the path of exploration! :slight_smile:

Try the example provided on the Processing site:
https://processing.org/reference/libraries/serial/Serial_bufferUntil_.html

The only thing I did was remove the font since I did not wish to create one.

// Example by Tom Igoe

import processing.serial.*;

Serial myPort; // The serial port
PFont myFont; // The display font
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed

void setup() {
size(400,200);
// You’ll need to make this font with the Create Font Tool
//myFont = loadFont(“ArialMS-18.vlw”);
//textFont(myFont, 18);
// List all the available serial ports:
printArray(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
//myPort = new Serial(this, Serial.list()[0], 9600);
myPort = new Serial(this, “COM7”, 9600);
myPort.bufferUntil(lf);
}

void draw()
{
background(0);
text("received: " + inString, 10,50);
}

void serialEvent(Serial p)
{
inString = p.readString();
}

On the Arduno side you are going back to sending ASCII characters:

val++;
Serial.println(val);

You are now sending ASCII characters along with a LF (Line Feed) and CR (Carriage Return) and Processing is receiving these into a string; you will have to “trim” and convert the received string to an int if you want to use an int variable.

Consider:
https://processing.org/reference/intconvert_.html

This is a simple example without extra characters in string to “trim”:

String p = “1023”;
print (int ( p));

Also consider:
https://processing.org/reference/trim_.html

:slight_smile:

Still counting:
image

2 Likes

It works fine too, thank you!

1 Like