Why does void loop() stop after some samples? I am not getting any compile time error. After some samples of data void loop() stops executing. Please let know what the problem is. Below is my code.

Thanks and regards
Thippeswamy

#include&lt;SPI.h&gt;
float amplitude=1.5;
int freq1=20; int freq2=12;
int k=20;float A= amplitude*k; 
int Fs=10550; float sine[5000]; float Fs1=3000;
int i=0;int reg1=1; int reg2=0;
int sample1=Fs/freq1; float sample2=Fs1/freq2;

void setup() {
  int SS=10; 
  pinMode(SS, OUTPUT);
  SPI.begin();  
}

void data(int reg, int data) {
  digitalWrite(SS,LOW);
  SPI.transfer(reg);
  SPI.transfer(data);
  digitalWrite(SS,HIGH);
}

void loop() {
  for(int j=0;j&lt;sample1||i&lt;sample2||i&gt;=sample2);j++) {
    float s=A*sin((2*3.14*freq1*j)/Fs);
    sine[j]=s+190;
    data(reg1,sine[j]);
    if(i&lt;=sample2) {
      float s=A*sin((2*3.14*freq2*i)/Fs1);
      sine[i]=s+190;
      data(reg2,sine[i]);
      i++;
    } else if(i&gt;sample2) {
      float s=A*sin((2*3.14*freq2*i)/Fs1);
      sine[i]=s+190;
      data(reg2,sine[i]);
      i++;
    }
  }
}
float sine[5000]
...
sine[j]=s+190;    



A float takes 4 bytes. You are allocating 5000 of them which is 20000 bytes. The Uno has 2048 bytes of RAM. Thus you are running out of RAM and overwriting something you shouldn't be.



As pointed out on the Arduino forum, your for loop which tests for:

i &lt; sample2 || i &gt;= sample2


will always be true. Of course i will be either less than or greater than or equal to sample2.