WIRING & CODING

Home FINAL

WIRING

You will need a Rotary Encoder and Digispark.



You will also need 4 female to female wires in order for it to work. There will be a picture below for reference.



After wiring, we will proceed with the coding in order for the thing to work.

CODING

I did not have a hard time understanding the codes because I am currently studying in Information Technology.
Those who have no coding background, no worries.
Code will be given below, check it out!


USB Volume knob(Code)
 
                   
  1. #include"TrinketHidCombo.h"
  2. #define"PIN_ENCODER_A 0"
  3. #define"PIN_ENCODER_B 2"
  4. #define"TRINKET_PINx PINB"
  5. static uint8_t enc_prev_pos = 0;
  6. static uint8_t enc_flags = 0;
  7.  
  8. void setup()
  9. {
  10. // set pins as input with internal pull-up resistors enabled
  11. pinMode(PIN_ENCODER_A, INPUT);
  12. pinMode(PIN_ENCODER_B, INPUT);
  13. digitalWrite(PIN_ENCODER_A, HIGH);
  14. digitalWrite(PIN_ENCODER_B, HIGH);
  15.  
  16. TrinketHidCombo.begin(); // start the USB device engine and enumerate
  17.  
  18. // get an initial reading on the encoder pins
  19. if (digitalRead(PIN_ENCODER_A) == LOW) {
  20. enc_prev_pos |= (1 << 0);
  21. }
  22. if (digitalRead(PIN_ENCODER_B) == LOW) {
  23. enc_prev_pos |= (1 << 1);
  24. }
  25. }
  26.  
  27. void loop()
  28. {
  29. int8_t enc_action = 0; // 1 or -1 if moved, sign is direction
  30.  
  31. // note: for better performance, the code will now use
  32. // direct port access techniques
  33. // http://www.arduino.cc/en/Reference/PortManipulation
  34. uint8_t enc_cur_pos = 0;
  35. // read in the encoder state first
  36. if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_A)) {
  37. enc_cur_pos |= (1 << 0);
  38. }
  39. if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_B)) {
  40. enc_cur_pos |= (1 << 1);
  41. }
  42.  
  43. // if any rotation at all
  44. if (enc_cur_pos != enc_prev_pos)
  45. {
  46. if (enc_prev_pos == 0x00)
  47. {
  48. // this is the first edge
  49. if (enc_cur_pos == 0x01) {
  50. enc_flags |= (1 << 0);
  51. }
  52. else if (enc_cur_pos == 0x02) {
  53. enc_flags |= (1 << 1);
  54. }
  55. }
  56.  
  57. if (enc_cur_pos == 0x03)
  58. {
  59. // this is when the encoder is in the middle of a "step"
  60. enc_flags |= (1 << 4);
  61. }
  62. else if (enc_cur_pos == 0x00)
  63. {
  64. // this is the final edge
  65. if (enc_prev_pos == 0x02) {
  66. enc_flags |= (1 << 2);
  67. }
  68. else if (enc_prev_pos == 0x01) {
  69. enc_flags |= (1 << 3);
  70. }
  71.  
  72. // check the first and last edge
  73. // or maybe one edge is missing, if missing then require the middle state
  74. // this will reject bounces and false movements
  75. if (bit_is_set(enc_flags, 0) && (bit_is_set(enc_flags, 2) || bit_is_set(enc_flags, 4))) {
  76. enc_action = 1;
  77. }
  78. else if (bit_is_set(enc_flags, 2) && (bit_is_set(enc_flags, 0) || bit_is_set(enc_flags, 4))) {
  79. enc_action = 1;
  80. }
  81. else if (bit_is_set(enc_flags, 1) && (bit_is_set(enc_flags, 3) || bit_is_set(enc_flags, 4))) {
  82. enc_action = -1;
  83. }
  84. else if (bit_is_set(enc_flags, 3) && (bit_is_set(enc_flags, 1) || bit_is_set(enc_flags, 4))) {
  85. enc_action = -1;
  86. }
  87.  
  88. enc_flags = 0; // reset for next time
  89. }
  90. }
  91.  
  92. enc_prev_pos = enc_cur_pos;
  93.  
  94. if (enc_action > 0) {
  95. TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_UP);
  96. }
  97. else if (enc_action < 0) {
  98. TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_DOWN);
  99. }
  100. else {
  101. TrinketHidCombo.poll(); // do nothing, check if USB needs anything done
  102. }
  103. }