LED Lights Sample
This sample demonstrates controlling LED lighting.
- 1.Download the sample code from https://github.com/Turta-io/RCDriverHAT/blob/master/Samples/Python/LED_Lights.py
- 2.Copy the sample code to a folder on the Raspberry Pi.
- 3.Open terminal, and then go to the folder you copied the sample.
- 4.Run the sample with the following command:
python3 LED_Lights.py
LED_Lights.py
#!/usr/bin/env python3
#This sample demonstrates controlling LED lighting.
#Install RC Driver HAT library with "pip3 install turta-rcdriverhat"
from time import sleep
from turta_rcdriverhat import Turta_RCDriver
#Initialize
rc = Turta_RCDriver.RCDriver()
try:
#Activate fan at 50% speed
rc.set_fan(50)
while True:
#Option one: Set all the LED lights in one shot
#0 is LED off, 4095 is LED full bright
#Parameters: Front, Read, Stop, Left, Right
rc.set_leds([1000, 1000, 1000, 1000, 1000])
print("LED Brightness: 1000/4096")
#Wait
sleep(1.0)
#Option two: Set the LED lights one by one
#0 is LED off, 4095 is LED full bright
rc.set_led(Turta_RCDriver.LED_OUT.FRONT, 2000)
rc.set_led(Turta_RCDriver.LED_OUT.REAR, 2000)
rc.set_led(Turta_RCDriver.LED_OUT.STOP, 2000)
rc.set_led(Turta_RCDriver.LED_OUT.LEFT, 2000)
rc.set_led(Turta_RCDriver.LED_OUT.RIGHT, 2000)
print("LED Brightness: 2000/4096")
#Wait
sleep(1.0)
#Showcase: Fade the front LED
print("LED is fading in")
for b in range(0, 1000, 5):
rc.set_led(Turta_RCDriver.LED_OUT.FRONT, b)
sleep(0.05)
print("LED is fading out")
for b in range(1000, 0, -5):
rc.set_led(Turta_RCDriver.LED_OUT.FRONT, b)
sleep(0.05)
#Wait
sleep(1.0)
#Exit on CTRL+C
except KeyboardInterrupt:
print('Bye.')
When you run the sample, it demonstrates different LED brightnesses. The program fades front LED in and out. The example output should be like this:
LED Brightness: 1000/4096
LED Brightness: 2000/4096
LED is fading in
LED is fading out
To stop the Python script, just press CTRL + C.
Last modified 3yr ago