I am using Sonic Pi 3.3.1
on my Windows PC and a Python
script(Python3.7.2
) on my raspberry pi(Raspbian Buster
) that detects distance from an HC-SR04
-Ultrasonic Sensor .The program then creates a tune with a pitch that ranges higher if the object is further away, this tune is then sent over OSC
to Sonic Pi. External OSC
is enabled on my windows PC. I also checked the Port
and IP
addresses, and they are correct.
I have tested my circuit extensively and I can confidently say this isn't the source of the problem, and that I added it for documentation purposes only at the bottom of my post for anyone who is interested, so I will move on.
My Python code:
from gpiozero import DistanceSensor
from time import sleep
from pythonosc import osc_message_builder
from pythonosc import udp_client
sensor = DistanceSensor(echo=17, trigger=4)
sender = udp_client.SimpleUDPClient('10.0.0.119', 4560)
while True:
pitch = round(sensor.distance * 100 + 30)
print(pitch)
sender.send_message('/play_this', pitch)
sleep(0.1)
Output:
60
60
58
59
The problem doesn't seem to be my python
code as it logs the correct values and successfully sends the pitch so once again I will move on.
My sonic Pi code:
live_loop :listen do
use_real_time
note = sync "/osc/play_this"
play note[0]
end
Output:
Sonic Pi log's the notes correctly but doesn't play it and this so the data is being received but isn't being played so the problem is most likely how I am trying to play the note but I can't find the correct way to do this
I then tried this, to test if sonic pi
installed correctly and is able to play sound:
live_loop :foo do
play 60
sleep 1
end
Sonic Pi did play a sound so I can rule out a defective installation where sound is not installed properly.
My Circuit:
The Vcc
pin on the HC-SR04
-Ultrasonic Sensor is connected to the 5V
power supply on the raspberry pi, then the Trig
pin is connected to the 4th GPIO
connector on the board the echo pin is then connected to the 17th GPIO
connector between a voltage divider consisting of a 330 ohm
resistor and a 470 ohm
resistor which protects the raspberry from a full 5V
.