Suspect Code, one array works but two crashes

Hi guys,
im new to this all. Build up my own arduino board alá "build-by-hand" (http://arduino.cc/en/Main/ArduinoBoardSerialSingleSided3) and now im figuring out what its all about.

I was wondering why the this code...

const int range = 361;

int field1[range];
//int field2[range];

int ctr = 0;
int pushButton = 2;
int led = 13;

int dR(int pin){return digitalRead(pin);}
void dW(int pin, int signal){digitalWrite(pin,signal);}
void d(int milis){delay(milis);}

void blinking()
{
  dW(led,HIGH);
  d(20);
  dW(led,LOW);
  d(20);
}

void fill(){
  for(int i = 0; i < range;i++){
    field1[i] = i;
    //field2[i] = i;
  }
}

void setup() {
  fill();  
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  int buttonState = dR(pushButton);
  if(buttonState == 1)
  {    
    Serial.println(field1[ctr]);
    //Serial.println(field2[ctr]);    
    if(ctr >= range)
      ctr=0;
    else
      ctr++;
    while(buttonState){
      blinking();
      buttonState=dR(pushButton);
    }
  }
  delay(1);
}

...works fine,
but if i try to use a bigger range (>361) or two or more arrays, it seems like the function fill crashes.And i wont get anything onto the serial monitor.The ATmega168 has 1KB of SRAM, so it should take some more than 361 integers^^.

Has anyone a clue ?

Thank's !
MosFett

2 * 361 * 2 = 1 444.

allright :slight_smile: 2* 150 *2 works fine,
but whats about 1 array with more than 361 values ?

but whats about 1 array with more than 361 values ?

Whatever the arithmetic supports.

ty for response
the arithmetic should stay stable till 2^15 or ?

the arithmetic should stay stable till 2^15 or ?

The arithmetic will. The results of the code operating will go unstable as soon as you use all the SRAM available on the Arduino.

Ahh ... ok,
theres 16KB flash arround, cant i save my data in this section ?

No, I meant like the arithmetic in reply #1.
Don't forget that you'll need RAM for stack, hidden buffers, stuff like that.

cant i save my data in this section ?

PROGMEM does that, for read-only data. Your data doesn't appear to be read-only. So, no.

Ty, now i get it !

  • Problem solved -