Error occurs when running the sketch in DUE, but no error occurs in MKR1000

Dear all,

I tried to comply the following code.

#include "time.h"
#include "iomanip"  
#include "math.h"


#define PI 3.14159
int x=0;
int runTime=1;

void setup() {
  // put your setup code here, to run once:

}

void loop() {

  delay(3000);
  // put your main code here, to run repeatedly:
int i, j;
  double A, B, C;
  double r[10000];
  double E=0;
  double D=1;
  double uni[2];
  double *p;
  srand((unsigned)time(NULL)); 

  if(runTime){
    runTime--;
  for (j = 0; j<10000; j++)
  {
    UNIFORM(&uni[0]);  
    A = sqrt((-2)*log(uni[0]));
    B = 2 * PI*uni[1];
    C = A*cos(B);
    r[j] = E + C*D;    
   // outfile << r << "   ";  
   Serial.println(r[j]);
  // Serial.print(" ");
  // Serial.println(x);
  }
  }
}


void UNIFORM(double *p)
{
  int i, a;
  double f;
  for (i = 0; i<2; i++, x = x + 689)
  {
    a = rand() + x; 
    a = a%1000;
    f = (double)a;
    f = f / 1000.0;
    *p = f;
    p++;
  }
}

There are no errors when it is complied using MKR1000, but there are the following errors when it is complied using Due.

c:/users/joe/appdata/local/arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/lib/armv7-m\libc.a(lib_a-gettimeofdayr.o): In function _gettimeofday_r': gettimeofdayr.c:(.text._gettimeofday_r+0xe): undefined reference to _gettimeofday'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Due (Programming Port).

May I know how to fix this problem? Thanks a lot.

Joe

IMO the issue is with time.h.

This line: srand((unsigned)time(NULL)); is used to initialize the random number generator.

With a DUE (Sam3x8e is the main uc), there is a TRNG peripheral to generate 32-bit random numbers.
Search in the DUE sub forum for an example sketch to use TRNG.

Moreover, you don't initialize Serial in setupt().

This skecth compiles without any error:

int x = 0;
int runTime = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(250000);
}

void loop() {

  delay(3000);
  // put your main code here, to run repeatedly:
  int i, j;
  double A, B, C;
  double r[10000];
  double E = 0;
  double D = 1;
  double uni[2];
  double *p;
  //srand((unsigned)time(NULL));

  if (runTime) {
    runTime--;
    for (j = 0; j < 10000; j++)
    {
      UNIFORM(&uni[0]);
      A = sqrt((-2) * log(uni[0]));
      B = 2 * PI * uni[1];
      C = A * cos(B);
      r[j] = E + C * D;
      // outfile << r << "   ";
      Serial.println(r[j]);
      // Serial.print(" ");
      // Serial.println(x);
    }
  }
}


void UNIFORM(double *p)
{
  int i, a;
  double f;
  for (i = 0; i < 2; i++, x = x + 689)
  {
    a = rand() + x;
    a = a % 1000;
    f = (double)a;
    f = f / 1000.0;
    *p = f;
    p++;
  }
}

Thanks, ard_newbie.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.