John Revier
Posts: 2
Joined: Sun Aug 19, 2018 1:31 pm

Recording while button is pressed.

Sun Aug 19, 2018 1:59 pm

Hello,

For my son who is vocally challenged I am building a sound recorder/player.
When a button is pushed it should start recording. When released it should be played back.
This way he can hear what he said. And what he needs to correct.

I made a board with a rpi boot and shut down button and that works.
also there is a button to start/stop recording.

As I press the record button I can see erratic behaviour with the print debug lines.
I need to de-bounce the button. Is my waiting for the button pressing ok?

Any tips on the recording method?

The USB soundboard is being ordered.

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
# Leds    6 22
# buttons 4 27
recbutton=27
recled=22

GPIO.setup(recbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(recled, GPIO.OUT)
print "Led Off"
while 1:

		GPIO.wait_for_edge(recbutton, GPIO.RISING)
		print "LED on"
		GPIO.output(recled,GPIO.HIGH)
		GPIO.wait_for_edge(recbutton, GPIO.FALLING)
		print "LED off"
		GPIO.output(recled,GPIO.LOW)

Thanks John.

User avatar
MrYsLab
Posts: 130
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Recording while button is pressed.

Wed Aug 22, 2018 10:34 pm

I am not sure if this answers your question. Below is a snippet of code that will light an LED when a button is pressed and extinguish it when released. I use pigpio since I find it the most reliable and versatile GPIO library for the RPi. To use this code, you will need to start up the pigpio daemon if not already started from within a command window.

Code: Select all

sudo pigpiod
Here is the code:

Code: Select all

import pigpio
import time


class PlayRecord:

    def __init__(self, led_pin, button_pin, pullup_pulldown=pigpio.PUD_OFF,
                 debounce_time=800):
        """
        Set input mode for button pin, output mode for led pin
        :param led_pin: bcm pin number
        :param button_pin: bcm pin number
        :param pullup_pulldown: defaults to pigpio.PUD_OFF, other choices are
                                pigpio.PUD_UP, or pigpio.PUD_DOWN
        :param debounce_time: default 800 microseconds
        """

        self.led_pin = led_pin

        # instantiate pigpio
        self.rpi = pigpio.pi()

        # set the mode for the record/playback button
        self.rpi.set_mode(button_pin, pigpio.INPUT)
        self.rpi.set_pull_up_down(button_pin, pullup_pulldown)

        # set a glitch filter to debounce the switch
        self.rpi.set_glitch_filter(button_pin, debounce_time)

        # set a callback method for when the state of the button changes
        self.rpi.callback(button_pin, pigpio.EITHER_EDGE,
                          self.button_process)

        # set the pin mode for the led
        self.rpi.set_mode(led_pin, pigpio.OUTPUT)

        # a do nothing loop
        while True:
            time.sleep(.01)

    def button_process(self, gpio, level, tick):
        """
        This method is called whenever the state of the button changes.
        Parameters below are filled in by pigpio
        :param gpio: bcm pin number
        :param level: pin level - 0 = change to low (a falling edge)
                                  1 = change to high (a rising edge)
                                  2 = no level change (a watchdog timeout)

        :param tick: time stamp of when the change occurred
        :return:
        """
        if level == 0:
            self.rpi.write(self.led_pin, 0)
            print('led off')
        elif level == 1:
            self.rpi.write(self.led_pin, 1)
            print('led on')
        else:
            print('Unexpected level', level)

# using a pibrella led is green led (bcm 4), and button is bcm 11
PlayRecord(4, 11)

John Revier
Posts: 2
Joined: Sun Aug 19, 2018 1:31 pm

Re: Recording while button is pressed.

Sat Aug 25, 2018 7:26 pm

Thanks,

I will study this :geek:

User avatar
OutoftheBOTS
Posts: 668
Joined: Tue Aug 01, 2017 10:06 am

Re: Recording while button is pressed.

Sat Aug 25, 2018 9:22 pm

Yes a small sleep before testing for next edge will allow for button bounce to finsh before testing again

something like this

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess, time

GPIO.setmode(GPIO.BCM)
# Leds    6 22
# buttons 4 27
recbutton=27
recled=22

GPIO.setup(recbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(recled, GPIO.OUT)
print "Led Off"
while 1:

		GPIO.wait_for_edge(recbutton, GPIO.RISING)
		print "LED on"
		GPIO.output(recled,GPIO.HIGH)
		time.sleep(0.05)
		GPIO.wait_for_edge(recbutton, GPIO.FALLING)
		print "LED off"
		GPIO.output(recled,GPIO.LOW)
		time.sleep(0.05)


Return to “Python”