180 lines
5.6 KiB
Python
180 lines
5.6 KiB
Python
#imports all necessary libraries and sets predetermined values
|
|
import RPi.GPIO as GPIO
|
|
import time
|
|
from apa102_pi.driver import apa102
|
|
|
|
strip = apa102.APA102(num_led=8, order='rgb')
|
|
strip.clear_strip()
|
|
|
|
#Function to light up an LED (je moet een hex waarde aanroepen in de vorm van bijv 0xFF0000)
|
|
def Strip(pixel, hex):
|
|
strip.set_pixel_rgb(pixel, hex)
|
|
strip.show()
|
|
|
|
#Function to reset the entire strip after a delay
|
|
def resetstrip(delay):
|
|
time.sleep(delay)
|
|
strip.clear_strip()
|
|
time.sleep(0.2)
|
|
|
|
#Function to fill the entire strip with 1 color
|
|
def fullstrip(hex):
|
|
Strip(0, hex)
|
|
Strip(1, hex)
|
|
Strip(2, hex)
|
|
Strip(3, hex)
|
|
Strip(4, hex)
|
|
Strip(5, hex)
|
|
Strip(6, hex)
|
|
Strip(7, hex)
|
|
return hex
|
|
|
|
#Function to slowly fill strip from pixel 0 to 7 with a color (bc idk... fancy?)
|
|
def fillstrip(hex, delay):
|
|
Strip(0, hex)
|
|
time.sleep(delay)
|
|
Strip(1, hex)
|
|
time.sleep(delay)
|
|
Strip(2, hex)
|
|
time.sleep(delay)
|
|
Strip(3, hex)
|
|
time.sleep(delay)
|
|
Strip(4, hex)
|
|
time.sleep(delay)
|
|
Strip(5, hex)
|
|
time.sleep(delay)
|
|
Strip(6, hex)
|
|
time.sleep(delay)
|
|
Strip(7, hex)
|
|
|
|
######################################
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setwarnings( 0 )
|
|
|
|
#Hier de aangesloten pinnen invoeren (17, 27, 22 is wat ik heb gebruikt dus denk ik handig om zo te houden?)
|
|
shift_clock_pin = 17
|
|
latch_clock_pin = 27
|
|
data_pin = 22
|
|
|
|
GPIO.setup( shift_clock_pin, GPIO.OUT )
|
|
GPIO.setup( latch_clock_pin, GPIO.OUT )
|
|
GPIO.setup( data_pin, GPIO.OUT )
|
|
|
|
def hc595( shift_clock_pin, latch_clock_pin, data_pin, value, delay ):
|
|
|
|
for k in range(0, 8): # 8 keer loopen
|
|
if value % 2 == 1: # als waarde oneven is
|
|
GPIO.output(data_pin, GPIO.HIGH) # data pin hoog zetten
|
|
else: # waarde is even
|
|
GPIO.output(data_pin, GPIO.LOW) # data pin laag houden
|
|
value = value // 2 # waarde halveren
|
|
|
|
GPIO.output(shift_clock_pin, GPIO.HIGH) # klok hoog, om de waarde pin te laten aflezen
|
|
GPIO.output(shift_clock_pin, GPIO.LOW) # klok laag, default positie
|
|
|
|
|
|
GPIO.output(latch_clock_pin, GPIO.HIGH) # geen idee, waarschijnlijk om de waardes op te laten slaan in de chip
|
|
GPIO.output(latch_clock_pin, GPIO.LOW) # default positie
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
|
|
|
|
delay = 0.1
|
|
try:
|
|
while True:
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 1, delay ) # waarde is 1 dus de eerste led hoort te branden de rest niet
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 2, delay ) # waarde is 2 dus de tweede led en niet de 1e hoort te branden
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 4, delay ) # waarde is 4 dus de 3e led hoort te branden de rest niet
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 8, delay ) # enzovoort
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 16, delay )
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 32, delay )
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 64, delay )
|
|
hc595( shift_clock_pin, latch_clock_pin, data_pin, 128, delay ) # de 8e led en niet meer
|
|
except KeyboardInterrupt:
|
|
GPIO.cleanup()
|
|
|
|
###############################################
|
|
GPIO.setmode( GPIO.BCM )
|
|
GPIO.setwarnings( 0 )
|
|
|
|
#zet hier de echo en trig pin die word gebruikt
|
|
|
|
TrigPin = 14
|
|
EchoPin = 15
|
|
|
|
sr04_trig = TrigPin
|
|
sr04_echo = EchoPin
|
|
|
|
GPIO.setup( sr04_trig, GPIO.OUT )
|
|
GPIO.setup( sr04_echo, GPIO.IN, pull_up_down=GPIO.PUD_DOWN )
|
|
|
|
def sr04( trig_pin, echo_pin ):
|
|
"""
|
|
Return the distance in cm as measured by an SR04
|
|
that is connected to the trig_pin and the echo_pin.
|
|
These pins must have been configured as output and input.s
|
|
"""
|
|
|
|
GPIO.output(trig_pin, False) # altijd met false starten
|
|
time.sleep(0.5)
|
|
GPIO.output(trig_pin, True)
|
|
time.sleep(0.00001) # pulse trigger
|
|
GPIO.output(trig_pin, False)
|
|
|
|
while GPIO.input(echo_pin) == False:
|
|
start_time = time.time() # tijd opslaan wanneer de echo false is
|
|
|
|
while GPIO.input(echo_pin) == True:
|
|
end_time = time.time() # tijd opslaan wanneer de echo true is
|
|
|
|
total_time = end_time - start_time # totaal aantal afstand in seconden
|
|
return (round(total_time*34300/2, 2), total_time) #tuple terug geven met afstand in cm en totaal aantal seconden
|
|
# sensor is niet helemaal naukeurig(~1-2cm variatie), kan ook aan de rpi timing liggen
|
|
|
|
try:
|
|
while True:
|
|
print( sr04( sr04_trig, sr04_echo ))
|
|
time.sleep( 0.5 )
|
|
except KeyboardInterrupt:
|
|
GPIO.cleanup()
|
|
|
|
###########################################
|
|
#Zet hier neer welke pin de Servo aanstuurd
|
|
servoPIN = 14
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setup(servoPIN, GPIO.OUT)
|
|
|
|
p = GPIO.PWM(servoPIN, 50)
|
|
p.start(2.5)
|
|
|
|
#geef het aantal graden in tussen 0 en 180 (dit is niet hoeveelheid verplaatsing maar naar welke positie)
|
|
def servo(degrees):
|
|
if degrees > 180:
|
|
return "Sorry, je kan maximaal 180 graden ingeven"
|
|
elif degrees < 0:
|
|
return "Sorry, je moet minimaal 0 graden ingeven"
|
|
leftSpan = 180 - 0
|
|
rightSpan = 12 - 2.5
|
|
valueScaled = float(degrees - 0) / float(leftSpan)
|
|
integer = 2.5 + (valueScaled * rightSpan)
|
|
p.ChangeDutyCycle(integer)
|
|
return "ok"
|
|
|
|
###########################################
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
#Insert GPIO Pin that Switch is connected to
|
|
SwitchPIN = 27
|
|
|
|
GPIO.setup(SwitchPIN,GPIO.IN)
|
|
input = GPIO.input(SwitchPIN)
|
|
|
|
#Function to check if Switch is pressed, will return a True or False Boolean
|
|
def test():
|
|
if(GPIO.input(SwitchPIN)):
|
|
return True
|
|
else:
|
|
return False
|