#define IR_PIN 4
#define PULSE_COUNT 34
#define EDGE_COUNT PULSE_COUNT * 2
int count = 0;
unsigned long edge_times[EDGE_COUNT];
unsigned long now;
void IRAM_ATTR ISR() {
now = micros();
edge_times[count++] = now;
}
void printArray(const char *title, unsigned long *arr, int size) {
Serial.println(title);
Serial.print("[");
for (int i=0; i<size - 1; i++) {
Serial.print(arr[i]);
Serial.print(",");
}
Serial.print(arr[size -1]);
Serial.println("]");
}
//step 1
void printEdgeTimes() {
printArray("Edge Times", edge_times, EDGE_COUNT);
}
//step 2
void printPulseWidths() {
unsigned long pulse_widths[PULSE_COUNT];
for (int i=0; i<PULSE_COUNT; i++) {
pulse_widths[i] = edge_times[2*i+1] - edge_times[2*i];
}
printArray("Pulse widths", pulse_widths, PULSE_COUNT);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
attachInterrupt(IR_PIN, ISR, CHANGE);
}
void loop() {
if (count == EDGE_COUNT ) {
detachInterrupt(IR_PIN);
//printEdgeTimes();
printPulseWidths();
count = 0;
}
//Serial.println(count);
}