Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //interrupt number
- #define int_quad1a 0 //pin 3
- #define int_quad1b 1 //pin 2
- #define int_quad2a 2 //pin 0
- #define int_quad2b 3 //pin 1
- //pin definitions
- #define quad1a 3
- #define quad1b 2
- #define quad2a 0
- #define quad2b 1
- #define l_button 4
- #define r_button 5
- volatile byte x_diff = 0;
- volatile byte y_diff = 0;
- volatile boolean l_button_pressed = false;
- volatile boolean r_button_pressed = false;
- volatile boolean l_button_last = false;
- volatile boolean r_button_last = false;
- volatile boolean change = false;
- void setup()
- {
- pinMode(quad1a, INPUT);
- pinMode(quad1b, INPUT);
- pinMode(quad2a, INPUT);
- pinMode(quad2b, INPUT);
- pinMode(l_button, INPUT);
- pinMode(r_button, INPUT);
- digitalWrite(quad1a, HIGH);
- digitalWrite(quad1b, HIGH);
- digitalWrite(quad2a, HIGH);
- digitalWrite(quad2b, HIGH);
- digitalWrite(l_button, HIGH);
- digitalWrite(r_button, HIGH);
- Serial.begin(19200);
- //Mouse.begin();
- attachInterrupt(int_quad1a, quad1a_sub, CHANGE);
- attachInterrupt(int_quad1b, quad1b_sub, CHANGE);
- attachInterrupt(int_quad2a, quad2a_sub, CHANGE);
- attachInterrupt(int_quad2b, quad2b_sub, CHANGE);
- }
- void loop()
- {
- /*if (change)
- {
- Serial.print("xdiff: ");
- Serial.print(x_diff);
- Serial.print(" ydiff: ");
- Serial.println(y_diff);
- Serial.print("l_button: ");
- Serial.print(l_button_pressed);
- Serial.print(" r_button: ");
- Serial.println(r_button_pressed);
- //delay(10);
- change = false;
- }*/
- if (digitalRead(l_button) == LOW && l_button_last == false)
- {
- change = true;
- //l_button_pressed = true;
- l_button_last = true;
- Mouse.press(MOUSE_LEFT);
- }
- else if (digitalRead(l_button) == HIGH && l_button_last == true)
- {
- change = true;
- //l_button_pressed = false;
- l_button_last = false;
- Mouse.release(MOUSE_LEFT);
- }
- if (digitalRead(r_button) == LOW && r_button_last == false)
- {
- change = true;
- //r_button_pressed = true;
- r_button_last = true;
- Mouse.press(MOUSE_RIGHT);
- }
- else if (digitalRead(r_button) == HIGH && r_button_last == true)
- {
- change = true;
- //r_button_pressed = false;
- r_button_last = false;
- Mouse.release(MOUSE_RIGHT);
- }
- }
- void quad1a_sub()
- {
- change = true;
- if (digitalRead(quad1a) == digitalRead(quad1b))
- {
- //x_diff--;
- Mouse.move(-1,0,0);
- }
- else
- {
- //x_diff++;
- Mouse.move(1,0,0);
- }
- }
- void quad1b_sub()
- {
- change = true;
- if (digitalRead(quad1a) != digitalRead(quad1b))
- {
- //x_diff--;
- Mouse.move(-1,0,0);
- }
- else
- {
- //x_diff++;
- Mouse.move(1,0,0);
- }
- }
- void quad2a_sub()
- {
- change = true;
- if (digitalRead(quad2a) == digitalRead(quad2b))
- {
- //y_diff++;
- Mouse.move(0,-1,0);
- }
- else
- {
- //y_diff--;
- Mouse.move(0,1,0);
- }
- }
- void quad2b_sub()
- {
- change = true;
- if (digitalRead(quad2a) != digitalRead(quad2b))
- {
- //y_diff++;
- Mouse.move(0,-1,0);
- }
- else
- {
- //y_diff--;
- Mouse.move(0,1,0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement