Elapsed time between two pulses on two interrupt pins (Microsecond)

Hello,

the goal is to check how good an (arduino controlled) electronic ignition works, I want to know how much advance it has.

The Engine runs between 600Rpm (10Hz) and 6000 Rpm (100Hz), the "ignition advance" is between 8° and 32° so I need a percision under 1°

At 600Rpm 1° means 278µs, at 6000 28µs

I tryed one night long how to count the short time between state rising on pin 2, and state rising on pint 3 using Timer1 on Arduino Nano

I want to avoid overflow, the worth condition is at low Rpm (8° means 2224µs), let us take 4ms.

Whould somebody be so nice and write the code for me ? (sounds a bit like a lazy student who needs to do a job for next day, sorry)

Thanks a lot

Philippe

2cv4ever:
Whould somebody be so nice and write the code for me ? (sounds a bit like a lazy student who needs to do a job for next day, sorry)

If you want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

If you want to learn to do it yourself we will be happy to help. Post your best attempt and tell us what it actually does and what you want it to do that is different.

...R

Hello Robin2,

I have tried a lot, now I have this code and I still have to test it in real conditions

const byte interruptPinTemp = 2;
const byte Pininterrupt = 3;
volatile int cnt1 = 0;

int timer1_counter;
boolean bData = false;

void setup() {

  Serial.begin(115200);      
  noInterrupts();           // disable all interrupts
 
  pinMode(interruptPinTemp, INPUT_PULLUP);
  pinMode(Pininterrupt, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPinTemp), printserial, RISING );
  attachInterrupt(digitalPinToInterrupt(Pininterrupt), printserial2, RISING );
 
  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 0;   // preload timer 65536-16MHz/256/2Hz


  TCNT1 = timer1_counter;   // preload timer
 //TCCR1B |= (1 << CS12);    // 256 prescaler
 //TCCR1B |= (1 << CS11);
 TCCR1B |= (1 << CS10);
 //TCCR1B |= (1 << WGM12);
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts
 
}


void printserial()
{
   //cnt1 = TCNT1;
   //Serial.println(cnt1);
   
   TCNT1 = 0;
}

void printserial2()
{
   cnt1 = TCNT1;
   Serial.println(cnt1);
   
   TCNT1 = 0;
}

void loop() {
 
}

On an other Nano I let run this code to let rise 2 pins with a small interval, I obtain 12080 on serial port why 12080, they should be16 000 counts in 1/1000s isnt’it ?

int outPin = 8; 
int outPin2 = 9; 

void setup()
{
  pinMode(outPin, OUTPUT);   
pinMode(outPin2, OUTPUT);}

void loop()
{
  digitalWrite(outPin, HIGH);   
  delay(1);      
  digitalWrite(outPin2, HIGH);           
   digitalWrite(outPin, LOW);   // sets the pin on      // pauses for 50 microseconds
  digitalWrite(outPin2, LOW);
}

2cv4ever:
On an other Nano I let run this code to let rise 2 pins with a small interval, I obtain 12080 on serial port why 12080, they should be16 000 counts in 1/1000s isnt'it ?

I won't have time to look at this today. I will bookmark it and try to look at it tomorrow. Someone else may come to the rescue in the meantime.

...R

void printserial2()
{
   cnt1 = TCNT1;
   Serial.println(cnt1);
   
   TCNT1 = 0;
}

You should not be printing in an ISR
Interrupts are disabled when in an ISR
Serial uses interrupts

Hello,

I have removed the serial print from the ISR and now it works fine.

Thx to Robin2 and UKHeliBob

Philippe