#define FRONT_RISENOT #define DEBUG //NOT #define LED 7 #define INT0_PIN 2 #define MAX_FRONTS_AMO 100 #define MAX_MCS_FROM_LAST_FRONT 50000 // 50 милисекунд с последнего фронта #define ON_OFF_KEY_CODE 1692 volatile boolean ON_OFF = false; volatile unsigned long mcs_from_last_front = 0; volatile unsigned long mcs[MAX_FRONTS_AMO] = {0}; volatile unsigned fronts_amo = 0; void int0_routine() { if(fronts_amo >= MAX_FRONTS_AMO) return; mcs_from_last_front = micros(); mcs[fronts_amo] = mcs_from_last_front; fronts_amo++; } void setup() { #ifdef DEBUG Serial.begin(9600); #endif pinMode(LED, OUTPUT); pinMode(INT0_PIN, INPUT_PULLUP); digitalWrite(LED, ON_OFF); #ifdef FRONT_RISE attachInterrupt(0, int0_routine, RISING); #else attachInterrupt(0, int0_routine, CHANGE); #endif } void loop() { if (fronts_amo < 2) return; if (micros() > (mcs_from_last_front + MAX_MCS_FROM_LAST_FRONT)) { uint16_t hash = 0; for(int i = 0; i < fronts_amo - 1; i++) { uint16_t time_tween_fronts = mcs[i+1] - mcs[i]; #ifdef DEBUG Serial.println(time_tween_fronts, DEC); #endif time_tween_fronts /= 700; hash += (1 << i) * time_tween_fronts; } #ifdef DEBUG Serial.print("---- changes: "); Serial.print(fronts_amo, DEC); Serial.print("---- hash: "); Serial.println(hash, DEC); #endif if (hash == ON_OFF_KEY_CODE) digitalWrite(LED, ON_OFF = !ON_OFF); fronts_amo = 0; } }