ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
422 stars 146 forks source link

No responding to button command #748

Closed ivan-marroquin closed 4 years ago

ivan-marroquin commented 4 years ago

In the Python script below, I would like the robot to play a wav file and stop moving when the left button is pressed. For some reason, the command to check when the left button is pressed doesn't work properly. To stop the script, I must press the backspace button.

Thanks for your help and suggestions. Below, you will find the script:

#!/usr/bin/env python3

import logging
from ev3dev2.motor import MoveTank, OUTPUT_B, OUTPUT_C
from ev3dev2.button import Button
from ev3dev2.sound import Sound
from ev3dev2.sensor.lego import InfraredSensor
from ev3dev2.led import Leds
from time import sleep
import sys

btn= Button()

tank_pair= MoveTank(left_motor_port= OUTPUT_B, right_motor_port= OUTPUT_C)

infra_sensor= InfraredSensor()

leds= Leds()

sound= Sound() 

sound.speak('Hello Doctor Marroquin', volume= 200)

sound.play_file('/home/robot/sounds/Activate.wav', volume= 100)

while (True):
    if (btn.check_buttons(buttons= ['left'])):
        sound.play_file('/home/robot/sounds/Fanfare.wav', volume= 100)
        sys.exit()

    tank_pair.on_for_seconds(left_speed= -50, right_speed= -50, seconds= 10)

    if (infra_sensor.proximity < 80 * 0.7):
        leds.set_color('LEFT', 'RED')
        leds.set_color('RIGHT', 'RED')

    else:
        leds.set_color('LEFT', 'GREEN')

        leds.set_color('RIGHT', 'GREEN')
WasabiFan commented 4 years ago

Are you holding the button for at least ten seconds? The "if" at the beginning of your loop runs once per loop iteration, and since you run the motors for ten seconds, you have to hold it long enough to make sure it's being held at the time your loop checks for pressed buttons.

ivan-marroquin commented 4 years ago

Hi @WasabiFan ,

Many thanks for the suggestion!

Ivan