# define F_CPU 16000000UL  // 16 MHz clock speed

// Define X12 control pins
const int A_STEP = 8;  // Pin PB0
const int A_DIR = 9;   // Pin PB1
const int RESET = 11;  // Pin PB3

const int STEPS = 260 * 12;  // Max movement 260° (12 steps/deg)

// Create a SwitecX25 object
//SwitecX12 motor1(STEPS, A_STEP, A_DIR);  // steps for 260-degree movement

volatile uint16_t pulse_duration = 0;       // Store time between two pulses in microseconds
volatile uint8_t pulse_count = 0;           // Count the number of pulses
uint32_t rpm = 0;

void setup() {

  Serial.begin(500000);
    
    // Configure Timer1 (16-bit) in normal mode
  TCCR1A = 0;              // Normal mode
  TCCR1B = (1 << CS11);    // Prescaler = 8 (16 MHz / 8 = 2 MHz, 1 tick = 0.5 us)
  TCNT1 = 0;               // Reset Timer1 Counter
  TIMSK1 |= (1 << TOIE1);  // Enable overflow interrupt for Timer1

//  sei();
        
        
//Enable External Interrupt on INT0 (PD2)

pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), whyNot, CHANGE);

//  EICRA |= (1 << ISC01) | (1 << ISC00);  // Rising edge on INT0 triggers interrupt
//  EIMSK |= (1 << INT0);                  // Enable INT0 interrupt
    
  pinMode (RESET, OUTPUT);
  digitalWrite(RESET, HIGH);

  pinMode(7, OUTPUT);
  pinMode(6, INPUT_PULLUP);

  // Initialize motor

//  motor1.zero();
}

unsigned long lastValidTime;

volatile bool flag0, flag1;
volatile byte edge;

void wtf()
{
  digitalWrite(7, edge ? HIGH : LOW);

  if (flag1) {
    Serial.print("  one "); Serial.println(TCNT1);
    flag1 = false;
  }

  if (flag0) {
    Serial.print(" zero "); Serial.print(TCNT1);
    flag0 = false;
  }
}

bool motorAtZero;

void loop() {
  static unsigned long lastTime;
  unsigned long now = millis();

  Serial.println(TCNT1);

  return;

  if (now - lastTime >= 1000) {
    lastTime = now;

    static int counter = 1000;
//    Serial.println(counter); counter++;
  }

//  return;

//    Serial.print(" L "); Serial.println(TCNT1L);
//    Serial.print(" H "); Serial.println(TCNT1H);

static uint16_t wasCount, isCount, elasped;

isCount = TCNT1;
Serial.print(" is "); Serial.print((uint16_t) isCount);
elasped = isCount - wasCount;


Serial.print(" <tcnt1> "); Serial.print(TCNT1);
wasCount = isCount;

Serial.print(" == "); Serial.println(elasped);



delay(300);
return;


  wtf();


/*
  if (pulse_count == 0) digitalWrite(7, HIGH);
  else if (pulse_count == 1) digitalWrite(7, LOW);
  else Serial.println("WTF!");
*/

  rpm = 0;
  int duration_us;

  unsigned int myPulseDuration;
  noInterrupts();
    myPulseDuration = pulse_duration;
    pulse_duration = 0;
  interrupts();
    
  if (myPulseDuration > 0) {
     duration_us = myPulseDuration * 0.5;
        
     if (duration_us > 0) {
        rpm = 60000000 / duration_us / 4;

        int motorPosition = map(rpm, 0, 5000, 0, STEPS);

        Serial.print(TCNT1); Serial.print("  ");
        Serial.println("motor to RPM");

        motorAtZero = false;


//        motor1.stepTo(motorPosition);

// reset timer
        lastValidTime = now;
     }
  }      

// if we didn't move the needle for 75 milliseconds, the pulses must have stopped

  if (now - lastValidTime > 2777 && !motorAtZero) {
   Serial.println("\n       motor to ZERO");
   motorAtZero = true;
//    motor1.stepTo(0);  
  }
}

void whyNot() {
//  static byte edge;
  static uint16_t temp;

  if (edge == 0) {
    pulse_duration = 0;
    temp = TCNT1;
    edge = 1;
    flag0 = true;
  } else {
    pulse_duration = TCNT1 - temp;
    edge = 0;
    TCNT1 = 0;
    flag1 = true;
  }
}

void whyNot_() {
  if (pulse_count == 0) {
    pulse_count++;
    flag0 = true;
  } else if (pulse_count == 1) {
    pulse_count = 0;         // Reset pulse count for next measurement
    flag1 = true;
  }
}









/*

void setup() {
  Serial.begin(115200);
  Serial.println("X\n");
  Serial.println(TIMSK1, HEX);
  Serial.println(TOIE1);
  Serial.println(1 << TOIE1);
  
  delay(444);

//  TIMSK1 |= (1 << TOIE1);  // Enable overflow interrupt for Timer1

  delay(333);
}

void loop(){}

ISR(TIMER1_OVF_vect)
{
}
*/