lubker
Posts: 2
Joined: Wed May 13, 2015 6:52 pm

Buzzer on for 0.5 sec

Wed May 13, 2015 7:02 pm

Hi,
I'm completely new to python, and have a small problem.
I have a program which constantly reads from the serial port.
The read function is within a while(1) loop and save the received data to a mysql database.
This part is working perfect.

Now I want to add a buzzer, which is on for 0.5 sec each time I receive some data on the serial port.
Right now I'm simple setting the GPIO output to True, sleep for 0.5 sec and set the GPIO to False.
This is working, but it will unfortunately stop the reading from the serial port in the meantime.

Is there a way to put the buzzer function in a function and run it parallel with the reading function?

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Buzzer on for 0.5 sec

Thu May 14, 2015 7:37 am

Hello,

look out for 'threads'. These are executing parallel to other code.
You get a lot of tutorials in the net about this topic.

The approach is then, that in your main code you handle the serial line and start a thread as needed. Your main code continues to run, while the thread code does the buzzer job.

Regards,
Gerhard

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Buzzer on for 0.5 sec

Thu May 14, 2015 8:19 am

A time.sleep is a blocking operation. Rather than using this, if you aren't too fussed by mega accuracy you could just time the event.

Code: Select all

import time

state = False
timer = None

while true

    ##Recieve serial data code goes here

    #If we have recieved a message, turn the buzzer on
    #and mark the current time.
    if message_recieved:
        if not state :
            state = True
            timer = time.time()
            ##Turn buzzer on

    ## If the state is True and we are 0.5s past the marked time
    ## turn the buzzer off.
    if state:
        if time.time() > timer + 0.5
            state = 0
            ##Buzzer off
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Buzzer on for 0.5 sec

Thu May 14, 2015 11:11 am

It probably doesn't matter if the serial data is delayed by half a second. Serial data is buffered and will still be available to be read when the buzzer stops.

lubker
Posts: 2
Joined: Wed May 13, 2015 6:52 pm

Re: Buzzer on for 0.5 sec

Thu May 14, 2015 11:20 am

Thanks for your comments.

Right now i'm timing the event. It is working, but as I need the "exact" timestamp I cannot allow the 0.5 sec delay.
For a single read i'm not that concerned but if i read multiple values from serial almost at the same time, the 0.5 sec will not work for this project.

I'll look a bit more into threads. But If you have an example it would be great.

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Buzzer on for 0.5 sec

Thu May 14, 2015 12:06 pm

Code: Select all

import time
import threading

def worker():
    """thread worker function"""
    print 'Worker start'
    time.sleep(0.5)
    print 'Worker end'
    return

for i in range(5):
    t = threading.Thread(target=worker)
    t.start()
be aware that this code has some problems:
- timing is good, but not perfect. There is no real parallelism in a computer...
- think about multiple events starting the threads within very short timeframe. Only one beep, a long beep, multiple beeps with a short break (queue them up) ? This is where fun is starting...

Regards, Gerhard

Return to “Python”