Dual Inputs Frequency Counter

Hello,
I'm trying to read double input RISING edge signals at the same time.

This is my Signal. Signal is shifted a little bit.

I just want to count frequency when both signals go HIGH.
I wrote a code like this;

const int pin2 = 2;                 //define input pin to be 2  
const int pin3 = 3;                 //define input pin to be 3
const int t = 30;                   //define count integral in seconds

unsigned long secondCounter = 0;    //define variable
unsigned long counts = 0;           //define variable for pin 2

void pulse (){counts = counts + 1;} //calculation adds 1 to counts on pulse for pin 2
void pulse2 (){secondCounter = secondCounter + 1;} //calculation adds 1 to counts on pulse for pin 3

unsigned long countArray[t];                  //define array size to be t
unsigned long cps = 0;                        //define counts per second

void setup() {
  
  Serial.begin(9600);                                       //start serial connection
  pinMode(pin2, INPUT);                                     //initialize input
  pinMode(pin3, INPUT);                                     //initialize input
  
  if ((digitalRead(2) == RISING) && (digitalRead(3) == RISING)){
    attachInterrupt(digitalPinToInterrupt(2), pulse, RISING);
    attachInterrupt(digitalPinToInterrupt(3), pulse2, RISING);
    
    } 
}


  
void loop() {

      if ( millis() > ((secondCounter + 1) * 1000) ) {      //counts seconds from start
        
              countArray[secondCounter % t] = counts;       //puts last seconds counts into array 
              counts = 0;                                   // resets counts
              Serial.print("secondCounter: ");
              Serial.println(secondCounter);

              unsigned long arraySum = 0;                             //define variable
              int arraySize = min(secondCounter, t);        //only relevant at startup
      
         for (int i = 0; i < arraySize; i++) { 
                        
              arraySum += countArray[i];                    // sum counts in array.
              Serial.print("Count: ");
              Serial.println(countArray[i]);
            
        }              
              cps = arraySum /t;        
             Serial.print("Count per Second: ");
             Serial.println(cps);
        }
                                                           
}

maybe you can give a better idea. Thank you from now.

Has someone changed digitalRead to return values other than HIGH or LOW?

I see three signals, but I don't see a scale.

1 Like

I did. Is that wrong?

In fact, the top signal rises when the bottom two are high. But it looks like this because there is some delay.

why not have interrupts increment a counter. every second report the count of both interrupt. in the code that reports the count, you can use noInterrupts(), just in the code that copies the counts, then re-enable the interrupts and display the counts

1 Like

I want to clarify something. You said

So you have 3 signals lets call them according to the color you have on the scope. Yellow, Blue and Violet. Do you want to count the number of violet rising edges from the time between both yellow and blue are HIGH? Maybe you can give an example by drawing the wave in a piece of paper and your expected results.

Also there should be 3 inputs (yellow, blue and violet) but your program only uses 2 inputs (pin2 and pin 3).

1 Like

Definitely right.


it should be like this.

Thank you for your reply.
The following changes were done to your code:

  1. Value Change instead of rising for the trigger. Since you want something done in both the rising and the falling edges.
  2. Use only one function for both triggers.
  3. If both are high start a timer.
  4. If one of the input signals and no longer high save the duration and signal to print

Try the code:

const int pin2 = 2;                 //define input pin to be 2
const int pin3 = 3;                 //define input pin to be 3
const int t = 30;                   //define count integral in seconds

unsigned long secondCounter = 0;    //define variable
/*GERRY MOD
  unsigned long counts = 0;           //define variable for pin 2

  void pulse () {
  counts = counts + 1; //calculation adds 1 to counts on pulse for pin 2
  }
  void pulse2 () {
  secondCounter = secondCounter + 1; //calculation adds 1 to counts on pulse for pin 3
  }
*/

unsigned long countArray[t];                  //define array size to be t
unsigned long cps = 0;                        //define counts per second

//GERRY MOD
unsigned long startTimer = 0;
bool printValues = false;
void inputValChanged() {
  if (digitalRead(pin2) == HIGH && digitalRead(pin3) == HIGH) {
    if (startTimer == 0)
       startTimer = millis();
  } else if (startTimer != 0) {
    countArray[secondCounter % t] = millis() - startTimer ;
    startTimer = 0;
    secondCounter++;
    printValues = true;
  }
}

void setup() {

  Serial.begin(9600);                                       //start serial connection
  pinMode(pin2, INPUT);                                     //initialize input
  pinMode(pin3, INPUT);                                     //initialize input

  /*GERRY MOD
    if ((digitalRead(2) == RISING) && (digitalRead(3) == RISING)) {
    attachInterrupt(digitalPinToInterrupt(2), pulse, RISING);
    attachInterrupt(digitalPinToInterrupt(3), pulse2, RISING);
    }
  */
  attachInterrupt(digitalPinToInterrupt(2), inputValChanged, CHANGE);
  attachInterrupt(digitalPinToInterrupt(3), inputValChanged, CHANGE);

}



void loop() {

  /*GERRY MOD
    if ( millis() > ((secondCounter + 1) * 1000) ) {      //counts seconds from start

      countArray[secondCounter % t] = counts;       //puts last seconds counts into array
      counts = 0;                                   // resets counts
  */
  if (printValues) {
    printValues = false;

    Serial.print("secondCounter: ");
    Serial.println(secondCounter);

    unsigned long arraySum = 0;                             //define variable
    int arraySize = min(secondCounter, t);        //only relevant at startup

    for (int i = 0; i < arraySize; i++) {

      arraySum += countArray[i];                    // sum counts in array.
      Serial.print("Count: ");
      Serial.println(countArray[i]);

    }
    cps = arraySum / t;
    Serial.print("Count per Second: ");
    Serial.println(cps);
  }

}