Created
February 18, 2023 18:32
-
-
Save mharsch/52b0a99a945711385fb0120020d8226d to your computer and use it in GitHub Desktop.
Arduino replaces MT-VIKI KVM remote module for changing / polling selected KVM input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
// connected to 'D+' pin on mini USB | |
const int input_pin = 11; | |
// connected to 'D-' pin on mini USB | |
const int output_pin = 12; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pinMode(input_pin, INPUT); | |
pinMode(output_pin, OUTPUT); | |
digitalWrite(output_pin, HIGH); | |
} | |
void loop() { | |
if (Serial.available() > 0) { | |
int new_port = Serial.parseInt(); | |
if (new_port >= 1 && new_port <=8) { | |
set_port(new_port); | |
} | |
int port = get_port(); | |
Serial.println(port); | |
} | |
} | |
int get_port() { | |
unsigned long duration; | |
double ms; | |
int p; | |
duration = pulseIn(input_pin, LOW); | |
ms = round(duration / 1000.0); | |
p = ((int) ms / 20); | |
return (p); | |
} | |
void set_port(int p) { | |
digitalWrite(output_pin, LOW); | |
delay(20); | |
digitalWrite(output_pin, HIGH); | |
delay(20); | |
digitalWrite(output_pin, LOW); | |
delay(p * 20); | |
digitalWrite(output_pin, HIGH); | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case anyone wants to do this with the GPIO pins on a Raspberry Pi instead, here's the equivalent Python code.