import pygame
import time
import random
import os
import serial
# Initialize pygame mixer
pygame.mixer.init()
serial_port = "/dev/ttyUSB0"
baud_rate = 115200
ser = serial.Serial(serial_port, baud_rate)
# Set the volume to maximum
pygame.mixer.music.set_volume(1.0)
# Define the folder paths
SMALL_FOLDER = 'small'
BIG_FOLDER = 'big'
# Function to play a random sound from the given sound list
def play_random_sound(sound_list):
random_sound = random.choice(sound_list)
random_sound.play()
time.sleep(random_sound.get_length())
pygame.mixer.stop()
# Function to load sound files from a folder
def load_sounds_from_folder(folder):
folder_path = os.path.join(os.getcwd(), folder)
sound_files = os.listdir(folder_path)
sound_list = []
print(f"Loading sounds from '{folder}' folder...")
for filename in sound_files:
sound = pygame.mixer.Sound(os.path.join(folder_path, filename))
sound_list.append(sound)
print(f"Loaded {len(sound_list)} sound(s) from '{folder}' folder.")
return sound_list
# Load the sound files from the small folder
small_sounds = load_sounds_from_folder(SMALL_FOLDER)
# Load the sound files from the big folder
big_sounds = load_sounds_from_folder(BIG_FOLDER)
# Function to check the latest command received on the serial port
def check_latest_command():
latest_command = ''
while ser.in_waiting:
latest_command = ser.readline().decode('utf-8').strip()
time.sleep(5/1000)
return latest_command
# Play the sounds based on the latest command received
print("Listening to the serial port...")
print("Send 's' to play a random sound from the 'small' folder.")
print("Send 'b' to play a random sound from the 'big' folder.")
print("Press Ctrl+C to exit.")
try:
while True:
latest_command = check_latest_command()
if latest_command == 's':
play_random_sound(small_sounds)
ser.reset_input_buffer() # Clear serial buffer
elif latest_command == 'b':
play_random_sound(big_sounds)
ser.reset_input_buffer() # Clear serial buffer
time.sleep(50/1000) # In seconds
except KeyboardInterrupt:
pass
# Clean up and exit
pygame.mixer.quit()
If you want to run the script as a service so it can start automatically on boot and be controlled with systemctl commands, put this file in /etc/systemd/system/play.service
. You might need to change the directory /home/pi/play.py
to the location you saved the script.
[Unit]
Description=Play Script
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/play.py
WorkingDirectory=/home/pi
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=3
User=pi
[Install]
WantedBy=multi-user.target
To controll the ToF sensor, you need to install the SparkFun_VL6180X library in you IDE of choice, connect all the wireing and simply upload following code to the Arduino Nano:
#include <Wire.h>
#include <SparkFun_VL6180X.h>
#define VL6180X_ADDRESS 0x29
VL6180x sensor(VL6180X_ADDRESS);
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200); // Start Serial at 115200bps
Wire.begin(); // Start I2C library
delay(100); // delay .1s
if (sensor.VL6180xInit() != 0)
{
Serial.println("Failed to initialize. Freezing..."); // Initialize device and check for errors
while (1);
}
sensor.VL6180xDefautSettings(); // Load default settings to get started.
delay(1000); // delay 1s
}
void loop()
{
if(sensor.getDistance() < 20.) // big money
{
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("b");
}
else if(sensor.getDistance() < 55.) // small money
{
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("s");
}
else
digitalWrite(LED_BUILTIN, LOW);
delay(50);
}