Skip to content

Instantly share code, notes, and snippets.

@nathfreder
Last active May 4, 2019 23:46
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 nathfreder/a0ea19e05a5c2e9caf168470dd905bce to your computer and use it in GitHub Desktop.
Save nathfreder/a0ea19e05a5c2e9caf168470dd905bce to your computer and use it in GitHub Desktop.
Truth Meter by @nugunski
# "Truth Meter" Code, by Alexander Petkov
import time
import board
import pulseio
import random
import sys
from adafruit_circuitplayground.express import cpx
# Setting variable for where on board the electrical signal for the servo will come from
servo = pulseio.PWMOut(board.A2, frequency=50)
# Setting funcion on how the servo will rotate
def servo_duty_cycle(pulse_ms, frequency=50):
period_ms = 1.0 / frequency * 1000.0
duty_cycle = int(pulse_ms / (period_ms / 65535.0))
return duty_cycle
# "Truth Meter" loop (all functionality of the trust meter will run through here)
while True:
# Much like a dice roll, random.randint will select a number between 1 and 5. Depending on the number drawn, a certain function will be run later in the code
response = random.randint(1, 5)
# Loop that will run when Circuit Python is tapped
while True:
# The LEDS on the Circuit Python will blink when it is tapped
if cpx.tapped:
cpx.pixels[0] = (1, 0, 3)
cpx.pixels[1] = (1, 0, 3)
cpx.pixels[2] = (1, 0, 3)
cpx.pixels[3] = (1, 0, 3)
cpx.pixels[4] = (1, 0, 3)
cpx.pixels[5] = (1, 0, 3)
cpx.pixels[6] = (1, 0, 3)
cpx.pixels[7] = (1, 0, 3)
cpx.pixels[8] = (1, 0, 3)
cpx.pixels[9] = (1, 0, 3)
time.sleep(0.5)
cpx.pixels[0] = (0, 0, 0)
cpx.pixels[1] = (0, 0, 0)
cpx.pixels[2] = (0, 0, 0)
cpx.pixels[3] = (0, 0, 0)
cpx.pixels[4] = (0, 0, 0)
cpx.pixels[5] = (0, 0, 0)
cpx.pixels[6] = (0, 0, 0)
cpx.pixels[7] = (0, 0, 0)
cpx.pixels[8] = (0, 0, 0)
cpx.pixels[9] = (0, 0, 0)
# Loop that will rotate the servo in certain positions according to the number drawn earlier using the randint function
while True:
if cpx.button_b:
sys.exit() # Stops running the code when button B is pressed
# Rotations of the servo according to ms pulses
elif response == 1:
servo.duty_cycle = servo_duty_cycle(2.45)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(
2.35
) # Brief shift in position of the servo to give the illusion of momentum as it rotates
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(2.45)
time.sleep(0.2)
sys.exit()
elif response == 2:
servo.duty_cycle = servo_duty_cycle(2.0)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(
2.1
) # Brief shift in position of the servo to give the illusion of momentum as it rotates
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(2.0)
time.sleep(0.2)
sys.exit()
elif response == 3:
servo.duty_cycle = servo_duty_cycle(1.5)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(1.6)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(1.5)
time.sleep(0.2)
sys.exit()
elif response == 4:
servo.duty_cycle = servo_duty_cycle(0.9)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(1.0)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(0.9)
time.sleep(0.2)
sys.exit()
elif response == 5:
servo.duty_cycle = servo_duty_cycle(0.1)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(0.6)
time.sleep(0.2)
servo.duty_cycle = servo_duty_cycle(0.1)
time.sleep(0.2)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment