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

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);

void data(int reg, int data) {

void loop() {
  for(int j=0;j<sample1||i<sample2||i>=sample2);j++) {
    float s=A*sin((2*3.14*freq1*j)/Fs);
    if(i<=sample2) {
      float s=A*sin((2*3.14*freq2*i)/Fs1);
    } else if(i>sample2) {
      float s=A*sin((2*3.14*freq2*i)/Fs1);
  • please format your code properly (indent at least 4 spaces)
    – jsotola
    Commented Jan 2, 2018 at 7:01
  • how do you know that the code stops?
    – jsotola
    Commented Jan 2, 2018 at 7:01
  • 4
    I find it hard to believe you are not getting a compiler error because this line for(int j=0;j<sample1||i<sample2||i>=sample2);j++) has a bracket too many.
    – Nick Gammon
    Commented Jan 2, 2018 at 7:31
  • Also I really don't like the conditions used in that if... It looks like it's likely to end up with j being more than the size of your sine[] array since you have no absolute bounds checking...
    – Majenko
    Commented Jan 2, 2018 at 13:56
  • Your huge sine[] array serves no useful purpose. Get rid of it and just data(reg2, s + 190);. Commented Jan 3, 2018 at 22:46

1 Answer 1

float sine[5000]

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 < sample2 || i >= sample2

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

  • Hi, Now i have chanced the data type to int and reduced the value to 1000, even now also same thing is happened Commented Jan 2, 2018 at 10:19
  • 1000 x 5 = 5000 which is still more than 2048.
    – Mark Smith
    Commented Jan 2, 2018 at 11:21
  • On arduino.cc forum OP has the same question, only there he mentions Arduino Due forum.arduino.cc/index.php?topic=520194
    – Juraj
    Commented Jan 2, 2018 at 11:52
  • See amended answer. Your for loop will never end.
    – Nick Gammon
    Commented Jan 2, 2018 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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