Micros() show the same two consecutive

Here is my code. I want to ask why I use Arduino Due to record the time and analog value, it will print two same time point value. Kindly to check in the figure, this is what I get from this code.
Why does it show the same value?

float z[2900][3];
int s;
int p;
int inPin = 5;


void setup() {
 pinMode(9,OUTPUT);
 digitalWrite(9,HIGH);
 pinMode(10,OUTPUT);
 digitalWrite(10,HIGH);
 Serial.begin(115200);
 analogReadResolution(12);
 pinMode(inPin,INPUT);
}



void loop() {

if (Serial.available() > 0) {
   int inByte1 = Serial.read();
   switch (inByte1) {
   case '2':
   digitalWrite(9,LOW);
   delay(1500);
   digitalWrite(9,HIGH);
   delay(300);
   digitalWrite(10,LOW);
   delay(300);
   digitalWrite(10,HIGH);
   
   //delay(76000); //等待extraction step
   
   p=0;
       while(p < 1){
       if(digitalRead(inPin) == 1){
        //delayMicroseconds(7420);
       for (int s=0;s<2620;s++){
        z[s][0] = micros();
        z[s][1] = analogRead(A1);
        }  
        p = p+1;
          }
       }

       /*while (p < 75){
       if(digitalRead(inPin) == 1){ 
       //x[p][3] = millis();
       for (int s=0;s<2620;s++){
        z[s][0] = micros();
        z[s][2] = analogRead(A2);
        }  
        p = p+1;
       for (int s=0;s<2620;s++){
       z[s][1] = z[s][2] + z[s][1]; 
       }
       
       }
       }

       for (int s=0;s<2620;s++){
       z[s][1] = z[s][1]/p;
       }*/
       
       
       
        for(int s=0;s<2620;s++){
          Serial.print(z[s][0]);
          Serial.print(","); 
          Serial.print(z[s][0]-z[0][0]);
          Serial.print(","); 
          Serial.print(z[s][1]);
          Serial.print("\n");
          delay(3);
        }
          

      break;
}
}
}

micros returns unsigned long, not float

So you mean that I need to convert unsigned long to float?

No, I mean you need to record (store) the value returned by micros() in an unsigned long (uint32_t) variable.

long

You mean like this? store the value in unsigned long.

No, not really.
Either use an array of struct (timestamp (uint32_t) and sample (uint16_t)), or two arrays one of uint32_t for the timestamps, and one of uint_16_t for the samples.

Edit: I've installed a decent calculator app on this phone now. The timestamp you highlighted requires just over 26 bits to accurately represent, but the float data type has only a 23 bit mantissa.

Now I try and it seems work. Thank you very much because I try to solve this problem for more than one day and did not find this problem on the forum. Thank you!

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