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,497
  • 15
  • 24
  • 29
  • What are the types and values of `reps1` and `reps2`? – Mark Smith Dec 09 '16 at 08:08
  • And why are you using `n <= reps2` instead of `n < reps2`? If it's intentional it should be noted in comment why. – KIIV Dec 09 '16 at 13:22
  • Where/what are the definitions of the ats arrays, and specifically, are they large enough that you haven't written beyond the bounds of either? What are reps1 and reps2, and the significance of their product, reps1*reps2, which you've used as the limit of the printing loop counter? – JRobert Dec 09 '16 at 14:21
  • @JRobert I neglected to include that information originally, my apologies. Edited the driver.ino section of the question to include that – Justin Shetty Dec 10 '16 at 01:51
  • @KIIV That was a typo on my part, it is < in the code I ran. – Justin Shetty Dec 10 '16 at 01:52

0 Answers0