unsigned long highStartTimes[NUM_CHANNELS + 1]; unsigned long lastInterrupt = 0; void readPins() { const unsigned long now = micros(); for(byte ch = 1; ch <= NUM_CHANNELS; ch++) { byte pin = channels[ch]; boolean isHigh = digitalRead(pin) == HIGH; if(isHigh && highStartTimes[ch] == 0) { // First time seeing this pin go high. highStartTimes[ch] = now; } else if(!isHigh && highStartTimes[ch] != 0) { // Channel was high, now low. Record how long it was high for. channelVals[ch] = now - highStartTimes[ch]; highStartTimes[ch] = 0; } } lastInterrupt = millis(); } /** * Detect controller disconnects by checking if it's been too long since we last had * an interurt occur. If so, set all channel values to 0. */ void deadmanSwitch() { if(millis() - lastInterrupt > DEAD_MAN_MILLIS) { debugln(F("Deadman switch activated!")); for(byte ch = 1; ch <= NUM_CHANNELS; ch++) { channelVals[ch] = 0; } } }