title: How to build an accurate iPhone strobe light using swift
body: <p>I am trying to build video stroboscopy app to power the light source on an otolaryngolgy endoscope. <a href="https://youtu.be/mJedwz_r2Pc" rel="nofollow noreferrer">https://youtu.be/mJedwz_r2Pc</a> shows an example of what a traditional stroboscopy system does. It flashes at 0.5 HZ below the fundamental frequency of the patient to induce a slow-motion effect that allows clinicians to visualize the motion of the cords and mucosal wave. To do this I need to strobe at roughly 120 to 250 HZ.</p>
  
  <p>I have used the printed functions with counters to verify my frequencies. When I comment out my code connecting the functions to the torch, I get an accurate frequency. When I uncomment the torch code, I lose accuracy. I do not understand why the torch functions are slowing down the strobe. Any insight or help would be greatly appreciated. </p>
  
  <pre><code>class StrobeLights {
    var counter: Int = 0
    var timer: Timer
    var isStrobing: Bool
    var isLightOn: Bool
    var frequency: Double
    var start = DispatchTime.now()
    var end = DispatchTime.now()
    var active: Bool
  
  
  
  
  
    init (){
      self.counter = 0
      self.timer = Timer()
      self.isStrobing = false
      self.isLightOn = false
      self.frequency = 200
      self.active = false
    }
  
    // Start Strobe process
    func toggleStrobe () {
      if isLightOn == true {
        self.isLightOn = false
        self.timer.invalidate()
        print("Turning timer off")
        self.end = DispatchTime.now()
        let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds
        let timeInterval = Double(nanoTime) / 1_000_000_000
        print("I counted this high \(counter) in this many seconds \(timeInterval) ")
        //toggleTorch(on: false)
        counter = 0
        incrementCounter()
      } else {
        self.isLightOn = true
        // change made by removing frequecy --&gt; 10
        self.timer = Timer.scheduledTimer(timeInterval: 1/frequency, target: self, selector: #selector(incrementCounter), userInfo: nil, repeats: true)
        print("Turning timer on")
        self.start = DispatchTime.now()
        //toggleTorch(on: true)
      }
    }
  
  
  
    // Increase counter by one
  
    @objc func incrementCounter () {
      self.toggleTorch(on: false)
      self.counter += 1
      //print("\(self.counter)")
      self.toggleTorch(on: true)
    }
    // Turns light on or off
    @objc func toggleTorch(on: Bool ) {
      guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }
      if device.hasTorch {
        if device.isTorchAvailable {
            do {
              try device.lockForConfiguration()
  
              if on == true {
                do {
                try device.setTorchModeOn(level: 0.5)
  
                } catch { print("Could not set torch level") }
                device.torchMode = .on
  
              } else {
                device.torchMode = .off
  
              }
              device.unlockForConfiguration()
            } catch {
              print("Torch could not be used")
            }
        } else {
            print( "torch unavailable")
        }
      } else {
        print("torch unavailable")
      }
    }
  }
  </code></pre>