Push button or other button from python

Hi
How could you do that from python and using a push button, you play a note or chord of sonic pi
This code is to turn on a LED (if it works):

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.OUT )

while True:
input_state = GPIO.input(24)
if input_state == False:
GPIO.output(23, True) # Enciende el LED
print(‘Botón pulsado’)
time.sleep(0.3)
GPIO.output(23, False) # Apaga el LED

and this plays a note from a python script (it also works):

from subprocess import call
call([“sonic_pi”, “play :E5”])


when i add this to the led code it doesn’t work
Thanks for your attention
thanks google translate xd

Hi @Fer, welcome!

I recommend using OSC to communicate the notes to play from Python to Sonic Pi. See this thread for an (similar) example and more detail: Link sonic pi to Python
You can adapt that code to send note values and play them in Sonic Pi - let us know if you need more help.

2 Likes

Thank you very much for answering, I will try

hi @Fer

to display code into posts more friendly, puts code between ```

I’ll keep it in mind