Timer0, 1 and 2 reading the exact micros as it times?

UNO and or LEONARDO for usecase.
I want to do something like this below but with higher resulution. Best possible, by adressing timers the hardcore way..that i dont master due to incompetence. :slight_smile:
Code below gives me troubles and inconsquent values, but is more lika a POC that shows the case
What i want is to timestamp multiple mics clockcycle as hit by sound, translate to micros and write it to serial. THATS IT!!!!
Purpouse is Trilatheration.

But seems for me like serial, micros, millis Sei(), cli(), etc analog read and other stuff crashes when bad handled, so i leave it all up to u pros here. Im a carpenter, not a programmer.

I suppose u grasp the idea by reading /testing below POC-code for your self.

Possible task yes no?
Yes-> code please:)
No?->Keep trying.

POC timestamp high res UNO/LEONARDO.

CODE STARTS HERE:

int mic1 = 2; //Right //Three mics on a distance of say 0.25 meters apart or any...
int mic2 = 3; //Centr
int mic3 = 7; //Left

volatile unsigned long tid1 = 0; //Use a var to take timen on trig 1, 2 and 3
volatile unsigned long tid2 = 0; //
volatile unsigned long tid3 = 0; //

volatile int stop1 = 0; //Use as var to prevent Void(Trigs) to run a second time....
volatile int stop2 = 0;
volatile int stop3 = 0;
volatile int counting = 0; //Use a var in loop and triggs as flag to prevent triggs while in loop.

//SETUP_______________________

void setup() {

pinMode(mic1, INPUT);
pinMode(mic2, INPUT);
pinMode(mic3, INPUT);
pinMode(11, OUTPUT);//Buzzer pƄ pin 6?

Serial.begin(9600);

attachInterrupt(digitalPinToInterrupt(mic1), trig1, RISING ); //? Falling? to get the first wave..
attachInterrupt(digitalPinToInterrupt(mic2), trig2, RISING);
attachInterrupt(digitalPinToInterrupt(mic3), trig3, RISING);

}

//LOOP________________________________
void loop() {

if (( stop1 == 1 && stop2 == 1 && stop3 == 1 && counting == 1 )) { // If trigged else do nothing.
counting = 2;

Serial.print("A");
 Serial.write((byte)0x00);
  Serial.print(tid1);
   Serial.write((byte)0x00);

Serial.print("B");
 Serial.write((byte)0x00);
  Serial.print(tid2);
   Serial.write((byte)0x00);

Serial.print("C");
 Serial.write((byte)0x00);
  Serial.print(tid3);
   Serial.write((byte)0x00);

Serial.println();

//reset trigs.
stop1 = 0;
stop2 = 0;
stop3 = 0;
counting = 0;

}//End if loop..

}//End loop...

void trig1() {

if (stop1 == 0 && counting == 0);
tid1 = micros();
stop1 = 1;
counting = 1;

}

void trig2() {

if (stop2 == 0 && counting == 0);
tid2 = micros();
stop2 = 1;
counting = 1;

}

void trig3() {

if (stop3 == 0 && counting == 0);
tid3 = micros();
stop3 = 1;
counting = 1;

}

Please read the first topic topic telling how to get the best from this Forum and clean up Your posting.
Code tags for code, formatted in the IDE, what the intention is and what is actually taking place.

Then don't use interrupts, that's ugly stuff.

Then ask your questions in Gigs and Collaborations.

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