ConformalConic ConformalConic - 4 months ago
220 0

Arduino Leonardo code for HID device to change lines and start/stop logging in Hypack Survey.

C#

Arduino Leonardo HID Code

/*Programmed By Chris using https://www.arduino.cc/en/Tutorial/KeyboardMessage as a guide
Reference: https://www.arduino.cc/en/Reference/MouseKeyboard
           https://www.arduino.cc/en/Reference/KeyboardModifiers
General Arduino Notes: https://www.arduino.cc/en/Guide/Environment
Arduino Leonardo Specfic Notes: https://www.arduino.cc/en/Guide/ArduinoLeonardoMicro#toc12    
*/

// definitions
#define DECREMENT 2
#define INCREMENT 4
#define LOG 3
#define pause 200

// libraries
#include <Keyboard.h>

//variables
int incPrevState = LOW;
int decPrevState = LOW;
int logPrevState = LOW;
int logging = false;

void setup() {
  pinMode(INCREMENT,INPUT);
  pinMode(DECREMENT,INPUT);
  pinMode(LOG,INPUT);
  delay(1000);
  //Serial.begin(9600);
  Keyboard.begin();
}

void loop(){
  
 int incState = digitalRead(INCREMENT);
 int decState = digitalRead(DECREMENT);
 int logState = digitalRead(LOG);
  
  if((incState != incPrevState) && (incState == HIGH)){
    //Serial.println("INCREMENT");
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press('i');
    delay(pause);
    Keyboard.releaseAll();
  }

  if((decState != decPrevState) && (decState == HIGH)){
    //Serial.println("DECREMENT"); //for debugging
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press('d');
    delay(pause);
    Keyboard.releaseAll();
  }

  if((logState != logPrevState) && (logState == HIGH)){
    if(logging == false){
    //Serial.println("LOG"); //for debugging
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press('s');
    logging = true;
    delay(pause + 400);
    Keyboard.releaseAll();
    }
 else{
      if(logging == true){
      //Serial.println("STOP LOGGING"); // for debugging
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press('e');
      logging = false;
      delay(pause + 400);
      Keyboard.releaseAll();
     }
    }
}
 incPrevState = incState;
 decPrevState = decState;
 logPrevState = logState;
  }
Comments