     {:noreply, state}
   end
 
-  def handle_info({:nerves_uart, _port, message}, state) do
-    Logger.info(message)
+  def handle_info({:nerves_uart, port, message}, state) when !Map.has_key?(state, port) do
     {:noreply, state}
+  end
+
+  def handle_info({:nerves_uart, port, message}, state) when Map.has_key?(state, port) do
+    settings = Map.get(state, port)
+    %{counter: counter, upper_limit: upper_limit} = settings
+
+    if counter == upper_limit do
+      Logger.info(message)
+
+      new_direction = -1 * direction
+      new_counter = counter + new_direction
+      new_settings = %{settings | direction: new_direction, counter: new_counter}
+      new_state = Map.put(state, port, new_settings)
+      {:noreply, new_state}
+    else
+      new_counter = counter + direction
+      new_settings = %{settings | counter: new_counter}
+      new_state = Map.put(state, port, new_settings)
+      {:noreply, new_state}
+    end
   end
 
   def handle_info(msg, state) do

        with :ok <- UART.write(uart_pid(), @off),
             :ok <- UART.drain(uart_pid()),
             :ok <- UART.close(uart_pid()),
             do: remove_backend(Map.get(state, port))

      case msg do
        :ok ->


      case msg do
        :ok ->
          {:ok, upper_limit} = calculate_upper_limit(sample_rate)
          settings = %{
            log_path: log_path,
            sample_rate: sample_rate,
            lower_limit: 0,
            upper_limit: upper_limit,
            counter: 0,
            direction: -1
          }

          new_state = Map.put(state, port, settings)
          {:reply, msg, new_state}

        _ ->
