Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to store a sequence of RGB values in a multidimensional array:

void loop()
{  
  short colors[][3] = {{2,2,2},{4,4,4},{4,4,4}};
  for(int i=0;i<10;i++)
  {
    Serial.write("Debug 1");
    Serial.write("\n");
    setColor(colors[i][0],colors[i][1],colors[i][2]);
    delay(500);
  }
}

But If my array goes above a certain size, the Arduino UNO does random things and doesn't react anymore (doesn't print Debug 1 anymore, but instead some random characters).

Has anybody experience in doing that stuff?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You've run out of RAM on the AVR microcontroller. Either use less RAM, by either reducing the scope of your project or finding a way to encode your data more densely, or use a AVR microcontroller that has more RAM in the first place such as the ATmega1284P.

share|improve this answer
 
Thank you! Then I'll see what I can do about the RAM. –  Aragon0 7 hours ago
add comment

Your array is statically assigned to 3 elements, but you are reading 10!

Normally, C programs don't check array bounds - there's no such information on array sizes! So, when your accessing items colors[>2][*], you are really reading memory after assigned array, which may be unassigned or assigned for something else, making your code vulnerable to data corruption or cause access violation exceptions. Buffer overflow exploit also explores unsafe code this way (very unlikely in Arduino, I think).

share|improve this answer
 
Sorry, my example code is a bit wrong. I used a larger array, but it doesn't make sense to post a 20 lines long multidimensional array on stackoverflow. The actual array was 10 long. –  Aragon0 7 hours ago
add comment

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.