RGB Matrix Panel Interferes with pulseIn() Function

Hi, I have a simple program that measures the width of a pulse every second or so, and displays the value on the Adafruit 32x32 RGB Matrix LED panel. The measurement code and the display code work separately but when they are used in the same program, the measurement values become grossly underestimated and unstable. It occurred to me that both the RGB panel library and the pulseIn() function use timer, and it's likely that the RGB panel lib code is changing or resetting the timer used by the pulseIn() function.

So here's my question. Is there an easy work around so I can either briefly hold the execution of the RGB panel code while using pulseIn(), or make one of the two use a different timer? Since the pulse width I am measuring is less than 10 ms, the effect on the display should be minimum if the display code is stopped momentarily.

Please post the combined code that you have tried

Lejaune:
the pulse width I am measuring is less than 10 ms

Maybe don't use pulseIn()? Just capture the value of micros() when you see the pulse start, and when you see it end, and subtract the figures. The code running the panel may cause some jitter in the measurements, but maybe you could take average or minimum over several readings?

UKHeliBob:
Please post the combined code that you have tried

Here is the simplified code.

echoled.ino (1.12 KB)

Lejaune:
Here is the simplified code.

Simplified, but still too large to post in code tags?

PaulRB:
Simplified, but still too large to post in code tags?

No, not large. I just can't find a way to upload in "code tag". I can copy and paste as plain text but I am not sure how the formatting would do to it.

Look at this thread, which is a sticky at the top of the form page read this before posting a programming question

It explains how to use code tags and has other useful tips such as Auto Formatting code before uploading

Ok here it is. Sorry for the newbie mistakes.

#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library

// Ultrasonic sensor pins
#define TRIG_PIN  34
#define ECHO_PIN  35

// LED panel signals
#define CLK 11  // MUST be on PORTB! (Use pin 11 on Mega)
#define OE  9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

void setup() {
  Serial.begin(112500);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  matrix.begin();
}

void loop() {
  unsigned long echoTime;
  
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  echoTime = pulseIn(ECHO_PIN, HIGH);
  Serial.print("Echo time:  ");
  Serial.println(echoTime);

  matrix.setTextSize(0);     // size 1 == 8 pixels high
  matrix.setTextWrap(true); // Don't wrap at end of line - will do ourselves
  matrix.fillScreen(0);
  matrix.setCursor(1, 1); 
  matrix.print("Echo time:");  
  matrix.print(echoTime/1000.0, 1);
  matrix.println("ms"); 
  delay(500);
}

PaulRB:
Maybe don't use pulseIn()? Just capture the value of micros() when you see the pulse start, and when you see it end, and subtract the figures. The code running the panel may cause some jitter in the measurements, but maybe you could take average or minimum over several readings?

Yes, micros() works much better. Thanks!

I took a look at the source file (wiring_pulse.c) for pulseIn() function, and it seem that time measurement is done by counting the number of loops of execution, which could explain why it's not accurate because of the interrupts generated by the RGB matrix. Furthermore, in the same file, there is a pulseInLong() function that actually uses micros(), and it is accurate. Somehow, pulseInLong() is not listed in the Arduino Reference.

Somehow, pulseInLong() is not listed in the Arduino Reference.

Good find. +1 Karma. However, the fact that it is not shown on the Arduino site may mean it is not officially supported, and could be removed without notice.

Well, the pulseInLong() is now on the Arduino site but not in the reference html docs included in my 1.8.8. Looks like they recently renovated the web site.

Lejaune:
Well, the pulseInLong() is now on the Arduino site but not in the reference html docs included in my 1.8.8. Looks like they recently renovated the web site.

Cool. Switch to using that and make your code more "arduino-style".