User:Yoana Buzova/ prototyping4
- started to make a raspberry pi prototype of a voice drop object.
presumably will turn into an object that is able to record sound (via a press of a button) and playback the last recorded sound, when someone passes close to it ( to trigger attention and eventually response) .
For now , what i have works like this. Raspberry pi with a microphone, speaker and a motion sensor. After pressing a button one can make a recording of a fixed length ( cannot figure another way to do it for no). The last recording is played when there is motion detected by the sensor. After a new recording is made, it is the one that is played back into public.
the code looks like this (with Javi's help)
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os
import RPi.GPIO as GPIO
import time
PIR = 23
LED = 24
BUTTON = 25
pirState = False
pirVal = False
pirValButton = False
#we list the sounds folder where the sounds are stored and count them for not overwriting them
numAlreadyRecordedSounds=os.popen('ls -l sounds | wc -l').read()
print numAlreadyRecordedSounds
#counter = 0
#convert it as a number to assign it to the counter variable
counter = int(numAlreadyRecordedSounds)-1
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR,GPIO.IN)
GPIO.setup(LED,GPIO.OUT)
GPIO.setup(BUTTON,GPIO.IN)
#static starts and ends of the commands, the counter variable goes in between them
commandRecordBegin='arecord -f cd sounds/sound'
commandRecordEnd='.wav -d 8'
commandPlayBegin='aplay sounds/sound'
commandPlayEnd='.wav'
#we set it in case the sensor tries to play a sound before somebody press the button
commandPlay=commandPlayBegin+str(counter)+commandPlayEnd
while True:
pirValButton = GPIO.input(BUTTON)
if(pirValButton == True):
counter = counter+1
commandRecord=commandRecordBegin+str(counter)+commandRecordEnd
commandPlay=commandPlayBegin+str(counter)+commandPlayEnd
os.system(commandRecord)
print 'back from recording'
else:
pirVal = GPIO.input(PIR)
if(pirVal == True):
GPIO.output(LED, True)
os.system(commandPlay)
GPIO.output(LED, False)
print 'back from playing'
prototyping slow...learning by trying what i need...ordering stuff and waiting for it. Like the usb sound card.
Next to do :
- build an amplifier for the mic so i can actually make a recording!
- get a battery for the pi