 Flameduino Controls an ignition coil with a set dwell (compile time)
 and variable spark frequency, specifically for purposes of flamethrowing.
 */
-#include <TimerOne.h>
+//#include <TimerOne.h>
+#define TIMER1_RESOLUTION 65536UL  // Timer1 is 16 bit
 
 // -------------------------
 //     Pin Definitions

  
  //Use built in pull-up resistor
  digitalWrite(ACTIVATION_PIN, HIGH);
  active = digitalRead(ACTIVATION_PIN) == LOW;
  dischargeCoil();

  attachInterrupt(TACH_PIN - 2, ISR_TACH, RISING);
  attachInterrupt(ACTIVATION_PIN - 2, ISR_ACTIVE, CHANGE);
  
  Timer1.initialize();
  Timer1.attachInterrupt(ISR_IGNITION);
  Timer1.start();
  
  if (debug)
  {

// -------------------------
void ISR_TACH()
{
  unsigned long current = micros();
  tachPrevious = tachCurrent;
  tachCurrent = current;
}

void ISR_ACTIVE()
{
  //Set the active global variable
  active = isActive();
}

void ISR_IGNITION()
{
  //Interrupt has fired
  updateTimer = true;
}

//Attach ISR to Timer1 Overflow
ISR(TIMER1_OVF_vect)
{
  ISR_IGNITION();
}

// -------------------------
// Generic Utility Functions
// -------------------------
void delayFixed(long milliseconds)
{
  if (milliseconds <= 0)
    return;
  else if (milliseconds < 16)
    delayMicroseconds(milliseconds * 1000);
  else
    delay(milliseconds);
}

void initializeTimer1()
{
  noInterrupts();
  //Set Mode: Phase and Frequency correct PWM (stops the timer)
  TCCR1A = 0;                 //Set Mode (bit 1A)
  TCCR1B = bit(WGM13);        //Set Mode (bit 1B)
  TIMSK1 = bit(TOIE1);        //Enable Overflow ISR
  interrupts();
}

void startTimer1(unsigned long microseconds)
{
  char clockSelectBits;
  short pwmPeriod;
  
  //Calculate clock select bits
  const unsigned long cycles = (F_CPU / 2000000) * microseconds;
  if (cycles < TIMER1_RESOLUTION) 
  {
    //No Prescaling
    clockSelectBits = bit(CS10);
    pwmPeriod = cycles;
  } 
  else if (cycles < TIMER1_RESOLUTION * 8) 
  {
    //Prescale by /8
    clockSelectBits = bit(CS11);
    pwmPeriod = cycles / 8;
  }
  else if (cycles < TIMER1_RESOLUTION * 64) 
  {
    //Prescale by /64
    clockSelectBits = bit(CS11) | bit(CS10);
    pwmPeriod = cycles / 64;
  } 
  else if (cycles < TIMER1_RESOLUTION * 256) 
  {
    //Prescale by /256
    clockSelectBits = bit(CS12);
    pwmPeriod = cycles / 256;
  }
  else if (cycles < TIMER1_RESOLUTION * 1024) 
  {
    //Prescale by /1024
    clockSelectBits = bit(CS12) | bit(CS10);
    pwmPeriod = cycles / 1024;
  } 
  else
  {
    //Out of bounds, use maximum prescaler
    clockSelectBits = bit(CS12) | bit(CS10);
    pwmPeriod = TIMER1_RESOLUTION - 1;
  }

  noInterrupts();
  TCNT1 = 1;                             //Resets the counter
  ICR1 = pwmPeriod;                      //Sets the Period
  //Set Mode: Phase and Frequency correct PWM (Starts the timer)
  TCCR1A = 0;                            //Set Mode (bit 1A)
  TCCR1B = bit(WGM13) | clockSelectBits; //Set Mode (bit 1B)
  //unsigned int tcnt1;
  //do {  // Nothing -- wait until timer moved on from zero - otherwise get a phantom interrupt
  //  tcnt1 = TCNT1;
  //} while (tcnt1==0);
  interrupts();
}

void stopTimer1()
{
  noInterrupts();
  //Set Mode: Phase and Frequency correct PWM (stops the timer)
  TCCR1A = 0;                 //Set Mode (bit 1A)
  TCCR1B = bit(WGM13);        //Set Mode (bit 1B)
  interrupts();
}

long readInternalTemp() 
