/*Arduino program to simulate a USB keyboard's buttons depending on the rotation direction *of a rotary encoder. * *Made by TechBuild: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1 * *This example simulates the pressing of UP and DOWN arrow keys for clockwise and anticlockwise *rotation of the rotary encoder repectively. * *Feel free to modify the code to do other work for the rotation of the rotary encoder. *(Change volume, switch tabs, windows, etc) * */ #include //Two digital input pins to be connected to the output pins of the rotary encoder. #define inputA 5 #define inputB 6 int aState; int aLastState; void setup() { // put your setup code here, to run once: pinMode (inputA,INPUT); pinMode (inputB,INPUT); Keyboard.begin(); } void loop() { // put your main code here, to run repeatedly: aState = digitalRead(inputA); // Reads the "current" state of the inputA // If the previous and the current state of the inputA are different, that means a Pulse has occured. if (aState != aLastState){ // If the inputB state is different to the inputA state, that means the encoder is rotating clockwise. if (digitalRead(inputB) != aState) { //When the rotary encoder is detected to be rotating in one direction. Keyboard.press(KEY_UP_ARROW); //You can put anything to do inside the braces '{}'. Keyboard.release(KEY_UP_ARROW); } else {//When the rotary encoder is detected to be rotated the other direction. Keyboard.press(KEY_DOWN_ARROW); //You can put anything to do inside the braces '{}'. Keyboard.release(KEY_DOWN_ARROW); } } aLastState = aState; // Updates the previous state of the inputA with the current state. }