element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Help, my Processing code is restarting my Arduino code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 35 replies
  • Answers 1 answer
  • Subscribers 412 subscribers
  • Views 3825 views
  • Users 0 members are here
Related

Help, my Processing code is restarting my Arduino code

kjhart0133
kjhart0133 over 12 years ago

Hello all,

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 loops 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. 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'm using an Mega Arduino board connected to my PC via the USB cable. 

I greatly appreciate any assistance on this.

Thanks,

Kevin H.

  • Sign in to reply
  • Cancel

Top Replies

  • kjhart0133
    kjhart0133 over 12 years ago in reply to mcb1 +1
    Success!! After trying a number of different things without solving the problem, I finally cut the jumper on the reset line between the USART and the Mega chip and this did the trick. These forums are…
  • kjhart0133
    kjhart0133 over 12 years ago in reply to kjhart0133 +1
    To solve the problem of now not being able to upload sketches, I soldered a wire to each of the reset line solder pads and wired an SPST switch across the two solder pads. This works perfectly (so far…
  • kjhart0133
    kjhart0133 over 12 years ago in reply to Robert Peter Oakes +1
    Peter, That's great info about the ports. I'm learning a lot in this thread. Here's a block diagram of my system that shows all the components. I run Processing code in the PC that uploads data from the…
  • Former Member
    0 Former Member over 12 years ago

    Nearly every time I've seen a problem with Arduino re-booting, it has turned out to be power issues.

    I'm not sure what the X-bee power requirements are, and I've no clue about what else you might have running off of the same power supply as the Arduino board. If you are running this whole thing on a 1 amp power supply, there is a good chance that you just need more power. The Arduino re-boots very easy any time it sees any kind of a power fluctuation.

    Double check the power requirements of everything that you have running off of the arduino and/or the arduino's power supply and make sure that your supply is rated at least about 50% higher than your estimate.

    If you have any motors that start whenever the connection is made, you need to make sure that they are not feeding noise in to the circuit.

     

    If it's not a power issue:

    Since the Arduino is not receiving any commands from the PC, I doubt it's the Processing code. The X-Bee itself does establish a connection (X-Bee attached to the PC via USB connects to other X-Bee's and the PC itself using Serial?), which might cause a small power spike as it initializes itself (See above). The only non-power related thing that I can think of which would cause a reset would have to be the x-bee code itself. Double check all of the x-bee code (I've never used an x-bee, so I can't be very specific here) that runs while it is attempting to send packets of info. If your Arduino code waits for a viable serial port connection with your PC before sending anything, that code would be a good place to start. If it ever gets stuck in an infinite loop or similar, it can crash and reset itself.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kjhart0133
    0 kjhart0133 over 12 years ago in reply to Former Member

    Bill,

     

    You may be on the right track thinking it's a power supply issue.  I'm running a Mega, a 4x20 LCD and one XBee on a shield, all from the USB cable.  The Processing code reads the data from the Mega over the USB, so that has to draw extra current, and that's when the reset occurs.  I tried hooking up a fresh 9V battery to the Mega to supplement the USB power and the Arduino code still restarted, but I still agree with you it could be the power.  I'm going to buy a hefty power supply from Adafruit and see if that helps.

     

    It will be a few days before I get the supply so stand by for the results.  If this solves it, you'll get the points for "Correct Answer!"

     

    Thanks,

     

    Kevin H.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 12 years ago in reply to kjhart0133

    Kevin

     

    Personally I haven't used processing, but the example here differs from yours

    http://www.processing.org/reference/libraries/serial/Serial_available_.html

    in that they aren't converting the port number to a string.

     

    // Example by Tom Igoe
    
    import processing.serial.*;
    
    // The serial port:
    Serial myPort;
    
    void setup() {
      // List all the available serial ports:
      println(Serial.list());
      // Open the port you are using at the rate you want:
      myPort = new Serial(this, Serial.list()[0], 9600);
    }
    
    void draw() {
      while (myPort.available() > 0) {
        int inByte = myPort.read();
        println(inByte);
      }
    }

     

     

    I seriously doubt that a Mega, LCD and Xbee are able to draw enough to drop the voltage of a 9v battery down to below 5.5 volts (or 7 if its not a LDO regulator).

    You should be able to easily measure it, which will give you a clue.

     

    The boards detect the Vin and if present doesn't use the USB power, so it will run on your 9v battery if its connected.

    If you think its spikes, then a 1000uF or larger across the 5v line should prove it.

     

    Cheers

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to mcb1

    I agree with most of what mcb1 wrote, with 1 minor note:

    Arduino Mega needs to have at least 7.5v input through the voltage reg to maintain stability(Not LDO).

    You can only pull about 500ma from a single 9v battery and maintain stability (The same as using USB).

     

    You can try wiring 2 9v in parallel to get extra juice, the larger power supply (As you plan to do), or use a USB hub with it's own dedicated power supply, which can sometimes reach as high as 2a per port on the high end ones.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 12 years ago in reply to Former Member

    Well put.

    One clue is that Kevin did say it works until he connects it, which to me suggested its not the physical supply loading that is causing the problem.

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kjhart0133
    0 kjhart0133 over 12 years ago

    I'm out of town until March 13 when I hope to have a new PS to test out these theories.

     

    Thanks for the great replies.

     

    Kevin

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago

    That sounds like a really great product I would be more then up to try it out for you.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ipv1
    0 ipv1 over 12 years ago

    The XBee CTS pin is usually wired to the reset pin of the arduino... This has caused some havoc in my past projects...

    Lemme know if this is a possible issue.

    Cheers.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • kjhart0133
    0 kjhart0133 over 12 years ago in reply to ipv1

    Mr. Singh, I think you have hit the nail on the head.  I hooked up an oscilloscope to the reset/ line on the Mega and observed that each time I started my Processing code the reset/ line was asserted low!!  Looking at the Arduino Mega schematic, it clearly shows the Mega's USART chip's CTS/ line wired to the ATmega chip's reset line.  Apparently, when the Processing code wants to read data on the Serial/USB port it sends control packets to the Mega's USART that somehow assert the Mega's CTS/ line low.  I've included a picture of the reset line being asserted when I run the Processing code.  This happens each time I start the Processing code.

     

    I guess my question now is: what do I do next?  Is there a way to 'turn off' the CTS/RTS flow control on the Mega?  The data sheet for the ATmega16u  USB controller shows a register, UCSRnD, that has a CTSEN (CTS enable) bit, but that register name is not recognized by the Arduino compiler so I don't know how to turn that enable bit off, or if that would even solve the problem.

     

    Any suggestions are appreciated.

     

    Thanks,

     

    Kevin H.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago

    With Singh's helpful post to guide me, I managed to track down a blog where someone else fixed this issue.

    Take a read and see if it helps you:

    Random Stuff - Adventures in Nerd Land: XBee woes..

     

    The important snippet is:

    "Anyway, turns out the solution for Annoyance #1, Mysteriously Resetting Arduino is also extremely simple. When using X-CTU to configure your EndDevice, simply set IOD7=0 (or use ATD7=0, same thing). The XBee will no longer reset your Arduino simply because it wants to sleep."

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube