I have been playing around with the Adafruit ItsyBitsy M4 Express and CircuitPython. Tonight I was digging through my box of attachments to see what I could learn about. A joystick. Well that sounds like fun. All I have with me is the small, 128×64 OLED scree, but it will work for now. If the concept lives then I will find something larger. I had to do a little digging around to find some examples for the mouse.

The first attempt was pretty funny as I ended up usurping my Mac’s mouse. The mouse moves quicker than the screen can keep up and when you let go of the joystick is returns to zero. I am working on those points next. When the joystick button is pressed the screen is cleared. And just to note, on my generic OLED I had to add pull ups to each SDA and SCL.

The Code

# ItsyBitsy stickDraw

import board
import time
import busio
import digitalio
import analogio
import adafruit_ssd1306
from simpleio import map_range
from adafruit_hid.mouse import Mouse

# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)

mouse = Mouse()

# Create the display
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
display.fill(0)
display.show()

# Setting up the Joystick (mouse)
x_axis = analogio.AnalogIn(board.A0)
y_axis = analogio.AnalogIn(board.A1)

# Setup the Clear Screen button
select = digitalio.DigitalInOut(board.A2)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP

pot_min = 0.00
pot_max = 3.29
step = (pot_max - pot_min) / 20.0

def get_voltage(pin):
    return (pin.value * 3.3) / 65536

def steps(axis):
    # creating steps from 0-20
    return round((axis - pot_min) / step)

# Time to go to work
while True:

    x = map_range(x_axis.value, 0, 65535, 128 - 1, 0)
    y = map_range(y_axis.value, 0, 65535, 0, 64 - 1)

    # Check for Clear Screen button
    if select.value is False:
        mouse.click(Mouse.LEFT_BUTTON)
        # Debounce delay
        time.sleep(0.2)
        # Clear the screen
        display.fill(0)
        display.show()

    # Drawing path on the screen
    display.pixel(int(x), int(y), 1)
    display.show()

© 2019, wrightmac. All rights reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.