I have am working on a project where I'm trying to get a Raspberry Pi to play some sounds when triggered by button presses on an Arduino. The command is sent to Raspberry Pi through serial. I used this tutorial to get started: http://blog.makezine.com/2012/10/22/a-halloween-sound-trigger-with-raspberry-pi-and-arduino/
I was able to get it to work as in the tutorial but my final product will be playing a lot more sounds and it will be playing different sounds on button ups and downs. I would like to be able to send commands through the serial similar to the following:
ButtonAPressed
ButtonAReleased
ButtonBPressed
Etc
I modified the tutorial to have my arduino script send 'Serial.write("ButtonAPressed")' and I modified the pertinent portion of the python script to look something like this:
while True:
try:
val = serialFromArduino.read()
if (val != ""):
print(val)
if (val == "ButtonAPressed"):
soundChannelA.play(soundA)
if (val == "ButtonBPressed"):
soundChannelB.play(soundB)
if (val == "ButtonBReleased"):
soundChannelC.play(soundC)
val = ""
sleep(.01)
except KeyboardInterrupt:
exit()
The issue I'm having is my Print's are spitting out information similar to the following:
But
tonAPressed
ButtonBPr
essed
ButtonB
Released
So clearly the whole line isn't getting in the buffer before it's getting read. Is there a way I can make sure the whole line from arduino is in the buffer before it gets read? I don't want to increase the sleep because the button presses or press/release could be quite close together. For the sake of readability I would really rather not convert all of my commands to single characters.
serialFromArduino
? Most likely that is not reading the buffer fully or doing some sort of timed dumps.