Counting changes in a PWM signal

Hello, I am working on a program that is receiving a PWM signal from an rc receiver and using it to perform servo movements based on the PWM signal. I have a basic program working to where it will make the servo move if the PWM correct value is detected. What I'm trying to do now make some kind of counter to count the number of times the PWM signal changes between high and low. I have looked around online for material covering something like this but can seem to find anything. Can suggest any methods I could use to count the PWM signal changes between high and low?

Below is my current code

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 // this is the 'maximum' pulse length count (out of 4096)
// our servo # counter
uint8_t servonum = 0;


int ch1; // channel values


void setup() {
pinMode(2, INPUT); 
Serial.begin(9600); 

pwm.begin();
pwm.setPWMFreq(60);  // Analog servo run at ~60 Hz updates
}

void loop() {
  
  ch1 = pulseIn(2, HIGH, 25000); // Read the pulse width of 
 
  
  if(ch1>1300){
        pwm.setPWM(0, 0, 125 );   
      delay(500);
        pwm.setPWM(0, 0, 575 );
      delay(500);}

  if(ch1<1300){Serial.println("Left Switch: Disengaged");}
 
Serial.println(); 

delay(100);
}

This is the section I am interested in counting.

if(ch1>1300){
        pwm.setPWM(0, 0, 125 );   
      delay(500);
        pwm.setPWM(0, 0, 575 );
      delay(500);}

  if(ch1<1300){Serial.println("Left Switch: Disengaged");}

Do you mean the number of times the "ch1" value changes or the number of pulses on the input pin? And what happens if "ch1 = 1300" in your code?

The usual RC signal repeats at 20msec intervals (or is it 50?) so if you identify a period of time you can calculate how many pulses there were.

...R

Danois90:
Do you mean the number of times the "ch1" value changes or the number of pulses on the input pin? And what happens if "ch1 = 1300" in your code?

The PWM signal im using can either be 1000 or 1800 so 1300 was just what I used as the cuttoff. It will never actually be at 1300. I am trying to write it so it keeps a running count of how many times the signal changes between being greater than 1300 and less than 1300.

You need a global variable of type "unsigned int" or "unsigned long" as counter. Then you need a variable of type "int" which holds the previous value of "ch1". Whenever the value of "ch1" is not equal to the previous value, the counter is incremented and the previous value is updated.

I am trying to write it so it keeps a running count of how many times the signal changes between being greater than 1300 and less than 1300.

This is a pretty standard state change scenario. You will need a variable to hold the previous value of ch1 and some new variables to hold your transition counts. Then some code like this. Be aware that pulseIn() is a blocking function, and may not work well with your application.

  previousValueCh1 = ch1;
  ch1 = pulseIn(2, HIGH, 25000); // Read the pulse width of
  if (ch1 >= 1300 and previousValue_ch1 < 1300)
    countGoingHigh++;
  else if ((ch1 < 1300 and previousValueCh1 >= 1300))
    countGoingLow++;

cattledog:
This is a pretty standard state change scenario. You will need a variable to hold the previous value of ch1 and some new variables to hold your transition counts. Then some code like this. Be aware that pulseIn() is a blocking function, and may not work well with your application.

  previousValueCh1 = ch1;

ch1 = pulseIn(2, HIGH, 25000); // Read the pulse width of
  if (ch1 >= 1300 and previousValue_ch1 < 1300)
    countGoingHigh++;
  else if ((ch1 < 1300 and previousValueCh1 >= 1300))
    countGoingLow++;

Thanks a lot! I was able to figure it out with that help.

Robin2:
The usual RC signal repeats at 20msec intervals (or is it 50?)

Yes 20, so 50Hz, which is probably why you have a "50" in your mind too.

servo attached.jpg