3.3. Pushing a Button

3.3.1. Our goal

In this tutorial, we are going to control GoPiGo3 Dex’s eyes with a Grove Button.

3.3.2. The code we analyse

In the end the code should look like this.

# import the time library for the sleep function
import time

# import the GoPiGo3 drivers
import easygopigo3 as easy

# Create an instance of the GoPiGo3 class.
# GPG will be the GoPiGo3 object.
gpg = easy.EasyGoPiGo3()

# Put a grove button in port AD1
my_button = gpg.init_button_sensor("AD1")

print("Ensure there's a button in port AD1")
print("Press and release the button as often as you want")
print("the program will run for 2 minutes or")
print("Ctrl-C to interrupt it")


start = time.time()
RELEASED = 0
PRESSED = 1
state = RELEASED

while time.time() - start < 120:

    if state == RELEASED and my_button.read() == 1:
        print("PRESSED")
        gpg.open_eyes()
        state = PRESSED
    if state == PRESSED and my_button.read() == 0:
        print("RELEASED")
        gpg.close_eyes()
        state = RELEASED
    time.sleep(0.05)

print("All done!")

The source code for this example program can be found here on github.

3.3.3. The modules

Start by importing 2 important modules:

import time
import easygopigo3 as easy

The easygopigo3 module is used for interacting with the GoPiGo3 robot, whilst the time module is generally used for delaying actions, commands, setting timers etc.

3.3.4. The objects

After this, we need to instantiate an easygopigo3.EasyGoPiGo3 object. The EasyGoPiGo3 object is used for 2 things:

gpg = easy.EasyGoPiGo3()

Now that we have an EasyGoPiGo3 object, we can instantiate a ButtonSensor object. The argument of the initializer method is the port to which we connect the Grove Button and it’s set to "AD1".

my_button = gpg.init_button_sensor("AD1")

Note

See the following graphical representation as a reference to where the ports are.

3.3.5. Setting variables

Define 2 states for the button we’re using. We are setting the default state to "RELEASED".

start = time.time()
RELEASED = 0
PRESSED = 1
state = RELEASED

There’s also a variable called start to which we assign the clock time of that moment. We use it to limit for how long the script runs.

3.3.6. Main part

The main part is basically a while loop that’s going to run for 120 seconds. Within the while loop, we have 2 if / else blocks that define a simple algorithm: whenever the previous state is different from the current one, we either turn on or close Dex’s eyes. Here’s the logic:

  • If in the previous iteration of the while loop the button was released and now the button is 1 (aka pressed), then we turn on the LEDs and save the new state in state variable.

  • If in the previous iteration of the while loop the button was pressed and now the button is 0 (aka released), then we turn off the LEDs and save the new state in state variable.

This way, we don’t call gpg.open_eyes() all the time when the button is pressed or gpg.close_eyes() when the button is released. It only needs to call one of these 2 functions once.

while time.time() - start < 120:

  if state == RELEASED and my_button.read() == 1:
    print("PRESSED")
    gpg.open_eyes()
    state = PRESSED
  if state == PRESSED and my_button.read() == 0:
    print("RELEASED")
    gpg.close_eyes()
    state = RELEASED

  time.sleep(0.05)

time.sleep(0.05) was added to limit the CPU time. 50 mS is more than enough.

3.3.7. Running it

Make sure you have connected the Grove Button to your GoPiGo3 robot to port "AD1". Then, on the Rasperry Pi, from within a terminal, type the following commands.

cd ~/Desktop/GoPiGo3/Software/Python/Examples
python easy_Button.py
../_images/button.gif