Go Down

Topic: Slow Serial Port connection with processing (Read 5299 times) previous topic - next topic

PaulS

The first example has problems. It send a new value each time through draw(), depending on whether or not the mouse is in the square, or not.

Try this code in Processing, instead:
Code: [Select]
import processing.serial.*;

Serial myPort;            // the serial port
int incomingValue =0;     // the value received in the serial port

void setup()
{
  // list all the available serial ports:
  println(Serial.list());

  // Open the appropriate serial port. On my computer, the RF
  // receiver is connected to a USB-to-serial adaptor connected to
  // the first port in the list. It may be on a different port on
  // your machine:

  myPort = new Serial(this, Serial.list()[1], 9600);
}

void draw()
{
  myPort.write('H');
//  println("Turn led on");
  delay(10);
  myPort.write('L');
//  println("Turn led off");
  delay(10);
}

When I run this code, the LED turns on and off as I expect.

What does it do for you?
The art of getting good answers lies in asking good questions.

steam229

#16
Sep 09, 2015, 11:38 am Last Edit: Sep 09, 2015, 11:41 am by steam229
it blinks as you expect, but it keeps blinking after for a few more seconds.
anyway, it starts blinking at the same time the program starts.

I used the native example before Processing 3, and I never got any lag problems.
and it worked.

To
(...) send a new value each time through draw(), depending on whether or not the mouse is in the square, or not.


is the idea I want to start from, for example.


PaulS

#17
Sep 09, 2015, 11:46 am Last Edit: Sep 09, 2015, 11:47 am by PaulS
Quote
is the idea I want to start from, for example.
Why? Sending a value ONCE when crossing into, or out of, the box makes sense. Continually spamming "I'm in the box" or "I'm out of the box" does not.

Anyway, the point is to define whether the serial data transfer is really slow, or if the spamming is the problem.
The art of getting good answers lies in asking good questions.

steam229

#18
Sep 09, 2015, 11:59 am Last Edit: Sep 09, 2015, 12:08 pm by steam229
Also delay() is no longer a processing function (since Processing 2) so even if you "delay(100);" you get the same nervous blink.

and the post lag to turn off is really really strange;

Like I  told you, I think there is need to debug whatever changed on the serialIO relation between Arduino and Processing and not with a specific piece of code.

steam229

Now it's working and no lag!! can't understand this:

Code: [Select]
import processing.serial.*;

Serial myPort;            // the serial port
int incomingValue =0;     // the value received in the serial port

void setup()
{
  // list all the available serial ports:
  println(Serial.list());

  // Open the appropriate serial port. On my computer, the RF
  // receiver is connected to a USB-to-serial adaptor connected to
  // the first port in the list. It may be on a different port on
  // your machine:

  myPort = new Serial(this, Serial.list()[7], 9600);
}

void draw()
{
  myPort.write('H');
//  println("Turn led on");
  delay(500);
  myPort.write('L');
//  println("Turn led off");
  delay(500);
}

void delay(int delay)
{
  int time = millis();
  while(millis() - time <= delay);
}

PaulS

Quote
can't understand this:
At least you have a building block.

delay() being removed was stupid.
The art of getting good answers lies in asking good questions.


steam229

Now I tried the opposite way with

Code: [Select]
int sensorPin = A0;
int ledPin = 13;

void setup() {
  pinMode (ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  if (analogRead(sensorPin) > 500) {
    digitalWrite (ledPin, HIGH);
    Serial.println("1");
  } else {
    digitalWrite (ledPin, LOW);
    Serial.println("0");
  }
  delay(100);
}


on arduino

and

Code: [Select]
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[7];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();
    println(val);// read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  }
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}


on processing


also on arduino side I tried different write to serial port methods:

serial.Write("int");
/
serial.println(int);
/
serial.println("int");

and all gave me different results with the println on the processing side:

for example, the last one:

"
10
48
13
10
49
13
10
49
13
10
49
13
10
49
13
10
49
13
10
48
13
10
48
13
10
48
13
10
48
13
10
49
13
10
49
13
10
49
13
10
"

thanks again!

PaulS

The 13 and 10 are the carriage return and line feed that the ln part of the name is responsible for adding.

The 48 and 49 ('0' and '1') look are like Serial.println(int); was used.
The art of getting good answers lies in asking good questions.

steam229

Thank you very much for your help:
now i got things to work, it a great sequence of misunderstandings by me:

1. the serial write example on processing is, like you very well detected, wrong;
2. the physical pixel example on arduino ide works perfectly;
3. The Graph example on Arduino ide has something wrong on the processing side.

in the end, through a sequence that I can't figure, it led me to wrong conclusions that without your help were impossible to detect...

Thanks a lot,

Go Up