Skip to content

Instantly share code, notes, and snippets.

@bign8
Last active May 25, 2025 04:26
Show Gist options
  • Select an option

  • Save bign8/db503c35703cfc7411472d8c816f91f3 to your computer and use it in GitHub Desktop.

Select an option

Save bign8/db503c35703cfc7411472d8c816f91f3 to your computer and use it in GitHub Desktop.
Digital Doodler
/*
Use two potentiometers as a digital etch-a-sketch.
Also, the builtin LED blinks every second :yey:
Author: Nate Woods < me@bign8.info >
Date: 2025-05-24
Source: https://gist.github.com/bign8/db503c35703cfc7411472d8c816f91f3
Board: AStar32U4 (compatible with Arduino Micro)
Variable Resistors connected to A0 and A5
*/
#include "Mouse.h"
const int DELAY = 16;
const int RESET = 1000 / DELAY;
int currentX, currentY;
int lastX, lastY;
bool ledOn = false;
int timer = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(A0, INPUT);
pinMode(A5, INPUT);
Mouse.begin();
Serial.begin(9600); // debugging
// pre-fill lastX/Y so first move isn't a huge jump
lastX = analogRead(A0);
lastY = analogRead(A5);
}
void loop() {
currentX = analogRead(A0);
currentY = analogRead(A5);
// smoothing current sensor read with previous sensor read (50/50 split)
currentX += lastX;
currentY += lastY;
currentX /= 2;
currentY /= 2;
// move the mouse if there is a change between previous and current values
Mouse.move(lastX - currentX, lastY - currentY);
// store off current values for next iteration
lastX = currentX;
lastY = currentY;
// if the serial debugger is attached, log out the current state
if (Serial) {
Serial.print(currentX);
Serial.print(",");
Serial.println(currentY);
}
// toggle the LED
timer++;
if (timer > RESET) {
timer = 0;
ledOn = !ledOn;
digitalWrite(LED_BUILTIN, ledOn);
}
// delay between analog reads for stability and to give computer a break
delay(DELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment