High Speed counting using interrupts

Status
Not open for further replies.

Gadget999

Well-known member
I want to count 4 speed channels of rpm

each sensor has 360 pulses

if anyone is interested the code below will update 100 times a second and seems pretty accurate

So far i have only been able to count on one channel - i will program another teensy to pulse 4 channels

the code works up to approx 270 khz :) plenty fast enough for me

is this a good strategy for counting 4 speed channels ?

Code:
uint8_t pulse_pin1 = 11;
uint8_t pulse_pin2 = 12;

unsigned long NoOfPulses1=360;
unsigned long NoOfPulses2=360;
unsigned long pulseCount1=0;
unsigned long pulseCount2=0;
unsigned long Period1=0;
unsigned long Period2=0;
unsigned long Freq1=0;
unsigned long Freq2=0;


unsigned long RPM1=0.000;
unsigned long RPM2=0.000;
unsigned int DataStamp = 0;
unsigned int NowStamp = 0;


void setup() {
    pinMode(pulse_pin1, INPUT_PULLUP);
    pinMode(pulse_pin2, INPUT_PULLUP);
    Serial.begin(115200);
    delay(2000);
    DataStamp = micros();
     
    attachInterrupt(digitalPinToInterrupt(pulse_pin1), pulseCounting1, RISING);
    attachInterrupt(digitalPinToInterrupt(pulse_pin2), pulseCounting2, RISING); 

}

void pulseCounting1()
{
  pulseCount1++; 
}

void pulseCounting2()
{
  pulseCount2++;
}

void loop() {

 if (millis() > (DataStamp/1000 + 99)) {   // 100 samples per second

     NowStamp = micros();
  
     if (pulseCount1 > 0)  {
      Period1 = ( (NowStamp - DataStamp) ) ;
      Freq1 = ((pulseCount1 * Period1)/10000) ;
      RPM1 = (Freq1 * 60) / NoOfPulses1 ;
      Serial.print("Freq 1: ");Serial.println(Freq1);
      Serial.print("RPM 1: ");Serial.println(RPM1);
      pulseCount1=0;
     }

     if (pulseCount2 > 0)  {
      Period2 = ( (NowStamp - DataStamp) ) ;
      Freq2 = ((pulseCount2 * Period2)/10000) ;
      RPM2 = (Freq2 * 60) / NoOfPulses2 ;
      Serial.print("Freq 2: ");Serial.println(Freq2);
      Serial.print("RPM 2: ");Serial.println(RPM2);
      pulseCount2=0;
     }
     
     DataStamp = micros(); 
     
   }

  }
 
Interestingly - I tried running the code on a arduino nano and a Teensy 2.0

the code did not run correctly and it gave a pulse count of one each time

am i right to assume the processor is too slow to read and compute the time period using micros() ?
 
this code worked on the Teensy 2.0

not as accurate because the time interval uses millis() instead of micros()

Code:
//uint8_t pulse_pin1 = 11;
//uint8_t pulse_pin2 = 12;
uint8_t pulse_pin1 = 6;
uint8_t pulse_pin2 = 7;

unsigned long NoOfPulses1=360;
unsigned long NoOfPulses2=360;
unsigned long pulseCount1=0;
unsigned long pulseCount2=0;
unsigned long Period1=0;
unsigned long Period2=0;
unsigned long Freq1=0;
unsigned long Freq2=0;


unsigned long RPM1=0.000;
unsigned long RPM2=0.000;
unsigned int DataStamp = 0;
unsigned int NowStamp = 0;


void setup() {
    pinMode(pulse_pin1, INPUT_PULLUP);
    pinMode(pulse_pin2, INPUT_PULLUP);
    Serial.begin(9600);
    delay(2000);
    DataStamp = millis();
     
    //analogWriteResolution(16);
    //analogWriteFrequency(pulse_pin, 700000.0L); 
    //analogWrite(pulse_pin, 32000);
    attachInterrupt(digitalPinToInterrupt(pulse_pin1), pulseCounting1, RISING);
    attachInterrupt(digitalPinToInterrupt(pulse_pin2), pulseCounting2, RISING); 

}

void pulseCounting1()
{
  pulseCount1++;
   digitalWrite(13, HIGH); 
}

void pulseCounting2()
{
  pulseCount2++;
  //Serial.print("Count 2: ");Serial.println(pulseCount2);
}

void loop() {

 if (millis() > (DataStamp + 9)) {

     NowStamp = millis();
     
     if (pulseCount1 > 0)  {
      Period1 = ( (NowStamp - DataStamp) ) ;
      Freq1 = ((pulseCount1 * Period1)* 10) ;
      RPM1 = (Freq1 * 60) / NoOfPulses1 ;
      Serial.print("Pulses 1: ");Serial.println(pulseCount1);
      Serial.print("Period 1: ");Serial.println(Period1);
      Serial.print("Freq 1 : ");Serial.println(Freq1);
      Serial.print("RPM 1 : ");Serial.println(RPM1);
      
      pulseCount1=0;
     }

     if (pulseCount2 > 0) {
      Period2 = ( (NowStamp - DataStamp) ) ;
      Freq2 = ((pulseCount2 * Period2)* 10) ;
      RPM2 = (Freq1 * 60) / NoOfPulses2 ;
      Serial.print("Pulses 2: ");Serial.println(pulseCount2);
      Serial.print("Period 2: ");Serial.println(Period2);
      Serial.print("Freq 2 : ");Serial.println(Freq2);
      Serial.print("RPM 2 : ");Serial.println(RPM2);
      
      pulseCount2=0;
     }
     
     DataStamp = millis(); 
     
   }

  
  
  }
 
Status
Not open for further replies.
Back
Top