

void no_hall_programme(void)
{
  long timer_reading;
  long throttle_reading;

  static unsigned long last_swap = 0;
  unsigned long now;

  long deltatime;  // time between swaps, max 3000 ms
  long duty_promille;  // 0 - 1000, how long the electromagnet is on

  do
  {
    timer_reading = analogRead(timer_pin);
    throttle_reading = analogRead(throttle_pin);
    deltatime = map(timer_reading, 0, 1023, 50, 3000);
    duty_promille = throttle_reading * 1000 / 1024;
    
    now = millis();
    if (now > last_swap + deltatime)
    {
      swap(!swapper);
      if (swapper)
      {
        digitalWrite(pole1, HIGH);
        digitalWrite(pole2, LOW);
      }
      else
      {
        digitalWrite(pole2, HIGH);
        digitalWrite(pole1, LOW);
      }
      last_swap = now;
      duty(true);
      digitalWrite(LED_BUILTIN, HIGH);
    }
    if (now > last_swap + duty_promille * deltatime / 1000)
    {
      duty(false);
      digitalWrite(pole1, LOW);
      digitalWrite(pole2, LOW);
    }
  }
  while (digitalRead(mode1p) == LOW);
}

void swap(bool direc)
{
  swapper = direc;
  if (direc)
  {
    digitalWrite(coil1, HIGH);
    digitalWrite(coil2, LOW);
  }
  else
  {
    digitalWrite(coil1, LOW);
    digitalWrite(coil2, HIGH);
  }
  
  
}
