Show Posts
Pages: [1]
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Communication lag using Arduino Mega 2560 on: February 14, 2013, 08:16:56 pm
I was able to fix it!  I google around and found this: http://arduino.cc/forum/index.php?topic=50484.0

It sounds like the issue is that the Arduino resets itself very time I start the Processing application, but things get hosed if I start sending data to the Mega too quickly.  The workaround is to put a delay into Processing's setup function:
  frameCount = 1; // required for the delay to work, since frameCount=0 during setup and the delay function requires frameCount >= 1
  delay(850);

Now it seems to be working great.  Thank you for your help!
2  Using Arduino / Interfacing w/ Software on the Computer / Re: Communication lag using Arduino Mega 2560 on: February 14, 2013, 06:05:32 pm
To give more details
1) the cutoff of how much lag is required for the Mega to work is between 830 and 840 millis
2) the cutoff is about the same for each Mega
3) when it works near the cutoff, two nulls get printed to the screen and then it's numbers.  when I'm far away from the cutoff, a single null gets printed.
4) following up on some other posts I saw, I tried running the program with the arduino editor closed.  It didn't do anything.
3  Using Arduino / Interfacing w/ Software on the Computer / Re: Communication lag using Arduino Mega 2560 on: February 14, 2013, 05:54:33 pm
Great observation, but unfortunately that didn't help either.  Any chance it could have to do with switching between boards and the port name changing?  I know that's a long shot but I'm at my wit's end, unless it's just a shortcoming of the 2560 boards, but I would find that hard to believe.

I'm putting my current code below this message again.  Thanks so much for your help!

Code:
// Arduino
void setup() {
  Serial.begin(9600);
}
char mybuffer[4];
void loop() {
  if (Serial.available() ) {
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);
    Serial.println(1000);
  }
}

// Processing code
import processing.serial.*;
Serial port;
void setup() {
  port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
  delay(100); // works when this is 1000 instead
  port.write("000\n");
  String inString = port.readStringUntil('\n');
  println(inString);
}


4  Using Arduino / Interfacing w/ Software on the Computer / Re: Communication lag using Arduino Mega 2560 on: February 14, 2013, 11:50:32 am
2.0b6
5  Using Arduino / Interfacing w/ Software on the Computer / Re: Communication lag using Arduino Mega 2560 on: February 14, 2013, 11:23:47 am
Same lag with both the Megas, but not getting it with the Uno.  Initially I thought it was a problem with my first Mega's hardware, but then I bought a new one from a different supplier and it's got the same issue.

Thanks for the tip about serialEvent, but I don't think it's as useful here; the Arduino only talk back to the computer when it's queried.  In my application (ie not the sample code I'm posting) I'm using the arduino to control some external sensors.  The computer tells the arduino the measurement it wants performed, then the arduino changes the sensor a little, performs a measurement, and relays the result to the computer.  In my mind it makes sense to include this in the draw function, but I'm a total noob with Processing so I could easily be wrong.
6  Using Arduino / Interfacing w/ Software on the Computer / Communication lag using Arduino Mega 2560 on: February 14, 2013, 12:44:19 am
I am having a weird issue with the Mega 2560 Arduino (two separate versions of it, from two separate manufacturers) that I am not having with an Uno.  I am trying to send a serial message to the board and then have it respond, but if I try to do the send-receive loop too quickly then I start just reading in nulls.  I'm putting my code (Arduino and Processing) below.  On an Uno, the Processing app prints a couple nulls and then starts printing 1000, but on both of my Megas it just prints nulls.  Subjectively, when I use the Arduino monitor, there is a fairly long lag between sending a message to the board and a response printing to the monitor.  Any idea what could be causing this, or how to fix this?  Thanks!

Code:
// ARDUINO CODE
void setup() {
  Serial.begin(9600);
}
char mybuffer[4];
void loop() {
  if (Serial.available() ) {
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);
    Serial.println(1000);
  }
}

// PROCESSING CODE
import processing.serial.*;
Serial port;
void setup() {
  port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
  delay(100);
  port.write("000\n");
  String inString = port.readStringUntil('\n');
  println(inString);
}


Moderator edit: [code] [/code] tags added.
7  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem with serial output from arduino. Processing can't read it on: January 21, 2013, 02:24:00 pm
Yes, it definitely is.  Thank you by the way for your time and help!  I'm inclined to suspect it's a problem with the serial port in general, since Processing and Python mess up in completely different ways on it.  But for some reason the Arduino monitor is working fine.
8  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem with serial output from arduino. Processing can't read it on: January 21, 2013, 12:49:19 pm
I'm afraid that doesn't help.  Here is the new code.  Note that it matches the baud rate in Processing.

// Arduino code
void setup() {
  Serial.begin(9600);
}
void loop() {
  if (Serial.available() ) {
    char mybuffer[4];
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);
    Serial.println(1000);
  }
}
9  Using Arduino / Interfacing w/ Software on the Computer / Re: Problem with serial output from arduino. Processing can't read it on: January 21, 2013, 11:17:38 am
Here are my entire Arduino and Processing programs.  One obvious thing to tweak with is the baud rate (something I admittedly don't know a lot about), but it was working fine a couple weeks ago.  Thanks!

// Arduino
void setup() {}
void loop() {
  if (Serial.available() ) {
    char mybuffer[4];
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);
    Serial.println(1000);
  }
}

// Processing
import processing.serial.*;
Serial port;
void setup() {
  port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
  delay(400);
  port.write("000\n");
  int res;
  String inString = port.readStringUntil('\n');
  if (inString != null) res = int(trim(inString));
  else res = 999;
  println(res);
}
10  Using Arduino / Interfacing w/ Software on the Computer / Problem with serial output from arduino. Processing can't read it on: January 21, 2013, 12:59:46 am
I am having a bizarre issue with the serial output from my arduino board.  I have a simple program (code below) that reads a dummy serial message from the computer and then sends a reply.  It works fine with the Arduino monitor.  But when I try to read it in with Processing it just read a null string.  And when I try to do it with Python it will read the correct output twice.  Also, when I try uploading new code onto the device there is often a timeout error, which can be fixed by unplugging and board and then re-plugging it.  This was all working fine a while ago, and the board hasn't undergone any trauma so I don't think there is a physical problem.  Any ideas?  I'm really stumped here.  Thanks for your help!


// Arduino code
// When I upload this and use the Serial monitor, putting in "000" and pressing enter
// makes "1000" print to the screen
void loop() {
  if (Serial.available() ) {
    char mybuffer[4];
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, NUM_COMM_DIGITS);
    Serial.println(1000);
  }
}

// Processing code
// Prints out a constant string of "999", rather than "1000"
void draw() {
  delay(400);
  port.write("000\n");
  int res;
  String inString = port.readStringUntil('\n');
  if (inString != null) res = int(trim(inString));
  else res = 999;
  println(res);
}

# Python code
from serial import Serial
ser = Serial("/dev/tty.usbserial-A900H0WW")
ser.write("000\n")
ser.read()  # prints '1000\r\n'
ser.read()  # prints '1000\r\n' again
ser.read()  # this time it hangs
11  Using Arduino / Sensors / analogRead fluctuates on: January 02, 2013, 11:10:50 pm
Hi everybody, I've written a program that constantly uses analogRead to measure the voltage being outputted by a sensor I made.  I am testing the voltage with a voltmeter and it's very steady, but the arduino reading is fluctuating by 0.05 to 0.2 volts around what the voltmeter says.  When I measure ground directly it seems to be fine.  Any ideas what could be the problem?  I tried grounding the analog pins that I wasn't reading from, but it didn't help.  Thanks!
Pages: [1]