duemilanove locks up using serial

I've done pretty well learning and reading on my own, but I'm stuck now and really need some help.

I'm writing a program to have a computer talk to it over usb/serial and have it send commands to turn pins on or off, or set pwm. simple concept. I get it almost finished, then I make some unrelated changes and upload it, then either the chip or the serial port locks up at the first point it has a command to send a byte variable to the port. I can't figure out why it's doing that, or what could even cause it. I can't get my program working again. I already rewrote it the last time I ran into this bug, now I got it again. any ideas?

here's an example, this is in the beginning of the setup function:
#define pin_START 2
#define pin_LAST 12
void setup(){
Serial.print("loading pins: ");

this is what I get on the screen:
loading pins:

then it hangs

if it matters to anyone, I'm posting the finished code on my website as open source. I'd like to get it posted soon, and start using it.

here's an example, this is in the beginning of the setup function:
#define pin_START 2
#define pin_LAST 12
void setup(){
 Serial.print("loading pins: ");

Is that the actual code, as I don't see a Serial.begin(speed) statement?
It's always better to post the actual and complete code when seeking troubleshooting help with software problems. I'm also not use to seeing a defined value used as a variable, but I'm a little too experienced in C to say that is wrong.


I really hope someone can explain this for me. I finally got my program working by commenting line by line. the code I was explaining in my first post is where it's locking up. what I found is both of these lines have to be commented out for it to work. they are much further down in the program and don't get called until after the user interacts with the serial console. so it's a bug in the compiler?

// Serial.print(" Set Port Memory");
// Serial.print("=================");

almost all of it was the real code. I just typed in the function line. here's the actual code. want me to upload the whole code to my website and link to it? you can try compiling it and repeating the problem if you want. btw, I'm running windows xp service pack 2, version 0018 of the arduino ide.

// pin range
#define pin_START 2
#define pin_LAST 12

void setup(){
Serial.begin(9600); delay(100);

// init pins
Serial.print("loading pins: ");

also, I'm just sending a defined value to the serial port because.. it's just the result I needed. I needed a preset range of ports to use, and I needed to send the same number to the serial port. I wasn't completely sure it would work when I typed it, so feel free to show me a better way to do it, but it does work. if I comment out those lines, it locks up later in the program where it sends an integer to the serial port. same result, just later in the program.

One possibility is that you're running out of RAM. As an example the code you commented out to make it work will take up 35 bytes of RAM.

//      Serial.print(" Set Port Memory");
//      Serial.print("=================");

If you're at or close to max (1kB for AtMega168, 2kB for AtMega328) then freeing a few bytes may get it to work again.


the 2 groups of code commented off at the bottom of the menu file. I have to comment those off or it locks up in the setup function where it sends the first integer to the serial port.

in my own defense, I'm still very new to this type of programming. less then a week, but I'm trying to learn the tricks. I'm open to suggestions.

"If you're at or close to max (1kB for AtMega168, 2kB for AtMega328) then freeing a few bytes may get it to work again."

good thought. what exactly uses ram? just variables? I'm not sure what I can change in those commented lines.

Variables and constants will use RAM, strings included, and if running out of RAM you get some nasty errors to deal with.

One possibility is to move all your string constants to FLASH. You can do this using the PSTR macro and create a small function to print to serial directly from flash memory as in the following:

#include <avr/pgmspace.h>

// print a character string from program memory
void print_P(const char *str)
  uint8_t val;
  while (true) {
    if (!val) break;

// code to print string from flash
print_P(PSTR("This is a string stored in flash memory"));
// code to print string with CR/LF from flash
print_P(PSTR("String with CR/LF\r\n"));

One way to find out if this is your issue would be to include the above print_P function in your sketch and change a few of the "Serial.print" statements to use the "print_P" function.

I thought constants were replaced with their value as part of the process of compiling, so once converted to machine code, it would be as if it was the number right in the code, not the constant, like transparent? or do all values, whether " " strings or just a number, do they all use ram just by being there?

how do I get the strings into flash? should it put it there at runtime, or do I have to upload a special program first to save everything I need to flash then upload my program to use it?

I found this thread http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720;start=all
at the bottom of the page they explain how to figure out the memory usage at compile time, but that's a little beyond me.

I'm wondering how Serial.println(); uses ram. I'm looking for the best way to save ram.

"#define is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in arduino don't take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time."

You're right that #define are replaced with their value as part of the compiling, however even a simple command like:


Will take 10 bytes of memory because you're creating a temporary string that's passed to print(). Which is essentially what you're doing with your defines. Hard to say if you're running out of memory without seeing the whole code.


I'm dealing with some PSTR() code with an Ethernet shield and I can't find any documentation on it. I get the general idea, but I don't know how to manipulate it. Are there docs on PSTR() and the equivalent read somewhere?

I found this http://itp.nyu.edu/~gpv206/2008/04/making_the_most_of_arduino_mem.html

they explain how to fix a few memory problems. might be useful to someone