Etch-A-Sketch Mouse Code

Code for my Etch-A-Sketch Mouse project, which I’ve put here to not disrupt the flow of the post.

// Based on example from https://www.pjrc.com/teensy/td_libs_Encoder.html?#
#include <USB.h>
#include <USBHIDMouse.h>
#include <Encoder.h>
#include <ezButton.h>
USBHIDMouse Mouse;

// X movement
Encoder encX(10, 11);
long xPos = 0;
int xChange = 0;
// Y movement
Encoder encY(8, 9);
long yPos = 0;
long yChange = 0;
// Z movement
Encoder encZ(6, 7);
long zPos = 0;
long zChange = 0;
// Sensitivity
const int slider = A0;
// Clicks
const int togglePin = 2; 
int lState = 0;
int rState = 0;
int mState = 0;
ezButton buttonL(5);
ezButton buttonR(4);
ezButton buttonM(3);

void setup() {
  Serial.begin(9600);
  pinMode(togglePin, INPUT);
  Mouse.begin();
  USB.begin();
}

void loop() {
  buttonL.loop();
  buttonR.loop();
  buttonM.loop();
  int toggle = digitalRead(togglePin);
  long increment = map(analogRead(slider), 0, 4095, 1, 120);
  long scrollIncrement = map(analogRead(slider), 0, 4095, 1, 25);
  long xNew = encX.read();
  long yNew = encY.read();
  long zNew = encZ.read();

Mouse.move(xChange, yChange, zChange);
xChange = 0;
yChange = 0;
zChange = 0;

// X Y Scroll movement
  if (xNew != xPos) {
      if (xNew % 4 == 0) {
        xChange = -(xNew - xPos) * increment/4;
        Serial.print("xChange: ");
        Serial.println(xChange);
        xPos = xNew;
      }
   }
   
  if (yNew != yPos) {
      if (yNew % 4 == 0) {
        yChange = -(yNew - yPos) * increment/4;
        Serial.print("yChange: ");
        Serial.println(yChange);
        yPos = yNew;
      }
   }

  if (zNew != zPos) {
      if (zNew % 4 == 0) {
        zChange = -(zNew - zPos) * scrollIncrement/4;
        Serial.print("zChange: ");
        Serial.println(zChange);
        zPos = zNew;
      }
   }

// L R M button click and hold
  if(buttonL.isPressed()){
    if(toggle == HIGH && lState == 0){
      Serial.println("l click toggle on");
      lState = 1;
      Mouse.press(MOUSE_LEFT);
    }else if(toggle == HIGH && lState == 1){
      Serial.println("l click toggle off");
      lState = 0;
      Mouse.release(MOUSE_LEFT);
    }else if(toggle != HIGH){
      Serial.println("L Click");
      lState = 0;
      Mouse.click(MOUSE_LEFT);
    }
    }

    if(buttonR.isPressed()){
    if(toggle == HIGH && rState == 0){
      Serial.println("r click toggle on");
      rState = 1;
      Mouse.press(MOUSE_RIGHT);
    }else if(toggle == HIGH && rState == 1){
      Serial.println("r click toggle off");
      rState = 0;
      Mouse.release(MOUSE_RIGHT);
    }else if(toggle != HIGH){
      Serial.println("R Click");
      rState = 0;
      Mouse.click(MOUSE_RIGHT);
    }
    }

    if(buttonM.isPressed()){
    if(toggle == HIGH && mState == 0){
      Serial.println("m click toggle on");
      mState = 1;
      Mouse.press(MOUSE_MIDDLE);
    }else if(toggle == HIGH && mState == 1){
      Serial.println("m click toggle off");
      mState = 0;
      Mouse.release(MOUSE_MIDDLE);
    }else if(toggle != HIGH){
      Serial.println("M Click");
      mState = 0;
      Mouse.click(MOUSE_MIDDLE);
    }
    }
}