Advertisement
j0h

sketchYetch.ino

j0h
Mar 15th, 2024
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Encoder.h>
  2. #include <Keyboard.h>
  3.  
  4. // Deine IO
  5. #define ENCODER1_PINA 1
  6. #define ENCODER1_PINB 3
  7. #define ENCODER2_PINA 5
  8. #define ENCODER2_PINB 7
  9. #define color_Btn     11  // g key
  10. #define clear_Btn     27  // c key
  11. #define lift_Btn      9   // l key
  12. #define save_Btn      29  // s key
  13. #define demo_Btn      31  // d
  14. // Create encoder objects
  15. // Apparently encoder.h inits my pins
  16. Encoder encoder1(ENCODER1_PINA, ENCODER1_PINB);
  17. Encoder encoder2(ENCODER2_PINA, ENCODER2_PINB);
  18.  
  19. // Variables to track encoder movement direction
  20. int prevEncoder1Position = 0;
  21. int prevEncoder2Position = 0;
  22.  
  23. void setup() {
  24.   //pinMode(ENCODER1_PINA, INPUT_PULLUP);
  25.   //pinMode(ENCODER1_PINB, INPUT_PULLUP);
  26.   //pinMode(ENCODER2_PINA, INPUT_PULLUP);
  27.   //pinMode(ENCODER2_PINB, INPUT_PULLUP);
  28.   pinMode(color_Btn, INPUT_PULLUP);
  29.   pinMode(clear_Btn, INPUT_PULLUP);
  30.   pinMode(lift_Btn,  INPUT_PULLUP);
  31.   pinMode(save_Btn,  INPUT_PULLUP);
  32.   pinMode(demo_Btn,  INPUT_PULLUP);  
  33.  
  34.   Serial.begin(9600);
  35.   delay(3000);
  36.   while (!Serial) {
  37.     ; // Wait for serial port to connect
  38.   }
  39.  
  40.   Keyboard.begin();
  41. }
  42.  
  43. void loop() {
  44.   // Read encoder positions
  45.   int encoder1Position = encoder1.read();
  46.   int encoder2Position = encoder2.read();
  47.  
  48.   // Detect encoder 1 movement direction
  49.   int encoder1Direction = 0;
  50.   if (encoder1Position > prevEncoder1Position) {
  51.     encoder1Direction = 1; // Positive direction
  52.   } else if (encoder1Position < prevEncoder1Position) {
  53.     encoder1Direction = -1; // Negative direction
  54.   }
  55.  
  56.   // Detect encoder 2 movement direction
  57.   int encoder2Direction = 0;
  58.   if (encoder2Position > prevEncoder2Position) {
  59.     encoder2Direction = 1; // Positive direction
  60.   } else if (encoder2Position < prevEncoder2Position) {
  61.     encoder2Direction = -1; // Negative direction
  62.   }
  63.  
  64.   // Update previous positions for next iteration
  65.   prevEncoder1Position = encoder1Position;
  66.   prevEncoder2Position = encoder2Position;
  67.  
  68.   // Use encoder direction variables to perform actions
  69.   if (encoder1Direction == 1) {
  70.     Keyboard.press(KEY_LEFT_ARROW);
  71.     Keyboard.release(KEY_LEFT_ARROW);
  72.   } else if (encoder1Direction == -1) {
  73.     Keyboard.press(KEY_RIGHT_ARROW);
  74.     Keyboard.release(KEY_RIGHT_ARROW);
  75.   } else {
  76.     Keyboard.release(KEY_RIGHT_ARROW);
  77.     Keyboard.release(KEY_LEFT_ARROW);
  78.   }
  79.  
  80.   if (encoder2Direction == 1) {
  81.     Keyboard.press(KEY_UP_ARROW);
  82.     Keyboard.release(KEY_UP_ARROW);
  83.   } else if (encoder2Direction == -1) {
  84.     //Serial.println("Encoder 2: Negative");
  85.     Keyboard.press(KEY_DOWN_ARROW);
  86.     Keyboard.release(KEY_DOWN_ARROW);
  87.   } else {
  88.     Keyboard.release(KEY_DOWN_ARROW);
  89.     Keyboard.release(KEY_UP_ARROW);
  90.   }
  91.  
  92.  
  93.   if(digitalRead(color_Btn) == LOW){
  94.    // Serial.println("G");
  95.     Keyboard.press(KEY_G);
  96.     Keyboard.release(KEY_G);
  97.     }
  98.   if(digitalRead(clear_Btn) == LOW){
  99.     //Serial.println("C");
  100.     Keyboard.press(KEY_C);
  101.     Keyboard.release(KEY_C);
  102.     }
  103.    
  104.   if(digitalRead(lift_Btn) == LOW){
  105.     //Serial.println("L");
  106.     Keyboard.press(KEY_L);
  107.     Keyboard.release(KEY_L);
  108.     }
  109. /*
  110.   if(digitalRead(save_Btn) == LOW){
  111.     Serial.println("S");
  112.     //Keyboard.press(KEY_S);
  113.     //Keyboard.release(KEY_S);
  114.     delay(3000); //can we save a file in 3s?
  115.     }
  116.  
  117.   if(digitalRead(demo_Btn) == LOW){
  118.     Serial.println("D");
  119.     //Keyboard.press(KEY_D);
  120.     //Keyboard.release(KEY_D);
  121.     }  
  122. */
  123.   // Delay to prevent spamming
  124.   delay(100);
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement