Skip to content

Instantly share code, notes, and snippets.

@adamcasey28
Created November 23, 2021 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcasey28/f1d346c86e2c2fb387cfdf87f1cda423 to your computer and use it in GitHub Desktop.
Save adamcasey28/f1d346c86e2c2fb387cfdf87f1cda423 to your computer and use it in GitHub Desktop.
Gemma M0 - Color Wheel
# Gemma IO demo
# Welcome to CircuitPython 5 :)
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn, AnalogOut
from touchio import TouchIn
import adafruit_dotstar as dotstar
import microcontroller
import board
import time
import random
# One pixel connected internally!
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Built in red LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
# Analog output on A0
aout = AnalogOut(board.A0)
# Analog input on A1
analog1in = AnalogIn(board.A1)
# Capacitive touch on A2
touch2 = TouchIn(board.A2)
# Helper to convert analog input to voltage
def getVoltage(pin):
return (pin.value * 3.3) / 65536
# Helper to give us a nice color swirl
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0:
# time.sleep(1)
return [0, 0, 0]
if pos > 255:
# time.sleep(1)
return [0, 0, 0]
if pos < 85:
# time.sleep(1)
return [int(pos * 3), int(255 - (pos * 3)), 0]
elif pos < 170:
# time.sleep(1)
pos -= 85
return [int(255 - pos * 3), 0, int(pos * 3)]
else:
# time.sleep(1)
pos -= 170
return [0, int(pos * 3), int(255 - pos * 3)]
i = 0
while True:
# spin internal LED around!
dot[0] = wheel(i)
dot.show()
# set analog output to 0-3.3V (0-65535 in increments)
aout.value = 100 * 256
led.value = touch2.value
i = (i + 1) % 256 # run from 0 to 255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment