int minepins[3] = {8 ,9 ,13};
int pin = 1;
int count = 0;
boolean First_run = true;
bool x_dominant;


typedef struct{
  uint32_t  counter_x,
            counter_y;

  uint8_t execute_step;
  
  uint32_t  step_counter;

  uint32_t  steps_x0,
            steps_y0,
            steps_x1,
            steps_y1;
            
  


}stepper_t;
static stepper_t st;


  void pulsgenerator(int x0, int y0, int x1, int y1){

    st.steps_x0 = (x0)*200; //Omformer fra mm til steg ved å gange distanse med steg per mm
    st.steps_y0 = (y0)*80;
    st.steps_x1 = (x1)*200; //Omformer fra mm til steg ved å gange distanse med steg per mm
    st.steps_y1 = (y1)*80;

    st.step_counter = max(st.steps_x1-st.steps_x0,st.steps_y1-st.steps_y0);
    st.counter_x = st.counter_y = (st.step_counter>>1);
  }


void setup()
{
  Serial.begin( 115200 );

  //set pins as outputs
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  cli();                      //stop interrupts for till we make the settings 
  //Timer 1 (interrupt each 2,032ms)
  TCCR1A = 0;                 // Reset entire TCCR1A to 0 
  TCCR1B = 0;                 // Reset entire TCCR1B to 0
  TCCR1B |= B00000011;        //Set CS12 to 1 so we get prescalar 64  
  TIMSK1 |= B00000010;        //Set OCIE1A to 1 so we enable compare match A
  OCR1A = 5;              //Finally we set compare register A to this value 

  TCCR2A = 0;                 // Reset entire TCCR1A to 0 
  TCCR2B = 0;                 // Reset entire TCCR1B to 0
  TCCR2B |= B00000100;        //Set CS20, CS21 and CS22 to 1 so we get prescalar 64  
  TIMSK2 |= B00000100;        //Set OCIE1B to 1 so we enable compare match B
  OCR2B = 20;                //Finally we set compare register B to this value 
  sei(); 
  
  pulsgenerator(0,0,20,320);

  

}

void loop()
{
}

ISR(TIMER2_COMPB_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)

//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
  //Timer2 (interrupt each 8.128 ms)
  // interrupt time = 1/(16Mhz/1024) * 127 =  8.128ms;
                 //Enable back the interrupts
  TCNT1  = 0;//initialize counter value to 0

  st.counter_x+=st.steps_x1;
  if (st.counter_x > st.step_counter){
    digitalWrite(minepins[1], HIGH);
    st.counter_x-=st.step_counter;
  }
  st.counter_y+=st.steps_y1;
  if (st.counter_y > st.step_counter){
    digitalWrite(minepins[2], HIGH);
    st.counter_y-=st.step_counter;
  }  
  
  
  st.step_counter--;
  //Serial.println();
  //Serial.print(st.step_counter);
  if (st.step_counter==0){

    delay(1000000000);
  }
}

ISR(TIMER1_COMPA_vect){//timer0 interrupt 2kHz toggles pin 8
//generates pulse wave of frequency 2kHz/2 = 1kHz (takes two cycles for full wave- toggle high then toggle low)
                  //First, set the timer back to 0 so it reset
  
  TCNT2  = 0; 
 // digitalWrite(minepins[0],LOW);
  
  digitalWrite(minepins[1],LOW);
  
  digitalWrite(minepins[2],LOW);


}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
D0D1D2D3D4D5D6D7GNDLOGIC
logic1:D0
logic1:D1
logic1:D2
logic1:D3
logic1:D4
logic1:D5
logic1:D6
logic1:D7
logic1:GND