#include #define PAUSE 1000 // 100mS gap means new number #define SNAPLEN 64 // grab this samples max #define CLOCK 2 #define DATA 3 #define CARD 4 volatile uint8_t bits[SNAPLEN]; volatile int byte_count = 0; volatile int bit_count = 0; volatile unsigned long when = 0; void pinChange() { bool a, b; a = digitalRead(CARD); if (a == HIGH) return; b = digitalRead(DATA); if (byte_count >= SNAPLEN) return; uint8_t mask = bits[byte_count]; if (bit_count == 0) mask = 0x00; mask <<= 1; if (b == LOW) mask |= 0x01; bit_count++; bits[byte_count] = mask; if (bit_count >= 8) { byte_count++; bit_count=0; } when = millis(); } void setup() { when = millis(); byte_count = 0; bit_count = 0; pinMode(CLOCK, INPUT_PULLUP); pinMode(DATA, INPUT_PULLUP); pinMode(CARD, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(CLOCK), pinChange, FALLING); Serial.begin(9600); help(); } void help() { Serial.println("Data Grabber (Clock + Data) / debugger"); Serial.println("Connect device to pins D2 & D3"); Serial.println("Commands: Z - Zero EEPROM. R - Read EEPROM, I - Immediate"); Serial.println(); } void loop() { unsigned long now = millis(); // hasnt been any new data for a while, write to eeprom if (byte_count >= SNAPLEN) { Serial.println("Buffer overflow. resetting."); byte_count = 0; bit_count = 0; } if (byte_count > 0 && when > now) { Serial.println("Clock wrap ?"); when = now - (PAUSE+1); } // time to write it bool card = digitalRead(CARD); if (byte_count > 0 && ( now > (when + PAUSE) || card == HIGH) ) { // find space in eeprom int start=0; while (start 0) { uint8_t mask = bits[byte_count+1]; mask <<= 8 - bit_count; byte_count++; bits[byte_count] = mask; } // it wont fit in the remaining space if (start+byte_count > EEPROM.length()) { start = -1; Serial.println("EEPROM full. use (z) to zero."); } for (int i=0; i