help, my Processing code is restarting my Arduino code

edited February 2014 in Arduino

Hello all, I'm new to this forum, and new to Processing as well so bear with me, please. I'm developing some Arduino code using XBees to sense environmental data at a remote location. My Arduino code is working great and I'm now ready to upload my remotely acquired data to my computer so that I can parse the data and send it via an SMS message to my smart phone. This way I can monitor my remote location from anywhere.

I'm using a chunk of code I got from the "Getting Started with Processing" book by Reas and Fry. I am able to get the Arduino/XBee data into my computer this way, but I have a problem/issue:

The Arduino code runs continually, but each time I start the Processing Code, it restarts (resets?) the Arduino code. The Arduino code starts over from the beginning going through setup and then into the loop. I have no idea why this is happening. Here is the Processing code I'm using:

// Based on Example 11-07 from "Getting Started with Processing" 
// by Reas & Fry. O'Reilly / Make 2010
// This sketch, as modified by kjh, reads data from an Arduino, stores it in an array
// then saves the data to a file on disk.

import processing.serial.*;

Serial port;                     // Create object from Serial class
int asize = 8;                 // define size of data array
byte[] val = new byte[asize];    // Data received from the serial port
                                 // representing the setting of the pot
int i = 0;                       // array index
int x;                           // scratch value
float y;

void setup() {
  //size(440, 220);
  // IMPORTANT NOTE:
  // The first serial port retrieved by Serial.list()
  // should be your Arduino. If not, uncomment the next
  // line by deleting the // before it. Run the sketch
  // again to see a list of serial ports. Then, change
  // the 0 in between [ and ] to the number of the port
  // that your Arduino is connected to.
  println(Serial.list());
  String arduinoPort = Serial.list()[2];
  port = new Serial(this, arduinoPort, 9600);
}

void draw() {
    if (port.available() > 0) {        // If data is available,
      x = port.read();        // read it and store it in val
      val[i] = byte(x);
      y =  float(x);
      //println(y);
      println(val[i]);
      //val = map(val, 0, 255, 0, height);  // Convert the value
      i++;
    }
    if (i > asize-1){
      saveBytes("arduino_data.dat", val);
      println("Data written.");
      exit();
    }
}

My Arduino code does not read data from the computer or Processing, it only sends data to processing (Tx). The Arduino code is quite long and involved using a fair amount of XBee interface code so I didn't include it here. But, as I said, it is running fine except for being restarted each time I run the Processing code.

I greatly appreciate any assistance on this.

Thanks,

Kevin H.

Answers

  • edited March 2014

    I am not really sure what you mean by resets the arduino code? Do you refer to the arduidno_data.dat file being overwritten or perhaps it's something else?

  • edited March 2014

    If you refer to the data inside the file being overwritten then it's possible this being done by saveBytes. There is a forum post here http://forum.processing.org/one/topic/overwritting-a-dat-file-with-savebytes.html

  • Sorry for the delay getting back to this question. I've been on travel.

    After some more investigation it appears that the when the Processing code tries to read serial data from the Arduino Mega it is somehow instructing the USART on the Mega to assert CTS/ low. Since the CTS/ line is wired to the reset/ line on the Mega, this results in the reset/ line on the Mega being asserted low - and the whole Mega board resets. I hook my oscilloscope to the reset/ line and I can see it being driven low each time I start the processing code.

    I guess my question now is: how do I stop the CTS/ line from being asserted by the Processing code?

  • Answer ✓

    try...

      println(Serial.list());
      String arduinoPort = Serial.list()[2];
      port = new Serial(this, arduinoPort, 9600);
    delay (3000); //new line
    }
    
  • Well, after trying a lot of different things, I finally cut the reset line between the USART and the Mega chip and it did the trick. Don't know why the serial USB causes CTS to be asserted, or why it's connected to the reset line, but cutting that short trace between the two solder pads on the Mega board solved my problem.

    Thanks to all for the helpful answers.

    Kevin H.

Sign In or Register to comment.