Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Now the code I'm writing in Arduino (Using Arduino) uses multiple 2-dimensional arrays. Now when I print some thing using the Serial Monitor it prints it correctly but when I declare and initialize the 2-dimentional array it won't print it.

Code:

void setup()
{
  Serial.begin(9600);
  int image_width = 56;
  int image_height = 96;


  int image_result[image_width][image_height];
  for (int i=0; i<image_height; i++) {
    for (int j=0; j<image_width; j++) {
      image_result[j][i] = 5;
    }
  }

  Serial.print("code works");
}

Now in this case "code works" does not print but when I remove the array declaration and initialization code works is printed. What is the problem?

Do 2 dimensional arrays work differently in Arduino or is it a space issue?

share|improve this question
    
Even a one-dimensional array of 5376 bytes, on a processor with 2048 bytes, will fail. –  Nick Gammon 23 hours ago
    
I meant, 10752 bytes. I overlooked that each array item is 2 bytes. Bear in mind you don't even have all of those 2048 bytes available. The Serial transmit and receive buffers alone use up 128 bytes. –  Nick Gammon 13 hours ago
    
Well if I connect a micro-SD car is there some method to declare array in that memory? –  George Adams 13 hours ago
    
You can connect a SD card and write data to your heart's content. However you can't "declare the array in memory" on it. What is your application here? An Arduino with 2 KB of RAM isn't the best thing for image processing. –  Nick Gammon 13 hours ago
    
I need to read a small BMP file in an array of ints in Arduino and need to do some processing on it. And the file size is 56 by 96 pixels. –  George Adams 13 hours ago

3 Answers 3

up vote 6 down vote accepted

2D arrays work fine on arduino, but you run out of space very quickly.

An uno has 2 kilobytes of ram; your array has 56*96 = 5376 2-byte elements.

I would guess that you are writing over some critical memory value with a 5 at some point causing the program to fail.

share|improve this answer
    
Not only over memory, but probably also over all the I/O registers. –  Edgar Bonet 23 hours ago
    
16 bit addressing, with SRAM starting from 256. So that's 65279 bytes of address space. So you won't reach that. Also, I'm pretty sure there is some kind of protection preventing this from happening. –  Gerben 23 hours ago

You are trying to use more memory than you have available: 96 * 56 * 2 = 10.752KBytes. Not much that the MCU can do when this happens.

If you look into the datasheet, you'll see that your microcontroller (ATmega328p) only has 2KBytes of RAM.

I guess the question then, is to ask yourself if you really need that large an array. You may consider getting an external flash chip or an SDcard shield. If you only need it for read-only purposes (such as a lookup table), you can use some of your flash program memory (you have 32KBytes).

share|improve this answer
    
I'm using a SDcard sheild but I'm reading the data into an array declared like this. How would I be able to use the SD Card memory. –  George Adams 13 hours ago
1  
You don't usually need to deal with all 5376 elements of your array at one instant. Consider dividing up your image into smaller chunks for processing. Depending on the kind of processing that you do, you may require different smarts to optimize the process. ;-) –  user3663369 13 hours ago
    
Can one write data on the flash memory again and again. What do you mean by read-only? How does one write the data once on it. For example I want to write the image data on the flash memory one. How will I be able to do it? –  George Adams 13 hours ago
    
I am referring to your program flash memory. AFAIK, you can only write to the program flash during programming. One you boot your MCU, the program flash is used as read-only memory (so that we don't mess up the program when it's running.) Since you want to write your data, the program flash is not suitable. –  user3663369 10 hours ago
    
Do you really need to process the whole image in one go? As I've mentioned before, you can divide your image to smaller blocks, say chunk[8][8] would be a comfortable size for Arduino to swallow, and it works well even if you use convolution filters. (Having said that, I must stress that the Arduino Uno is ill-suited for any serious image processing application.) –  user3663369 10 hours ago

This is what I fed my Uno:

#define IMAGEWIDTH 56
#define IMAGEHEIGHT 96

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

  int i, j;
  int image_result[IMAGEWIDTH][IMAGEHEIGHT];

  for (i = 0; i < IMAGEWIDTH; i++) {
    Serial.print("\nIMAGEWIDTH ");
    Serial.print(i + 1);
    Serial.println(":");
    for (j = 0; j < IMAGEHEIGHT; j++) {
      image_result[i][j] = i + j;
      Serial.print("image_result[");
      Serial.print(i + 1);
      Serial.print("][");
      Serial.print(j + 1);
      Serial.print("]: ");
      Serial.println(image_result[i][j]);
    }
  }
  Serial.println(F("\nDone!"));
}

void loop() {
}

This will run over the whole array, while printing progress over Serial, which then print "Done!" once it is. I suggest changing:

#define IMAGEWIDTH 56
#define IMAGEHEIGHT 96

To lesser values, unless you have a few good minutes at hand.

share|improve this answer
1  
This is not at all going to solve his problem. It isn't time, it is the size of the array. 56 * 96 * 2 (an int is two bytes) uses 10752 bytes. Your Uno has 2048 bytes. The issue is not time, it is storage. –  Nick Gammon 13 hours ago
    
Nick was right, this hasn't solved my problem. Anyway, thanks for trying. –  George Adams 10 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.