02_readPins.ino 599 B

1234567891011121314151617181920
  1. unsigned long highStartTimes[NUM_CHANNELS + 1];
  2. void readPins() {
  3. const unsigned long now = micros();
  4. // Serial.print("Interrupt started: ");
  5. // Serial.println(now);
  6. for(byte ch = 1; ch <= NUM_CHANNELS; ch++) {
  7. byte pin = channels[ch];
  8. boolean isHigh = digitalRead(pin) == HIGH;
  9. if(isHigh && highStartTimes[ch] == 0) {
  10. // First time seeing this pin go high.
  11. highStartTimes[ch] = now;
  12. } else if(!isHigh && highStartTimes[ch] != 0) {
  13. // Channel was high, now low. Record how long it was high for.
  14. channelVals[ch] = now - highStartTimes[ch];
  15. highStartTimes[ch] = 0;
  16. }
  17. }
  18. }