3.2. Flashing an LED¶
3.2.1. Our goal¶
3.2.2. The code we analyse¶
The code we’re analyzing in this tutorial is the following one.
# import the EasyGoPiGo3 drivers import time import easygopigo3 as easy # Create an instance of the GoPiGo3 class. # GPG will be the GoPiGo3 object. gpg = easy.EasyGoPiGo3() # create the LED instance, passing the port and GPG my_led = gpg.init_led("AD1") # loop 100 times for i in range(100): my_led.light_max() # turn LED at max power time.sleep(0.5) my_led.light_on(30) # 30% power time.sleep(0.5) my_led.light_off() # turn LED off time.sleep(0.5)
The source code for this example program can be found here on github.
3.2.3. The modules¶
Start by importing 2 important modules:
import time import easygopigo3 as easy
easygopigo3 module is used for interacting with the GoPiGo3 robot, whilst
time module is generally used for delaying actions, commands, setting timers etc.
3.2.4. The objects¶
After this, we need to instantiate an
We are using the
EasyGoPiGo3 object for creating an instance of
which is necessary for controlling the Grove Led device.
gpg = easy.EasyGoPiGo3()
my_led = gpg.init_led("AD1")
See the following graphical representation as a reference to where the ports are.
3.2.5. Main part¶
In this section of the tutorial we are focusing on 3 methods of the
All in all, the following code snippet turns on the LED to the maximum brightness, then it sets the LED’s brightness at 30% and in the last it turns off the LED. The delay between all these 3 commands is set at half a second.
for i in range(100): my_led.light_max() # turn LED at max power time.sleep(0.5) my_led.light_on(30) # 30% power time.sleep(0.5) my_led.light_off() # turn LED off time.sleep(0.5)
3.2.6. Running it¶
Connect the Grove Led to your GoPiGo3 robot to port
"AD1" and then let’s crank up the Raspberry Pi.
For running the analyzed example program, within a terminal on your Raspberry Pi, type the following 2 commands:
cd ~/Desktop/GoPiGo3/Software/Python/Examples python easy_LED.py