Skip to content

Instantly share code, notes, and snippets.

@duhdugg
Last active August 10, 2020 01:50
Show Gist options
  • Save duhdugg/dda3c48fc89143529ac726dde45e0b52 to your computer and use it in GitHub Desktop.
Save duhdugg/dda3c48fc89143529ac726dde45e0b52 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from time import sleep
from gpiozero import LED
from ADCDevice import ADS7830
from threading import Thread
red = LED(4)
green = LED(5)
blue = LED(6)
adc = ADS7830()
# ADS7830 is an 8-bit analog-to-digital converter,
# The possible values I can get from the analog input is 0-255.
# Sometimes it reads as 1 when the potentiometer is turned completely down,
# so I'm using min_a_value=1 to set a higher threshold
min_a_value = 1
max_a_value = 255
# a_value will be the value from the analog input
a_value = 0
# I'm using separate threads to handle the analog input and LED outputs
class PThread(Thread):
def run(self):
# a_value is not accessible within thread without using global
global a_value
while True:
a_value = adc.analogRead(0)
sleep(0.05)
class LThread(Thread):
def run(self):
# a_value is not accessible within thread without using global
global a_value
# using c to count 1-100
c = 1
# using x to count 0-5
# LEDs will light up indicating the value of x as shown in this table:
#
# x | red | grn | blu
# --------------------
# 0 | off | off | on
# 1 | off | on | on
# 2 | on | on | on
# 3 | on | on | off
# 4 | on | off | off
# 5 | off | off | off
#
x = 0
while True:
# LEDs will not cycle if potentiometer is turned all the way down
if a_value > min_a_value:
# zzz_ms is the number of milliseconds between LED cycles
# possible values are 0-100
zzz_ms = int((1 - (a_value / max_a_value)) * 100)
# setting a minimum of 3ms
if zzz_ms < 3: zzz_ms = 3
# if c divided by zzz_ms has no remainder,
# LEDs will cycle according to the value of x in table above
if c % zzz_ms is 0:
blue.on() if x < 3 else blue.off()
green.on() if 0 < x < 4 else green.off()
red.on() if 1 < x < 5 else red.off()
# increment x
x += 1
# reset x counter at 6
if x > 5: x = 0
# increment c
c += 1
# reset c counter at 101
if c > 100: c = 1
# sleep for 1ms
sleep(0.01)
PThread().start()
LThread().start()
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment