02_readPins.ino 944 B

12345678910111213141516171819202122232425262728293031323334
  1. unsigned long highStartTimes[NUM_CHANNELS + 1];
  2. unsigned long lastInterrupt = 0;
  3. void readPins() {
  4. const unsigned long now = micros();
  5. for(byte ch = 1; ch <= NUM_CHANNELS; ch++) {
  6. byte pin = channels[ch];
  7. boolean isHigh = digitalRead(pin) == HIGH;
  8. if(isHigh && highStartTimes[ch] == 0) {
  9. // First time seeing this pin go high.
  10. highStartTimes[ch] = now;
  11. } else if(!isHigh && highStartTimes[ch] != 0) {
  12. // Channel was high, now low. Record how long it was high for.
  13. channelVals[ch] = now - highStartTimes[ch];
  14. highStartTimes[ch] = 0;
  15. }
  16. }
  17. lastInterrupt = millis();
  18. }
  19. /**
  20. * Detect controller disconnects by checking if it's been too long since we last had
  21. * an interurt occur. If so, set all channel values to 0.
  22. */
  23. void deadmanSwitch() {
  24. if(millis() - lastInterrupt > DEAD_MAN_MILLIS) {
  25. debugln(F("Deadman switch activated!"));
  26. for(byte ch = 1; ch <= NUM_CHANNELS; ch++) {
  27. channelVals[ch] = 0;
  28. }
  29. }
  30. }