antiquekid3

Microsoft Bus Mouse Code

Feb 26th, 2013
959
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //interrupt number
  2. #define int_quad1a 0 //pin 3
  3. #define int_quad1b 1 //pin 2
  4. #define int_quad2a 2 //pin 0
  5. #define int_quad2b 3 //pin 1
  6.  
  7. //pin definitions
  8. #define quad1a 3
  9. #define quad1b 2
  10. #define quad2a 0
  11. #define quad2b 1
  12. #define l_button 4
  13. #define r_button 5
  14.  
  15. volatile byte x_diff = 0;
  16. volatile byte y_diff = 0;
  17. volatile boolean l_button_pressed = false;
  18. volatile boolean r_button_pressed = false;
  19. volatile boolean l_button_last = false;
  20. volatile boolean r_button_last = false;
  21. volatile boolean change = false;
  22.  
  23. void setup()
  24. {
  25.   pinMode(quad1a, INPUT);
  26.   pinMode(quad1b, INPUT);
  27.   pinMode(quad2a, INPUT);
  28.   pinMode(quad2b, INPUT);
  29.   pinMode(l_button, INPUT);
  30.   pinMode(r_button, INPUT);
  31.   digitalWrite(quad1a, HIGH);
  32.   digitalWrite(quad1b, HIGH);
  33.   digitalWrite(quad2a, HIGH);
  34.   digitalWrite(quad2b, HIGH);
  35.   digitalWrite(l_button, HIGH);
  36.   digitalWrite(r_button, HIGH);
  37.   Serial.begin(19200);
  38.   //Mouse.begin();
  39.   attachInterrupt(int_quad1a, quad1a_sub, CHANGE);
  40.   attachInterrupt(int_quad1b, quad1b_sub, CHANGE);
  41.   attachInterrupt(int_quad2a, quad2a_sub, CHANGE);
  42.   attachInterrupt(int_quad2b, quad2b_sub, CHANGE);
  43. }
  44.  
  45. void loop()
  46. {
  47.   /*if (change)
  48.   {
  49.     Serial.print("xdiff: ");
  50.     Serial.print(x_diff);
  51.     Serial.print(" ydiff: ");
  52.     Serial.println(y_diff);
  53.     Serial.print("l_button: ");
  54.     Serial.print(l_button_pressed);
  55.     Serial.print(" r_button: ");
  56.     Serial.println(r_button_pressed);
  57.     //delay(10);
  58.     change = false;
  59.   }*/
  60.   if (digitalRead(l_button) == LOW && l_button_last == false)
  61.   {
  62.     change = true;
  63.     //l_button_pressed = true;
  64.     l_button_last = true;
  65.     Mouse.press(MOUSE_LEFT);
  66.   }
  67.   else if (digitalRead(l_button) == HIGH && l_button_last == true)
  68.   {
  69.     change = true;
  70.     //l_button_pressed = false;
  71.     l_button_last = false;
  72.     Mouse.release(MOUSE_LEFT);
  73.   }
  74.   if (digitalRead(r_button) == LOW && r_button_last == false)
  75.   {
  76.     change = true;
  77.     //r_button_pressed = true;
  78.     r_button_last = true;
  79.     Mouse.press(MOUSE_RIGHT);
  80.   }
  81.   else if (digitalRead(r_button) == HIGH && r_button_last == true)
  82.   {
  83.     change = true;
  84.     //r_button_pressed = false;
  85.     r_button_last = false;
  86.     Mouse.release(MOUSE_RIGHT);
  87.   }
  88. }
  89.  
  90. void quad1a_sub()
  91. {
  92.   change = true;
  93.   if (digitalRead(quad1a) == digitalRead(quad1b))
  94.   {
  95.     //x_diff--;
  96.     Mouse.move(-1,0,0);
  97.   }
  98.   else
  99.   {
  100.     //x_diff++;
  101.     Mouse.move(1,0,0);
  102.   }
  103. }
  104.  
  105. void quad1b_sub()
  106. {
  107.   change = true;
  108.   if (digitalRead(quad1a) != digitalRead(quad1b))
  109. {
  110.     //x_diff--;
  111.     Mouse.move(-1,0,0);
  112.   }
  113.   else
  114.   {
  115.     //x_diff++;
  116.     Mouse.move(1,0,0);
  117.   }
  118. }
  119.  
  120. void quad2a_sub()
  121. {
  122.   change = true;
  123.   if (digitalRead(quad2a) == digitalRead(quad2b))
  124.   {
  125.     //y_diff++;
  126.     Mouse.move(0,-1,0);
  127.   }
  128.   else
  129.   {
  130.     //y_diff--;
  131.     Mouse.move(0,1,0);
  132.   }
  133. }
  134.  
  135. void quad2b_sub()
  136. {
  137.   change = true;
  138.   if (digitalRead(quad2a) != digitalRead(quad2b))
  139.   {
  140.     //y_diff++;
  141.     Mouse.move(0,-1,0);
  142.   }
  143.   else
  144.   {
  145.     //y_diff--;
  146.     Mouse.move(0,1,0);
  147.   }
  148. }
RAW Paste Data