Skip to content

Instantly share code, notes, and snippets.

@nomolk
Last active November 26, 2019 15:24
Show Gist options
  • Save nomolk/ec12836ffd7e198d2e492d19ed251627 to your computer and use it in GitHub Desktop.
Save nomolk/ec12836ffd7e198d2e492d19ed251627 to your computer and use it in GitHub Desktop.
#include "Keyboard.h"
#include "Mouse.h"
const int KeyNum = 8;
int threshold[KeyNum] = {
//しなぷすのハード製作記 https://synapse.kyoto/calc/ResDiv/page001.html にて算出
46, 161, 296, 437, 585, 728, 857, 974
// R1 : 1kΩ 1k
// R2 : 110Ω 120
// R3 : 180Ω 120+47
// R4 : 270Ω 220+47
// R5 : 430Ω 330+100
// R6 : 820Ω 680+120
// R7 : 1.6kΩ 1k+680
// R8 : 5.1kΩ 2.2k+2.2k+680
};
int cursorPin = A1;
int scrollUpPin = 15;
int scrollDownPin = 14;
int leftClickPin = 2;
int rightClickPin = 3;
int leftDragPin = 4;
int rightDragPin = 5;
int doubleClickPin = 6;
int changeSpeedPin = 7;
int moveButtonPrv = -1;
int moveSpeedMode = 4;
const int moveSpeedModeNum = 4;
int moveSpeeds[moveSpeedModeNum] = {10, 30, 100, 200};
float moveSpeed = moveSpeeds[0];
boolean onDraggingLeft = false;
boolean onDraggingRight = false;
boolean scrollUpStatus;
boolean scrollDownStatus;
boolean leftClickStatus;
boolean rightClickStatus;
boolean leftDragStatus;
boolean rightDragStatus;
boolean doubleClickStatus;
boolean changeSpeedStatus;
boolean scrollUpPrv;
boolean scrollDownPrv;
boolean leftClickPrv;
boolean rightClickPrv;
boolean leftDragPrv;
boolean rightDragPrv;
boolean doubleClickPrv;
boolean changeSpeedPrv;
void setup() {
pinMode(scrollUpPin, INPUT_PULLUP);
pinMode(scrollDownPin, INPUT_PULLUP);
pinMode(leftClickPin, INPUT_PULLUP);
void setup() {
pinMode(scrollUpPin, INPUT_PULLUP);
pinMode(scrollDownPin, INPUT_PULLUP);
pinMode(leftClickPin, INPUT_PULLUP);
pinMode(rightClickPin, INPUT_PULLUP);
pinMode(leftDragPin, INPUT_PULLUP);
pinMode(rightDragPin, INPUT_PULLUP);
pinMode(doubleClickPin, INPUT_PULLUP);
pinMode(changeSpeedPin, INPUT_PULLUP);
Serial.begin(9600);
Mouse.begin();
}
void loop() {
byte i=0;
int cursorPinVal;
scrollUpStatus = (digitalRead(scrollUpPin) == LOW);
scrollDownStatus = (digitalRead(scrollDownPin) == LOW);
leftClickStatus = (digitalRead(leftClickPin) == LOW);
rightClickStatus = (digitalRead(rightClickPin) == LOW);
leftDragStatus = (digitalRead(leftDragPin) == LOW);
rightDragStatus = (digitalRead(rightDragPin) == LOW);
doubleClickStatus = (digitalRead(doubleClickPin) == LOW);
changeSpeedStatus = (digitalRead(changeSpeedPin) == LOW);
if(scrollUpStatus && !scrollUpPrv){
Mouse.move(0, 0, 1);
Serial.println("scrollUpPin");
}
if(scrollDownStatus && !scrollDownPrv){
Mouse.move(0, 0, -1);
Serial.println("scrollDownPin");
}
if(leftClickStatus && !leftClickPrv){
Mouse.press(MOUSE_LEFT);
Serial.println("leftClickPin");
}
if(!leftClickStatus && leftClickPrv){
Mouse.release(MOUSE_LEFT);
Serial.println("leftClickPin");
}
if(rightClickStatus && !rightClickPrv){
Mouse.click(MOUSE_RIGHT);
Serial.println("rightClickPin");
}
if(!rightClickStatus && rightClickPrv){
Mouse.release(MOUSE_RIGHT);
Serial.println("rightClickPin");
}
if(leftDragStatus && !leftDragPrv){
if(Mouse.isPressed(MOUSE_LEFT)){
Mouse.release(MOUSE_LEFT);
} else {
Mouse.press(MOUSE_LEFT);
}
Serial.println("leftDragPin");
}
if(rightDragStatus && !rightDragPrv){
if(Mouse.isPressed(MOUSE_RIGHT)){
Mouse.release(MOUSE_RIGHT);
} else {
Mouse.press(MOUSE_RIGHT);
}
Serial.println("rightDragPin");
}
if(doubleClickStatus && !doubleClickPrv){
Mouse.click(MOUSE_LEFT);
Mouse.click(MOUSE_LEFT);
Serial.println("DoubleClickPin");
}
cursorPinVal = analogRead(cursorPin);
while(i<KeyNum && cursorPinVal>threshold[i]){
i++;
}
if (moveSpeedMode == moveSpeedModeNum){ //自然加速のとき
if(i == moveButtonPrv){
moveSpeed = (moveSpeed < 200) ? (moveSpeed * 1.03) : 100;
} else {
moveSpeed = moveSpeeds[0];
}
}
if(i<KeyNum){
Serial.println(moveSpeed);
switch (i) {
case 0: //左
Mouse.move(-moveSpeed/10, 0, 0);
break;
case 1: //上
Mouse.move(0, -moveSpeed/10, 0);
break;
case 2: //右上
Mouse.move(moveSpeed / 10 * 0.71, -moveSpeed / 10 * 0.71, 0);
break;
case 3: //右
Mouse.move(moveSpeed/10, 0, 0);
break;
case 4: //右下
Mouse.move(moveSpeed / 10 * 0.71, moveSpeed / 10 * 0.71, 0);
break;
case 5: //下
Mouse.move(0, moveSpeed/10, 0);
break;
case 6: //左下
Mouse.move(-moveSpeed / 10 * 0.71, moveSpeed / 10 * 0.71, 0);
break;
case 7: //左上
Mouse.move(-moveSpeed / 10 * 0.71, -moveSpeed / 10 * 0.71, 0);
break;
}
moveButtonPrv = i;
}
if(changeSpeedStatus && !changeSpeedPrv){
Serial.println("changeSpeedPin");
moveSpeedMode++;
if(moveSpeedMode == moveSpeedModeNum + 1){
moveSpeedMode = 0;
}
Serial.println(moveSpeedMode);
if(moveSpeedMode == moveSpeedModeNum){ //自然加速
moveSpeed = moveSpeeds[0];
moveButtonPrv = -1;
} else {
moveSpeed = moveSpeeds[moveSpeedMode];
}
}
scrollUpPrv = scrollUpStatus;
scrollDownPrv = scrollDownStatus;
leftClickPrv = leftClickStatus;
rightClickPrv = rightClickStatus;
leftDragPrv = leftDragStatus;
rightDragPrv = rightDragStatus;
doubleClickPrv = doubleClickStatus;
changeSpeedPrv = changeSpeedStatus;
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment