2

Currently working on a data collection algorithm on the Arduino MEGA (ATMega1280) and I've run into a problem with some of the code I've written. I feel my problems may be stemming from the way in which I'm storing the sample points I've generated in arrays ats1 and ats2 or in the ways that I'm generating said sample points in generate_tspairs. Any insight would be appreciated.

I've defined a struct in an external file structs.h

struct tspair{
  int t;
  int s;
};

I have a function defined in generate_tspairs.h

    void generate_tspairs(tspair ats1[], tspair ats2[], int N, int reps1, int reps2, int reps3){

        int alpha = log(N)/log(2); //equivalent to log2(N) 

        for(int j =  0 ; j < reps1 ; j++){
            double randomDecimal = (double) random(1000)/1000;
            int r = (int) pow(2,alpha-1)*randomDecimal + 1;
            int s = 2*r - 1; //s is a random odd integer on the interval [1,N-1]

                for(int n = 0 ; n < reps2 ; n++){
                    double randomDecimal = (double) random(1000)/1000;
                    int t =  (int) N*randomDecimal;
                    tspair temp = {t,s};
                    ats1[(j*reps2)+n] = temp;
                }

                for(int n = 0 ; n < reps3 ; n++){
                    double randomDecimal = (double) random(1000)/1000;
                    int r = (int) pow(2,alpha-1)*randomDecimal + 1;
                    int s = 2*r - 1; //s is a random odd integer on the interval [1,N-1]

                    randomDecimal = (double) random(1000)/1000;
                    int t =  (int) N*randomDecimal;
                    tspair temp = {0,s};
                    ats2[(j*reps3)+n] = temp;
                }
        }
    return;
    }

After initializing and setting my two arrays of tspair structs, I am printing the values of the first array ats1 using the following in driver.ino

  int reps1 = 3;
  int reps2 = 5;
  int reps3 = 5;

  tspair ats1[reps1*reps2];
  tspair ats2[reps1*reps3];

  generate_tspairs(ats1, ats2, N, reps1, reps2, reps3);

  for(int i = 0 ; i < reps1*reps2 ; i++){
    Serial.print("t: ");
    Serial.print(ats1[i].t);
    Serial.print(" s: ");
    Serial.println(ats1[i].s);
  }

However, oftentimes I will receive the exact same values across multiple runs of generate_tspairs or when printing the values it will not stop at the end of my ats1 but rather it will continue to print the same line ad infinitum. E.g.

t: 815 s: 523
t: 975 s: 523
t: 244 s: 523
t: 8689 s: -26051
t: 31001 s: 29184
t: -25856 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
t: -2057 s: 6939
...
...
...
t: -2057 s: 6939
VE7JRO
  • 2,515
  • 19
  • 27
  • 29

0 Answers0