PWM Input Issue

Hi,
I'm using an Arduino Uno to control an LED via two PWM inputs. However, it will only read up one PWM input at a time. If I plug in one signal on input 5 it reads fine, if I plug in one signal on input 7 it reads fine. If I plug in both signals together on inputs 5 and 7 it will only read one of the two.

I've confirmed my signal source is outputting the correct signal, and i've tried two different boards with the same result.

Any thoughts? Not really sure if its a hardware or software issue at this point.

int ch1;
int ch2;
int ch3;

void setup() {

  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);

  Serial.begin(9600);

}

void loop() {

  ch1 = pulseIn(5, HIGH, 25000); // Read the pulse width of 
  ch2 = pulseIn(6, HIGH, 25000); // each channel
  ch3 = pulseIn(7, HIGH, 25000);

  Serial.print("Channel 1:"); // Print the value of 
  Serial.println(ch1);        // each channel

  Serial.print("Channel 2:");
  Serial.println(ch2);

  Serial.print("Channel 3:");
  Serial.println(ch3);

  delay(2000); // I put this here just to make the terminal 
              // window happier
}

You understand that the calls are sequential, right?

pulseIn(5, HIGH, 25000) will wait for pin 5 to go HIGH, starts timing, then waits for the pin to go LOW and stops timing and if the pin is already high when the function is called, it will wait for the pin to go LOW and then HIGH before it starts counting or return 0 if 25000 microseconds have expired

Only then it will go do the same for pin 6 and next for pin 7

In general it is better to have your return values from the pulseIn() call to be of type unsigned long rather than int depending the length of your pulse. Your int will overflow after 32767 microseconds (in your case this is not a problem because your function will timeout in 25000 microseconds, so you won't see the issue)

--> what does your signal looks like and what do you expect? why do you think it does not work ?

So the result of this in the serial monitor is:

"Channel 1: 500
Channel 2: 0
Channel 3: 0"

When it should be:

"Channel 1: 500
Channel 2: 0
Channel 3: 500"

(I only have inputs on Pin 5 and 7 so channel 2 can be disregarded"

Thanks!
Kyle

and if you start by reading channel 3 instead of channel 1 - what do you see?

(you should give names to your pins like const byte channel1Pin = 5; and use channel1Pin instead of 5 everywhere for readability)

Ah ha! I got it

Thank you!

double channel[2];  //Channel input variable

void setup(){
  pinMode(4, INPUT);
  pinMode(5, INPUT);

  Serial.begin(9600);
}

  void loop(){
  channel[0] = pulseIn(4, HIGH); // Read the pulse width of channel 9
  channel[1] = pulseIn(5, HIGH); // Read the pulse width of channel 10
  
  Serial.print("Channel 9: "); // Print the value of channel 9
  Serial.println(channel[0]);
  Serial.print("Channel 10: "); // Print the value of channel 10
  Serial.println(channel[1]);

were you reading the wrong pins?

pulseIn does not return a double but an unsigned long. why do you use a double?

Your code would be better like

const byte ch9Pin = 4;
const byte ch10Pin = 5;

unsigned long channel[2];  //Channel input variable

void setup(){
  pinMode(ch9Pin, INPUT);
  pinMode(ch10Pin, INPUT);

  Serial.begin(115200); // why use 9600? any reason to go slower than easily achieved?
}

void loop(){
  channel[0] = pulseIn(ch9Pin, HIGH); // Read the pulse width of channel 9
  channel[1] = pulseIn(ch10Pin, HIGH); // Read the pulse width of channel 10
  
  Serial.print("Channel 9: "); // Print the value of of channel 9
  Serial.println(channel[0]);
  Serial.print("Channel 10: "); // Print the value of of channel 10
  Serial.println(channel[1]);
 }

If you use an array for the results you might want to use an array for the pins number

I'll give that a shot! Thanks