Created
December 13, 2023 23:33
-
-
Save danybony/f84ee688e07befad4da81eef5a46b146 to your computer and use it in GitHub Desktop.
DigiMouse Arduino jiggler with timeout
This file contains hidden or 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
#define SIZE 40 | |
#define SPEED 30 | |
#include <DigiMouse.h> | |
const int inputPin = 2; | |
int buttonState; | |
int lastButtonState = LOW; | |
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled | |
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers | |
const int ledPin = 1; | |
int ledState = LOW; | |
int timeoutHours = 0; | |
int timeoutBeginMillis = 0; | |
const long notifyInterval = 200; | |
bool timeoutNotified = false; | |
unsigned long previousNotifyMillis = 0; | |
const long mouseMovementInterval = 10000; | |
unsigned long previousMouseMovementMillis = 0; | |
void setup() { | |
DigiMouse.begin(); | |
pinMode(ledPin, OUTPUT); | |
pinMode(inputPin, INPUT); | |
timeoutBeginMillis = millis(); | |
} | |
void loop() { | |
DigiMouse.update(); | |
int i; | |
int bigDiagonal = SIZE/3.1; | |
int smallDiagonal = SIZE/4; | |
int reading = digitalRead(inputPin); | |
if (reading != lastButtonState) { | |
// reset the debouncing timer | |
lastDebounceTime = millis(); | |
} | |
if ((millis() - lastDebounceTime) > debounceDelay) { | |
// whatever the reading is at, it's been there for longer than the debounce | |
// delay, so take it as the actual current state: | |
// if the button state has changed: | |
if (reading != buttonState) { | |
buttonState = reading; | |
// only toggle the LED if the new button state is HIGH | |
if (buttonState == HIGH) { | |
timeoutHours = timeoutHours + 1; | |
for (int h = 0; h < timeoutHours; h++) { | |
digitalWrite(ledPin, HIGH); | |
delay(notifyInterval); | |
digitalWrite(ledPin, LOW); | |
delay(notifyInterval); | |
} | |
} | |
} | |
} | |
lastButtonState = reading; | |
unsigned long currentMillis = millis(); | |
// calculate the end time when the user input the button | |
bool shouldContinue = (timeoutHours == 0) || (currentMillis - timeoutBeginMillis <= 3600000 * timeoutHours); | |
if (shouldContinue && currentMillis - previousMouseMovementMillis >= mouseMovementInterval) { | |
previousMouseMovementMillis = currentMillis; | |
for (i = 0; i < bigDiagonal; i++) { | |
DigiMouse.move(-i,-i,0); | |
DigiMouse.delay(SPEED); | |
} | |
for (i = 0; i < SIZE/4; i++) { | |
DigiMouse.move(0,-i,0); | |
DigiMouse.delay(SPEED); | |
} | |
for (i = 0; i < smallDiagonal; i++) { | |
DigiMouse.move(i,-i,0); | |
DigiMouse.delay(SPEED); | |
} | |
for (i = 0; i < smallDiagonal; i++) { | |
DigiMouse.move(i,i,0); | |
DigiMouse.delay(SPEED); | |
} | |
for (i = 0; i < smallDiagonal; i++) { | |
DigiMouse.move(i,-i,0); | |
DigiMouse.delay(SPEED); | |
} | |
for (i = 0; i < smallDiagonal; i++) { | |
DigiMouse.move(i,i,0); | |
DigiMouse.delay(SPEED); | |
} | |
for (i = 0; i < SIZE/4; i++) { | |
DigiMouse.move(0,i,0); | |
DigiMouse.delay(SPEED); | |
} | |
for (i = 0; i < bigDiagonal; i++) { | |
DigiMouse.move(-i,i,0); | |
DigiMouse.delay(SPEED); | |
} | |
// DigiMouse.move(20,0,0); // 2px right | |
// DigiMouse.delay(50); | |
// DigiMouse.move(-20,0,0); // 2px left | |
// DigiMouse.delay(10000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moves the mouse in a heart-shaped path every 10 seconds.
It reads input from a button and every time that button it's pressed, in increases the working timeout by 1 hour.
Based on ericdraken.com/usb-mouse-jiggler