Created
September 16, 2017 19:24
-
-
Save objcode/4381c713a32cd6c3f13d55bd78a5cac6 to your computer and use it in GitHub Desktop.
How to make a tiered light indicator.
This file contains 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
# A tiered light indicator is pretty easy to build and this will show you how to build scalable indicators using (2n + 1) decoders | |
# and one arithmetic. I made mine with three lights, which is enough to understand at a glance what's going on. | |
# | |
# You can also put the logic on the lights and use fewer deciders but I prefer this setup because it's easier to modify. | |
# | |
# Inputs: | |
# - R is a constant "ratio". It should be set to the integer value of (tank size - 1 / # of lights). | |
# - resource types | |
# | |
# Variables: | |
# - Colors are used to control the lights and have a value of 1 | |
# - [0], [1], [2], .. are used to toggle light 0, 1, 2, and so on. Each light gets a decider. This allows you to make changes easily | |
# for example you might want to make an error state have a blinking light. | |
# - P is resource / R and is used to make logic independent of tank and light size. | |
def calc_P(R, resource): | |
return resource / R # this is an arith combinator + a constant combinator. | |
def calc_color(P): | |
if P == 0: | |
# decoder combinator | |
return red | |
if P == 1: | |
# decider combinator | |
return yellow | |
if P == 2: | |
# decider combinator | |
return green | |
if P >= 3: | |
# decider combinator | |
return red for error or blue if this is ok (tank is full) | |
# just line up four decider combinators and connect all the inputs. on a different line connect all the outputs. This group of | |
# four should always have exactly one color coming out! | |
def calc_numbers(P): | |
# Numbers are used to control the lights. If [1] is emitted then light 1 is on. More than one number will be emitted at the | |
# same time. I will use a function called "emit" to mean the output of a combinator | |
if P >= 0: | |
emit([0], 1) | |
if P >= 1: | |
emit([1], 1) | |
if P >= 2: | |
emit([2], 1) | |
# then all you need to do is wire up three lights: | |
light 0: | |
* Enable Disable ([0] = 1) | |
* Use Colors | |
light 1: | |
* Enable Disable ([1] = 1) | |
* Use Colors | |
light 2: | |
* Enable Disable ([2] = 1) | |
* Use Colors | |
# That's it! You can reduce the combinators by moving the number logic into the lights if you prefer a compact design. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment