 
 #endif
 
-
-void pulse(int length, int value)
-{
+static const int prescaler = 1;
+static const double systemHz = 16000000;
+static const double bitLength = 52.0 / 1000000.0;
+static const unsigned int finetuning = 50; // fine-tuned with an oscilloscope
+static const unsigned int timerPreload = 65536 - systemHz / prescaler / (1 / bitLength) + finetuning;
+//static const unsigned int = 0;
+
+void setupTimer()
+{
+  // initialize timer1 
+  noInterrupts();           // disable all interrupts
+  TCCR1A = 0;
+  TCCR1B = 0;
+
+  TCNT1 = 34286;            // preload timer 65536-16MHz/256/2Hz
+//  TCCR1B |= (1 << CS12);    // 256 prescaler 
+  TCCR1B |= (1 << CS10);    // no prescaler 
+  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
+  interrupts();             // enable all interrupts
+}
+
+ISR(TIMER1_OVF_vect)
+{
+  TCNT1 = timerPreload;            // preload timer
+#if 1
+  if (writeBitsLeft > 0) {
+    --writeBitsLeft;
+    int value = ((sequence[writeCurByte] >> writeCurBit) & 1) ? HIGH : LOW;
+    digitalWrite(out, value);
+    digitalWrite(debug, value);
+    if (++writeCurBit == 8) {
+      writeCurBit = 0;
+      ++writeCurByte;
+    }
+  } else {
+    digitalWrite(out, HIGH);
+    digitalWrite(debug, HIGH);
+  }
+#else
+  static int value = 0;
+  value ^= 1;
   digitalWrite(out, value);
   digitalWrite(debug, value);
-  time += 52 * length;
-  delayMicroseconds(time - micros());
-//  delayMicroseconds(length * 52);
+#endif
 }
 
 // the loop routine runs over and over again forever:

    }
  }
  Serial.println();

}

void doit() {
  sendSequence(initsequence);
}

const unsigned sequenceSize = 200;
unsigned char sequence[sequenceSize + 1];
int sequenceAt = 0;
int prevSequenceSize = 0;

int sequenceAtBit = 0;
int inBinarySequence = 0;

void baseLoop() {
  if (Serial.available()) {

int in = 3;
int debug = 4;

const unsigned sequenceSize = 200;
unsigned char sequence[sequenceSize + 1];
int sequenceAt = 0; // for reading sequences
int sequenceAtBit = 0;
int prevSequenceSize = 0;

int inBinarySequence = 0;

volatile int writeBitsLeft = 0;
volatile int writeCurBit = 0;
volatile int writeCurByte = 0;

unsigned char initsequence[] = {
0,0,1,0,0,0,0,0,0,1,1,0,1,0,1,0,1,1,0,0,1,1,1,0,0,1,1,0,0,0,1,1,1,1,0,1,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,2,
