Programs larger than ~16K run abnormally on my Uno. Is it fake?

I got an Arduino Uno on ebay. Its has an ATMEGA328P-PU chip in it (that's what's printed on it anyway).

When I compile my program with a small array everything runs smoothly and the hex file is ~8K big. When I add more data do my array, things are getting strange. Up to the point where the compiled hex file is ~16-17K big, the program runs smoothly. When I add more data making the compiled file even bigger ~22K, the program doesn't really run... With the smaller compiled file, the serial monitor prints all my debug Serial.print() calls. With the bigger files, it prints nothing or prints partial info (depending on the size of the compiled file).

Is it possible that the processor is from the 168 family with only 16K of memory? If not, what else can it be (I doubled and triple checked my code, making it super simple - just init an array with values and printing those values to the serial monitor).

Your array is almost certainly being stored in SRAM which means that you only have a maximum of 2kB. You will have to move the array(s) to PROGMEM. If that isn't the problem we'd have to see your code.

Pete

szangvil: With the bigger files, it prints nothing or prints partial info (depending on the size of the compiled file).

To me that suggests that you are running out of RAM. Your mention of using arrays and printing makes me suspect you could be using a lot of RAM and may not appreciate how little you have to work with. There is only 2K of RAM for everything - initialised data, stack, heap.

I think the other guys are correct. I just went through almost the exact same problem last week, although in my case, I was implementing a multilevel menuing system. See the following thread, especially posts #10, 11, which describes how I muddled through to where I had some useful stats.

http://arduino.cc/forum/index.php/topic,91208.0.html

Well, my code is simple enough:

#define NUMBER_OF_CAMERAS 76

typedef struct Cameras {
    float lat;
    float lon;
    int speed;
    int dir;
    float Dist;
    float PrevDist;
} CAMERAS;

CAMERAS Cam[NUMBER_OF_CAMERAS];


void InitCameras () {
    Cam[0].lat = 32.58209; Cam[0].lon = 34.92381; Cam[0].speed = 90; Cam[0].dir = 210;
    Cam[1].lat = 32.54429; Cam[1].lon = 34.91654; Cam[1].speed = 100; Cam[1].dir = 10;
    Cam[2].lat = 32.52507; Cam[2].lon = 34.91464; Cam[2].speed = 100; Cam[2].dir = 193;
...
...
...

    Cam[74].lat = 32.659216; Cam[74].lon = 34.103458; Cam[74].speed = 80; Cam[74].dir = 218;
    Cam[75].lat = 32.659216; Cam[75].lon = 34.103458; Cam[75].speed = 80; Cam[75].dir = 33;
    
}

void setup() {
    Serial.begin(115200);
    InitCameras();
}

void loop () {
...
...
Serial.println(Cam[i]...)
...
...
}

el_supremo: I never heard of PROGMEM until now… I will look into it.

1520 bytes of RAM table. Anything else?

(BTW, you know you can initialise the table when you declare it, don't you? Doesn't save any RAM, but it saves a helluva lot of typing.)

I know I can init it when declaring... I like not to. I don't type it :) I use Excel to generate it from a list I have.

So you are basically saying that just this array take %75 of my RAM?

I know I can init it when declaring… I like not to

Better get used to it, if you want to use PROGMEM.

So you are basically saying that just this array take %75 of my RAM?

Or more. And that is just for that array. Lots of other stuff uses SRAM, too.

Got it. Now I am using:

typedef float PROGMEM  prog_float_t ;
prog_float_t PROGMEM CamLat[] = {32.58209,32.54429,32.52507...};
prog_float_t PROGMEM CamLon[] = {34.92381,34.91654,34.91464...};
prog_float_t PROGMEM CamVel[] = {90,100,100,90,100,90,90,90...};
prog_float_t PROGMEM CamDir[] = {210,10,193,18,193,184,351,8...};

and accessing the data using:

            cLat = pgm_read_float(&CamLat[i]);
            cLon = pgm_read_float(&CamLon[i]);

Seems to work.

Also I can see the AVR Memory usage dropped (using avr-size). First output is before using progmem:

C:\Temp Data\cam>avr-size -C --mcu=atmega328p SpeedCam.cpp.elf
AVR Memory Usage
----------------
Device: atmega328p

Program:   16524 bytes (50.4% Full)
(.text + .data + .bootloader)

Data:       1878 bytes (91.7% Full)
(.data + .bss + .noinit)



C:\Temp Data\cam>avr-size -C --mcu=atmega328p SpeedCam.cpp.elf
AVR Memory Usage
----------------
Device: atmega328p

Program:   11530 bytes (35.2% Full)
(.text + .data + .bootloader)

Data:        345 bytes (16.8% Full)
(.data + .bss + .noinit)

How can I use PROGMEM to define an array of a struc:

typedef struct Cameras {
    float lat;
    float lon;
    float vel; 
    float dir;
} CAMERAS;

CAMERAS Cam[NUMBER_OF_CAMERAS];

Right now I am ussing 4 arrays:

typedef float PROGMEM  prog_float_t ;
prog_float_t PROGMEM CamLat[] = {32.58209,32.54429,...};
prog_float_t PROGMEM CamLon[] = {34.92381,34.91654,...};
prog_float_t PROGMEM CamVel[] = {90,100,100,90,100,...};
prog_float_t PROGMEM CamDir[] = {210,10,193,18,193,...};

But I’d like to use an array of struct…

pretty much like you had in reply #4, except you can't initialise it in "setup()"

prog_float_t PROGMEM CamLat[] = {32.58209,32.54429,...};

Check the following page, as I'm not sure your PROGMEM is in an acceptable position there. I believe it compiles ok, but crashes at runtime [I just did that last week with my menuing system].

http://www.arduino.cc/en/Reference/PROGMEM

Note that, in the version of PROGMEM that comes with IDE 0022, they indicated your positioning was allowable, but have since updated the info, and also removed PROGMEM.html from the IDE 1.0 Reference directory.