Last active
November 13, 2019 20:30
-
-
Save EllieTheYeen/c940d25618848f64914e1e3b92105cf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Keyboard.h> | |
#include <Mouse.h> | |
byte invert = 0, active = 0, mactive = 0, state = 0, mstate = 0; | |
int ontimer = 0, offtimer = 0; | |
//#define dodebug | |
void setup() { | |
pinMode(5, INPUT_PULLUP); | |
pinMode(6, INPUT_PULLUP); | |
pinMode(7, INPUT_PULLUP); | |
pinMode(A0, INPUT); | |
#if defined(dodebug) | |
Serial.begin(9600); | |
#else | |
Keyboard.begin(); | |
Mouse.begin(); | |
#endif | |
} | |
void loop() { | |
mactive = !digitalRead(7); | |
if (mactive != mstate) { | |
mstate = mactive; | |
#if defined(dodebug) | |
if (mactive) { | |
Serial.println("m1"); | |
} else { | |
Serial.println("m0"); | |
} | |
#else | |
if (mactive) { | |
Mouse.press(); | |
} else { | |
Mouse.release(); | |
} | |
#endif | |
} | |
active = !digitalRead(6); | |
switch (state) { | |
case 0: | |
if (active) { | |
invert = !digitalRead(5); | |
int a = analogRead(A0); | |
int b = map(a, 0, 1023, 1, 1000); | |
ontimer = b; | |
offtimer = 1000 - b; | |
#if defined(dodebug) | |
if (invert) { | |
Serial.println("a1"); | |
} else { | |
Serial.println("d1"); | |
} | |
#else | |
if (invert) { | |
Keyboard.press('a'); | |
} else { | |
Keyboard.press('d'); | |
} | |
#endif | |
state++; | |
} | |
break; | |
case 1: | |
if (ontimer && active) { | |
ontimer--; | |
} else { | |
#if defined(dodebug) | |
if (invert) { | |
Serial.println("a0"); | |
} else { | |
Serial.println("d0"); | |
} | |
#else | |
if (invert) { | |
Keyboard.release('a'); | |
} else { | |
Keyboard.release('d'); | |
} | |
#endif | |
state++; | |
} | |
break; | |
case 2: | |
//Serial.println("state=2"); | |
if (offtimer) { | |
if (!active) state = 0; | |
offtimer--; | |
} else { | |
state = 0; | |
} | |
break; | |
} | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment