playing a speaker via python/firmata


I tried to port the freqout() library to python, but it just doesn’t sound right, the pitch is wrong:

import pyfirmata
from time import sleep

PIN = 8 # Pin 12 is used
DELAY = 0.5 # A 0.5 seconds delay

Adjust that the port match your system, see samples below:

On Linux: /dev/tty.usbserial-A6008rIF, /dev/ttyACM0,

On Windows: \.\COM1, \.\COM2

PORT = ‘/dev/ttyUSB0’

Creates a new board

board = pyfirmata.Arduino(PORT)

def freqout(freq, t):
hperiod = (500000 / freq) - 7
cycles = (freq * t) / 1000
i = 0
while i <= cycles:[PIN].write(1) # Set the LED pin to 1 (HIGH)
board.pass_time(hperiod * 0.000001)[PIN].write(0) # Set the LED pin to 1 (HIGH)
board.pass_time((hperiod-1) * 0.000001)
i +=1

while True:
freqout(140, 200)

also, the script takes too much cpu (since pass_time is non blocking).

I would like to find a python program that plays an arduino speaker the right way, but I couldn’t find it
I may change firmata but not python for my project. Any help ?

use '#' tags to post code, so put your under hash tags.